如何在没有点击事件的情况下在计时器事件上生成Toast消息?

时间:2014-03-23 04:47:16

标签: android timer

b=(Button) findViewById(R.id.btn);
b.setOnClickListener(new View.OnClickListener() {       
@Override
   public void onClick(View v) 
   {
       // TODO Auto-generated method stub
       Calendar c = Calendar.getInstance();
       SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
       String formattedDate = df.format(c.getTime());
       Toast.makeText(MainActivity.this,"Current time => " + formattedDate ,1).show();
   }
});

1 个答案:

答案 0 :(得分:0)

在一秒钟后重复使用计时器,计时器将运行30秒

new CountdownTimer(30000, 1000) {

 public void onTick(long millisUntilFinished) {
 Calendar c = Calendar.getInstance();
   SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
   String formattedDate = df.format(c.getTime());
   Toast.makeText(MainActivity.this,"Current time => " + formattedDate ,1).show();
 }
 public void onFinish() {
  finish();
  }
  }.start();