杀死Toast消息

时间:2017-06-29 05:53:55

标签: java android-toast

在我的应用程序中,有13个按钮具有不同的祝酒词。 当我点击另一个按钮时,吐司没有停止正常销毁,这是我的代码。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button one = (Button) this.findViewById(R.id.gg);
        final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.gg);
        one.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                Toast toast  = Toast.makeText(MainActivity.this, "Good Game", Toast.LENGTH_SHORT);
                toast.show();
                mp1.start();
                toast.cancel();
            }
        });

2 个答案:

答案 0 :(得分:0)

您可以通过致电Toasts对象上的cancel()来取消个人Toast

 if (toast != null)
 {
   toast.cancel();
 } 

您可以参考Kill android toast?

答案 1 :(得分:0)

您需要为吐司添加持续时间,这就是吐司自毁的方式。

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();

你可以在吐司实现上找到很多例子。

Android Developer Website - Toast Example

相关问题