Android: Toast won't delay on spinner

谁说我不能喝 提交于 2019-12-20 06:48:52

问题


I want to delay the toast on selected delay times like (15, 30, 60 seconds and no delay) but it won't work. Here's the code:

public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        if(FirstLoad){
            FirstLoad = false;
            return;                         
        }
          Toast.makeText(parent.getContext(), "You chose " + 
              parent.getItemAtPosition(pos).toString()+ " to delay", Toast.LENGTH_LONG).show();

          Message message = new Message();
          Bundle bun = new Bundle();
          bun.putString("delay", parent.getItemAtPosition(pos).toString());
          message.obj = bun;
          if (pos == 0) {
              handler.sendMessageDelayed(message, 0);
          }
          else if (pos == 1) {
              handler.sendMessageDelayed(message, 15000);
          }
          else if (pos == 2) {
              handler.sendMessageDelayed(message, 30000);
          }
          else if (pos == 3) {
              handler.sendMessageDelayed(message, 60000);
          }
          //handler.sendMessageDelayed(message, 15000);
        }

        public void onNothingSelected(AdapterView<?> parent) {
          return;
        }

Help Please.


回答1:


Try this :

    final Toast toast = Toast.makeText(parent.getContext(), "You chose "
            + parent.getItemAtPosition(pos).toString() + " to delay",
            Toast.LENGTH_LONG);

    Runnable showToastRunnable = new Runnable() {
        public void run() {
            toast.show();
        }
    };

    if (pos == 0) {
        handler.postDelayed(showToastRunnable, 0);
    } else if (pos == 1) {
        handler.postDelayed(showToastRunnable, 15000);
    } else if (pos == 2) {
        handler.postDelayed(showToastRunnable, 30000);
    } else if (pos == 3) {
        handler.postDelayed(showToastRunnable, 60000);
    }

Edit:

By the way, I want to transfer this to the send button, i want to delay the toast of "Message sent" according to the delay the user chose. How should I implement it?

How are you fetching the delay? Is it something the user enters in an EditText? In that case you could just get the delay like this :

int delay = Integer.parseInt(delayEditText.getText().toString());

and then use that delay amount to post the runnable to the handler like this :

handler.postDelayed(showToastRunnable, delay);

You can remove your entire if-else block in this case.




回答2:


for this you can use custom dialog and hide it after a particular time.

    class CustomDialog extends Dialog
    {
             setContentView(R.layout.dialogxml);    
         txtview=(TextView)findViewById(R.id.txtmsg);
    }
    Customdialog dialog= CustomDialog.show();

    dialog.hide();



回答3:


Handler hl_DelayedToast = new Handler(); // scope global

public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) 
{
    if(FirstLoad)
    {
        FirstLoad = false;
        return;                         
    }

    //if else logic to check the time
    // if 0
    hl_DelayedToast.postDelayed(mytoastshower,0);
    // if 1
    hl_DelayedToast.postDelayed(mytoastshower,1000);
}

public Runnable mytoastshower = new Runnable
{ 
    public void run()
    {
        Toast.show();// show the toast
    }
}

hope it helps.




回答4:


Declare your handler this way:

Hanlder handlder=new Handler() {

    public  void handleMessage (Message msg) {
         Toast.makeText(YOUR_ACTIVITY_CLASS_NAME.this,"You chose"+(Bundle(msg.obj)).getString("delay","DEFAULT_VALUE")+"to delay",Toast.LENGTH_LONG).show();
      }
    };

Simply, you don't have to use a bundle, but you can call msg.what=THE DELEY TIME. Also, you can call handler.obtainMessage to get a message. See http://developer.android.com/reference/android/os/Handler.html#obtainMessage%28%29 So every time you send a message, it will be handled here, and thus you call show the toast. Sorry that I don't have Eclipse installed on this laptop, so I can not test the code. However, I believe it works.



来源:https://stackoverflow.com/questions/7976474/android-toast-wont-delay-on-spinner

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