English 中文(简体)
根据安乐施会[复制品]的pin子从发送的纽扣
原标题:Delaying toast from the send button according to spinner in Android [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Android: Toast won t delay on spinner

I want to delay the toast "Your message "message" is sent to "contact" to appear according to the delay the user chose in the spinner. The following codes has errors (pos), here s my code:

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

我应如何宣布这一说法?

最佳回答

仅宣布全球一级变数<代码>(int=10)和特定中继器议程项目的储存位置(count=pos),并使用您寄出的Button s onClick(> >中该变量在<代码>(如果(count==1)上

为此,

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();
            }
        });        
    }
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签