简体   繁体   English

如何显示在Android中立即覆盖旧Toast的新Toast

[英]How to display new Toast that overrides old Toast in no time in android

I have a an array of values displayed in the form of dialog in android, tap on any of the item, displays that particular toast. 我在android中以对话框的形式显示了一个值数组,点击任何项目,显示特定的吐司。 Now if I tap on other item on this list (array) in the very next moment, that particular toast displays after short duration(Approximately 5sec) till then old toast is displayed. 现在,如果我在下一刻点击此列表(阵列)上的其他项目,则在短时间(大约5秒)之后显示该特定的吐司,直到显示旧的吐司。 If I wanted to display new Toast immediately after I select/tap other item on the list what can I do ? 如果我想在选择/点击列表中的其他项目后立即显示新的Toast,我该怎么办

So can someone tell me what is the logic to achieve this ? 那么有人可以告诉我实现这一目标的逻辑是什么?

For instance consider this code - 例如,考虑这个代码 -

     String NumberOfItems[] = { "1", "2", "3" };
     Activity mActivity;
     int id =0;
     AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
     builder.setIcon(R.drawable.ic_launcher);
     builder.setTitle(R.string.dialog_heading);
     builder.setSingleChoiceItems(NumberOfItems, id, new DialogInterface.OnClickListener() 
      {
        public void onClick(DialogInterface dialog, int id)
         {

       switch(id)
         {
         case 0:
              Toast.makeText(mActivity.getBaseContext(),"Item selected is 1",Toast.LENGTH_SHORT).show();
                   break;
         case 1:
              Toast.makeText(mActivity.getBaseContext(),"Item selected is 2",Toast.LENGTH_SHORT).show();
                   break;
         case 2:
              Toast.makeText(mActivity.getBaseContext(),"Item selected is 3",Toast.LENGTH_SHORT).show();
                   break;

                } 

          }
        });  
            AlertDialog alertDialog =  builder.create();
            alertDialog.show(); 

Now Dialog containing 3 items displays, tap on 1st item displays "Item selected is 1" soon I tap on next item but it displays "Item selected is 2" after ~5sec . 现在显示包含3个项目的对话框,点击第1个项目显示“项目选择为1”我很快点击下一个项目,但在约5秒后显示“项目选择为2” But if I wanted to display immediately on tap of 2nd item what can I do ? 但如果我想立即显示第二项 ,我该怎么办?

In short what is the logic to refresh Toast ? 简而言之,刷新Toast的逻辑是什么?

Any help is greatly appreciated. 任何帮助是极大的赞赏。

you can initialize a single Toast in the activity. 您可以在活动中初始化单个Toast。 Then changing its text on each click. 然后在每次单击时更改其文本。

Toast mToast = Toast.makeText( this  , "" , Toast.LENGTH_SHORT );

 switch(id)
     {
     case 0:
           mToast.setText( "Item selected is 1" );
           mToast.show();

               break;
     case 1:
          mToast.setText( "Item selected is 2" );
          mToast.show();
               break;
     case 2:
          mToast.setText( "Item selected is 3" );
          mToast.show();
               break;
            } 
      }
    });  

i think this worked for you. 我认为这对你有用。

more info 更多信息

Make a reference of that toast. 参考那个吐司。 Usually everyone uses Toast.makeText(..) to show toast and does not create reference 通常每个人都使用Toast.makeText(..)来显示toast并且不会创建引用

Toast t = new Toast(this, "Text 1", Toast.LENGTH_LONG);
t.show();
t.cancel();
t = new Toast(this, "Text 2", Toast.LENGTH_LONG);
t.show();


t.show(); will stack the toast and displays them sequentially. 将堆叠吐司并按顺序显示它们。 you have to call cancel() to cancel the toast. 你必须调用cancel()来取消祝酒。 Now its the t.cancel() that makes the difference. 现在它的t.cancel()产生了不同。
This code will cancel the "Text 1" toast if it is showing and display "Text 2" immediately. 此代码将取消“文本1”吐司,如果它正在显示并立即显示“文本2”。
You can use this code with appropriate variation according to your needs. 您可以根据需要使用此代码并进行适当的变更。

I think you want to show toast in lesser frequency and as per my consideration the message displayed in this app . 我想你想以较低的频率显示吐司,并根据我的考虑显示在这个应用程序中显示的消息。

For that you can do one thing use relative layout and over listview put one textview with background like toast and apply text value on clicked item and use show hide on it. 为此,您可以使用相对布局做一件事,并且在listview上放置一个带有toast等背景的textview,并在点击的项目上应用文本值并在其上使用show hide。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM