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 方法来启动你的计数器

但要小心,您只能从创建此视图的线程更改视图。一种方法是在视图上发布可运行

    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 回答