【问题标题】:Android edittext validation like dialog boxAndroid edittext 验证类似对话框
【发布时间】:2012-12-05 12:50:45
【问题描述】:

按下按钮时的Android输入验证并在对话框中显示验证消息,如下所示。

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

如何验证。

【问题讨论】:

    标签: android android-edittext android-alertdialog


    【解决方案1】:

    将当前代码更改为 EditText 上的输入验证在对话框中显示验证消息:

    首先创建一个显示 Alert 的方法:

    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
    

    【讨论】:

      【解决方案2】:

      我们可以在如下所示的对话框中进行编辑文本验证。根据您的要求确定范围。

      private  EditText name;
      private  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;
      }
      

      【讨论】:

      • 请在此处添加一些解释。
      猜你喜欢
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      相关资源
      最近更新 更多