0

このアプリを作成し、テキストビューを使用して他のアクティビティが開始するまでの秒数を表示したかったのですが、方法がわかりません。カウントダウンタイマー内にtxtviewを作成しましたが、表示されませんでした。

Event=new String(Edt.getText().toString());
final int time = Integer.parseInt(sec.getText().toString());

Intent myInt = new Intent(MainActivity.this,Receiver.class);

myInt.putExtra("key",Event);
endingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,2,myInt,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(time*1000),pendingIntent);

new CountDownTimer(time*1000, 1000) {

    @Override
    public void onTick(long millisUntilFinished) {
    // TODO Auto-generated method stub
        txtV.setText("Activity starts"+millisUntilFinished/1000+"seconds"); // here is the txtV which isn't shown 
    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub

    }
};
4

1 に答える 1

2

まず、 startメソッドを呼び出してカウンターを開始する必要があります

ただし、このビューを作成するスレッドからのみビューを変更できることに注意してください。あなたがそれをすることができる1つの方法はビューで実行可能なポストです:

    CountDownTimer timer = new CountDownTimer(time*1000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            txtV.post(new Runnable() {
                @Override
                public void run() {
                            txtV.setText("Activity starts"+millisUntilFinished/1000+"seconds"); // here is the txtV which isn't shown 
                }
            });
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub

        }
    };
    timer.start();
于 2013-03-25T13:18:28.067 に答える