以编程方式安装Android Toast通知? [英] Android Stop toast notification programmatically?

查看:180
本文介绍了以编程方式安装Android Toast通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法可以通过编程方式停止Toast消息?

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

假设我有一个按钮,我点击滚动查看吐司信息,在onclick事件中,我想停止所有队列,只显示新的,如何我会这样做吗?

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++;
    }
}
}


推荐答案

这就是我实现这一目标的方式。

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.

这篇关于以编程方式安装Android Toast通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆