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. 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. If I wanted to display new Toast immediately after I select/tap other item on the list what can I do ?

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. 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 ?

Any help is greatly appreciated.

有帮助吗?

解决方案

you can initialize a single Toast in the activity. 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 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. Now its the t.cancel() that makes the difference.
This code will cancel the "Text 1" toast if it is showing and display "Text 2" immediately.
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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top