2012-12-18 164 views
-3

按下按鈕時的Android輸入驗證,並在對話框中顯示驗證消息,如下所示。Android edittext驗證如對話框

Codename is required 
Password is required 
USername is required. 
Enter the valid Email id. 

Dismiss 

這是我的代碼:

btninsert = (Button)findViewById(R.id.new_customer); 
    btninsert.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      EditText Firstname = (EditText) findViewById(R.id.tf_userName); 
      EditText Lastname = (EditText) findViewById(R.id.tf_password); 
      EditText Baddress = (EditText) findViewById(R.id.tf_address); 
      if(Firstname.getText().toString().length() == 0) 
       Firstname.setError("Firstname is required!"); 
      else if(Lastname.getText().toString().length() == 0) 
       Lastname.setError("Lastname is required!"); 
      else if(Baddress.getText().toString().length() == 0) 
       Baddress.setError("Address is required!"); 
       else 
         insertValues(); 
         } 
         }); 

我如何可以驗證。

回答

1

更改當前的代碼在對話框上的EditText顯示驗證消息輸入驗證:

首先創建一個方法用於顯示警報爲:

public void showAlertbox(String erroMessage){ 
AlertDialog alertDialog = new AlertDialog.Builder(
       YOur_Current_Activity.this).create(); 

     // Setting Dialog Title 
    alertDialog.setTitle("Error Message"); 

    // Setting Dialog Message 
    alertDialog.setMessage(erroMessage); 


    // Setting OK Button 
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
       // Write your code here to execute after dialog closed 

     } 
     }); 

    // Showing Alert Message 
alertDialog.show(); 

} 

做的方法來檢查字符串驗證:

public static boolean notEmpty(String s) { 
return (s != null && s.length() > 0); 
} 

呼叫按鈕,點擊此方法爲:

btninsert.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 

    if(notEmpty(Firstname.getText().toString())){ 
      if(notEmpty(Lastname.getText().toString())){ 
       if(notEmpty(Baddress.getText().toString())){ 

        // instert here 
        insertValues(); 
       }else{ 
        showAlertbox("Baddress Name Empty!!"); 

      }else{ 
       showAlertbox("Lastname Name Empty!!"); 
      } 
     } 
    else{ 
     showAlertbox("First Name Empty!!"); 
     } 

    } 
//...your code here 
0
EditText name; 
boolean valid = false; 

public void showDialog() { 

    final Dialog dialog= new Dialog(Activity.this); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.custom_dialog); 
    name= (EditText) dialog.findViewById(R.id.et_name); 
    Button btnSubmit = (Button) dialog.findViewById(R.id.btn_submit); 
    Button btnCancel = (Button) dialog.findViewById(R.id.btn_cancel); 

    btnSubmit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      //validate the edit text 
      if(validateName()) { 
        // add your code here 
       dialog.cancel(); 
      } 
     } 
    }); 
} 

private boolean validateName() { 
    valid = true; 

    if(name.getText().toString().isEmpty()){ 
     valid =false; 
     name.setError("is empty"); 
    } 
    return valid; 
} 
+0

請在這裏添加一些解釋。 – Nick