2012-08-11 75 views
0

我如何要求用戶輸入數據到EditText,並且在填充EditText之前不允許應用程序繼續進行?空白EditText上彈出錯誤

現在,即使在用戶確認錯誤消息指出EditText爲空並且是必需的之後,我的應用程序仍會繼續進行。

  private final static int EMPTY_TEXT_ALERT = 0; 

    protected Dialog onCreateDialog(int id) { 

    switch(id) { 

     case EMPTY_TEXT_ALERT: { 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("oops!!") 
        .setPositiveButton("ok", new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.cancel(); 
         } 
      }); 

      return builder.create(); 
     } 
    } 

    return null; 


} 




     public void sends(View v) { 

      DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);  

      int year = datePicker.getYear(); 
      int month = datePicker.getMonth(); 
      int day = datePicker.getDayOfMonth(); 
       final EditText phone = (EditText) findViewById(R.id.editText2); 

      final EditText nameplate = (EditText) findViewById(R.id.editText3); 

      final EditText issue = (EditText) findViewById(R.id.editText4); 



        String ph = phone.getText().toString(); 
        if(ph.trim().equals("")) { 
          // text is empty 

          showDialog(EMPTY_TEXT_ALERT); 

         } 
        String np = nameplate.getText().toString(); 
        if(np.trim().equals("")) { 
         // text is empty 

         showDialog(EMPTY_TEXT_ALERT); 
        } 
        String i = issue.getText().toString(); 
        if(i.trim().equals("")) { 
          // text is empty 

          showDialog(EMPTY_TEXT_ALERT); 
         } 


        else 
         {StringBuilder s= new StringBuilder(100); 
      s.append(year); 
      s.append(". "); 
      s.append(month+1);// month starts from 0 in this 
      s.append(". "); 
      s.append(day); 
      s.append(". "); 
      s.append(". "); 
      s.append(ph); 
      s.append(". "); 
      s.append(np); 
      s.append(". "); 
      s.append(i); 

      String st=s.toString(); 


      Intent emailIntentt = new Intent(android.content.Intent.ACTION_SEND); 
      emailIntentt.setType("plain/text"); 


      String aEmailList[] = { "[email protected]" }; 


      emailIntentt.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList); 

      emailIntentt.putExtra(android.content.Intent.EXTRA_SUBJECT, "Feedback"); 


      emailIntentt.putExtra(android.content.Intent.EXTRA_TEXT, st); 


      startActivity(emailIntentt); 
     } 

}} 

回答

0

Shreyas Tallani

如何驗證電話號碼...進入超過10個數字應顯示

的 錯誤消息,如果你只是想測試String的長度,只需獲得String並將長度與最大長度10進行比較。

在你validate(...)方法做類似如下的內容:

String ph = phone.getText().toString(); 
if(ph.trim().equals("")) { 
    showDialog(EMPTY_TEXT_ALERT); 
} else if (ph.length() > 10) { 
    showDialog(TEXT_TOO_LONG_ALERT); 
} 

你也可以讓你的EditText只允許數值。這將幫助您驗證數字。您可以在xml文件或代碼中執行此操作。

XML

android:inputType="TYPE_NUMBER_VARIATION_NORMAL" 

代碼

EditText.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL); 

你可以做的第一件事,就是添加一個validate(...)方法。在validate(...)內部,您需要驗證所有字段,如果有任何內容留空,則顯示錯誤消息並停止應用程序進度。

如果所有的字段都很好,那麼調用你的send方法。 send(...)只應發送您的數據,而不是檢查驗證。

+0

嘿謝謝.. :)我已經找到了錯誤... :) – Shreya 2012-08-11 15:58:50

+0

我需要1個更多的幫助...如何驗證電話號碼..「ph」代表電話號碼...如果用戶輸入10位以上的數字應顯示錯誤信息:) – Shreya 2012-08-11 15:59:27

+0

在我的回答中添加了有關您的問題的信息。 – prolink007 2012-08-11 21:05:32

0

您可以在顯示對話框後添加return語句,如下所示。

if(i.trim().equals("")) { 
          // text is empty 

          showDialog(EMPTY_TEXT_ALERT); 
          return; 
         } 

儘管使用Toast消息比showDialog更好。

+1

吐司有時是更好的選擇。但是如果你想強制用戶注意到這個字段是空白的,你需要使用一個'Dialog',用戶必須按下「ok」或者什麼來關閉'Dialog'。敬酒不需要用戶輸入進度,有時難以看到。 – prolink007 2012-08-11 15:46:55

+0

@gogu:它的工作.....非常感謝:) :) – Shreya 2012-08-11 15:47:32

+0

@ prolink007:是的對話更適合我的應用程序...我想向用戶展示錯誤... – Shreya 2012-08-11 15:49:53

0

我不知道你是如何調用你的sending()方法的,但是在任何空的錯誤之後,你可以在showDialog()之後立即添加一個return語句。這意味着在用戶輸入文本後,必須通過UI重新調用send()方法。

如果您通過onClick()從按鈕調用了sending()方法,那麼這意味着用戶將看到帶有錯誤的對話框,輸入一些文本,然後點擊按鈕再次發送。

+0

是啊,這個問題解決了:)我需要1更多的幫助...如何驗證電話號碼..「ph」代表電話號碼...如果用戶輸入超過10位數應顯示錯誤消息。 – Shreya 2012-08-11 15:57:26