如何重复Toast一段时间而不是像现在一样循环,同时考虑定时器延迟?
public class MainActivity extends Activity {
Button btn;
EditText edit;
TextView view;
Timer myTimer = new Timer();
MyTimerTask myTimerTask= new MyTimerTask();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn =(Button)findViewById(R.id.bu);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit=(EditText)findViewById(R.id.ed1);
view=(TextView)findViewById(R.id.te);
int aa = Integer.valueOf(edit.getText().toString());
view.setText(edit.getText().toString());
myTimer.scheduleAtFixedRate(myTimerTask, aa, aa);
}
});
}
private class MyTimerTask extends TimerTask {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run()
//code to do
Toast.makeText(getBaseContext(), "Toast test", Toast.LENGTH_SHORT).show(); //how to repeat this toast for certain counts not just looping like now
}
});
}
}
答案 0 :(得分:0)
您可以在MyTimerTask.run()
的每次执行中递增计数器。然后,当此计数器等于您希望其显示Toast通知的次数时,您可以使用TimerTask
取消myTimer.cancel();
。