Android Stop toast notification programmatically?

。_饼干妹妹 提交于 2019-12-10 10:24:25

问题


Is there a way I can stop a toast message programmatically?

Say I have a button which I click to scroll through toast messages, and in the onclick event I wanted to stop all in the queue and just show the new one, how would I do that?

A simplified version of my code is below - Code:

public class Help extends Activity{

LinearLayout background;
int screenNo = 1;
Toast toast;

 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help);

       background = (LinearLayout) findViewById(R.id.helpLayout);

        ImageButton next = (ImageButton) findViewById(R.id.imageButtonNext);
        next.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                toast.cancel();
                showNextScreen();
            }});        
 }

private void showMessageBox(String title, String msg) {
    AlertDialog.Builder b = new AlertDialog.Builder(this);
     b.setTitle(title);
     b.setMessage(msg);
     b.setPositiveButton("Next", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface arg0, int arg1) {
             showNextScreen();
        }});
     b.setNegativeButton("Quit Help", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface arg0, int arg1) {
                 returnHome();
        }});
     b.show();
 }

private void showNextScreen() {
    int time = 7000;
    String tstMsg = "error";    

    switch (screenNo) {
    case 1:
        break;          
    case 2:
        break;          
    case 3:
        break;          
    case 4:
        break;
    case 5:
        toast.cancel();
        returnHome();
        break;

    default:
        break;
    }

    if(screenNo < 5)
    {
    toast=Toast.makeText(this, tstMsg, time);   
    toast.setGravity(Gravity.BOTTOM, 0, 0);
    toast.show();
    screenNo++;
    }
}
}

回答1:


This is how i achieved this one.

public static Toast toastShow;

public void showToast(Activity actRef, String message) {

    if (toastShow == null
            || toastShow.getView().getWindowVisibility() != View.VISIBLE) {
        toastShow = Toast.makeText(actRef, message, Toast.LENGTH_SHORT);
        toastShow.setGravity(Gravity.CENTER, 0, 0);
        toastShow.show();
    }
}

define above code in separate class and instantiate that class where you want show message,you are done with it.




回答2:


Create a custom global object

private Toast toast;

Initialize it in onCreate

 toast = Toast.makeText(YOUR_CLASS_NAME.this, "", Toast.LENGTH_SHORT);

Whenever you need to show a Toast

toast.setText("Hi....");
toast.show();

To kill all the message based on requirement onPause or onDestroy

toast.cancel();



回答3:


You're all free to cancel the Toast object.



来源:https://stackoverflow.com/questions/8563439/android-stop-toast-notification-programmatically

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