0

如何在考虑计时器延迟的同时重复 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
            }
        });
    }
}
4

1 回答 1

0

您可以在每次执行MyTimerTask.run(). 然后,当此计数器等于您希望它显示 Toast 通知的次数时,您可以取消TimerTaskmyTimer.cancel();

于 2013-06-21T16:58:15.620 回答