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

守給你的承諾、 提交于 2019-12-05 19:25:32
nikis

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);

    }
});
super-qua

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.

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);

}
Piyush

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);

        }
});
Yashdeep Patel
new Handler().postDelayed(new Runnable() {

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

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