根据Android中的微调器,从发送按钮延迟吐司

时间:2011-11-03 07:05:55

标签: android spinner delay toast

  

可能重复:
  Android: Toast won't delay on spinner

我想延迟吐司“你的消息”消息“被发送到”联系人“根据用户在微调器中选择的延迟出现。以下代码有错误(pos),这是我的代码:

Handler handler = new Handler();
spinnerTimeDelay = (Spinner) findViewById(R.id.spinner_delay);
spinnerTimeDelay.setOnItemSelectedListener(this);
        ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,items);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerTimeDelay.setAdapter(aa);

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

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

btnSend.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    String phoneNo = editTextRecipient.getText().toString();
                    String message = editTextNewMessage.getText().toString(); 
                    boolean split = false;

                final Toast toast = Toast.makeText(getBaseContext(), 
                         "Your message " + "\"" + message + "\"" + " is sent to " +"\""+ phoneNo+"\"", 
                          Toast.LENGTH_SHORT);1

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

                if (phoneNo.length()>0 && message.length()>0)  {
                    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);
                      }
                }

                else
                    Toast.makeText(getBaseContext(), 
                        "Please enter both phone number and message.", 
                        Toast.LENGTH_SHORT).show();
            }
        });        
    }

我该如何申报pos?

1 个答案:

答案 0 :(得分:2)

只需在全局声明一个int变量(int count=0)并存储所选微调器项(count=pos)的位置,并在sendButton的onClick()的if-else条件(if(count==1))中使用该int变量

试试这个,

private int count=0;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
Handler handler = new Handler();
spinnerTimeDelay = (Spinner) findViewById(R.id.spinner_delay);
spinnerTimeDelay.setOnItemSelectedListener(this);
        ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,items);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerTimeDelay.setAdapter(aa);

public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {

           count=pos;
        if(FirstLoad){
            FirstLoad = false;
            return;                         
        }
}

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

btnSend.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    String phoneNo = editTextRecipient.getText().toString();
                    String message = editTextNewMessage.getText().toString(); 
                    boolean split = false;

                final Toast toast = Toast.makeText(getBaseContext(), 
                         "Your message " + "\"" + message + "\"" + " is sent to " +"\""+ phoneNo+"\"", 
                          Toast.LENGTH_SHORT);1

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

                if (phoneNo.length()>0 && message.length()>0)  {
                    if (count == 0) {
                          handler.postDelayed(showToastRunnable, 0);
                      }
                      else if (count == 1) {
                          handler.postDelayed(showToastRunnable, 15000);
                      }
                      else if (count == 2) {
                          handler.postDelayed(showToastRunnable, 30000);
                      }
                      else if (count == 3) {
                          handler.postDelayed(showToastRunnable, 60000);
                      }
                }

                else
                    Toast.makeText(getBaseContext(), 
                        "Please enter both phone number and message.", 
                        Toast.LENGTH_SHORT).show();
            }
        });        
    }