Can I use a Toast for the CountDownTimer

六月ゝ 毕业季﹏ 提交于 2019-12-25 06:39:16

问题


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:


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



来源:https://stackoverflow.com/questions/15174293/can-i-use-a-toast-for-the-countdowntimer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!