Android: Display a toast message after a custom time when a button is clicked

拜拜、爱过 提交于 2019-12-12 09:46:49

问题


I want to add a toast say after 30 seconds when a button is clicked. Can you please help me out.


回答1:


Something like that:

Button button = new Button(this);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, "Hello!", Toast.LENGTH_LONG).show();
            }
        }, 30000);

    }
});



回答2:


You can use a Handler with postDelayed(). You can find the documentation here

For example:

new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {

             // Put your Toast here

        }
}, 30 * 1000);

You have to watch out which Thread your Handler is running on. If you want to make UI modifications (like the Toast), you have to attach the Handler on your UI-Thread.




回答3:


You can use postDelayed() method of Handler...pass a Thread and specific time after which time the Thread will be executed as below...

private Handler mTimerHandler = new Handler();

private Runnable mTimerExecutor = new Runnable() {

    @Override
    public void run() {
        Toast.makeText(Activity.this, "Button Clicked", Toast.LENGTH_LONG).show().
    }
};

Then call as below inside the onClick() method...

public void onClick(View view) {

    mTimerHandler.postDelayed(mTimerExecutor, 30000);

}



回答4:


you can implement this on button click event.

button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            Toast.makeText(Activity.this, "Button Clicked", Toast.LENGTH_LONG).show();

    }, 3000);

        }
});



回答5:


new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {
        // Toast message here
    }
}, 500);



回答6:


You can start a timer for 30 second after the button is clicked and then in onFinish() method you can display the toast message.

public class TestActivity extends Activity{

private MyCounter mCounter;
private Button mBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    mBtn=(Button)findViewById(R.id.btn);

    mBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            mCounter=new MyCounter(40000, 10000);
            mCounter.start();
        }
    });
}

private class MyCounter extends CountDownTimer{

    public MyCounter(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Display your text here",                  Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub
        Log.i("on tick>>>>>>",millisUntilFinished+">>>>>");
    }
}
}


来源:https://stackoverflow.com/questions/22323119/android-display-a-toast-message-after-a-custom-time-when-a-button-is-clicked

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