Can I use a Toast for the CountDownTimer

2019-09-07 15:49发布

I want to use a Toast inside the CountdownTimer, but the problem is the Toast counts too slow and when the new Activity stars the Toast isn't finished counting.I know it is easier to use a TextView but I just wanted to know if it is possible.Any ideas?

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    if(Edt.getText().toString().length() == 0){
        Toast.makeText(MainActivity.this,"What, bro?",Toast.LENGTH_LONG).show();
    }else if(sec.getText().toString().length() == 0){
        Toast.makeText(MainActivity.this,"When, bro?",Toast.LENGTH_LONG).show();

    }else{
    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);
    PendingIntent 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) {

         public void onTick(long millisUntilFinished) {

            Toast.makeText(MainActivity.this,"Alarm starts in"+ +millisUntilFinished/1000 + "seconds",Toast.LENGTH_SHORT).show();

         }

         public void onFinish() {


         }
      }.start();


}

1条回答
Summer. ? 凉城
2楼-- · 2019-09-07 16:31

If you mean that they stack up causing a delay then you should cancel the prior toast before showing a new one.

If you want something more fancy you could try using a PopupWindow instead to show the countdown, there you have more freedom for layout etc.

http://developer.android.com/reference/android/widget/PopupWindow.html

查看更多
登录 后发表回答