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

有帮助吗?

解决方案 2

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

    }
});

其他提示

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

}

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

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

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

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+">>>>>");
    }
}
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top