Android中的EditText验证

时间:2012-07-18 12:21:54

标签: android android-edittext ksoap

我通过SOAP Web服务使用MySQL数据库连接开发了一个登录表单。在这里,我验证我的EditText(用户名,密码)框。这是一个基本问题,但我无法为此开发代码。请帮帮我。

我的代码是:

Button login = (Button) findViewById(R.id.btn_login);
    login.setOnClickListener(new View.OnClickListener() {

 public void onClick(View arg0) {
loginAction();

  }
 });
}

private void loginAction(){
 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    EditText userName = (EditText) findViewById(R.id.tf_userName);
    String user_Name = userName.getText().toString();
    EditText userPassword = (EditText) findViewById(R.id.tf_password);
    String user_Password = userPassword.getText().toString();

  //Pass value for userName variable of the web service
    PropertyInfo unameProp =new PropertyInfo();
    unameProp.setName("userName");//Define the variable name in the web service method
    unameProp.setValue(user_Name);//set value for userName variable
    unameProp.setType(String.class);//Define the type of the variable
    request.addProperty(unameProp);//Pass properties to the variable

  //Pass value for Password variable of the web service
    PropertyInfo passwordProp =new PropertyInfo();
    passwordProp.setName("password");
    passwordProp.setValue(user_Password);
    passwordProp.setType(String.class);
    request.addProperty(passwordProp);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try{
        androidHttpTransport.call(SOAP_ACTION, envelope);
           SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
           String status = response.toString();
           TextView result = (TextView) findViewById(R.id.tv_status);
           result.setText(response.toString());

           **if(status.equals("Success!"))
           {
               //   ADD  to save  and  read next time
                   String strUserName = userName.getText().toString().trim();
                   String strPassword = userPassword.getText().toString().trim();
                   if (null == strUserName || strUserName.length() == 0)
                               {
                       //  showToast("Enter Your Name");
                     userName.setError( "username is required!" );
                   } else if (null == strPassword || strPassword.length() == 0)
                               {
                           //      showToast("Enter Your Password");
                     userPassword.setError( "password is required!" );
                   } else
                               {
                       if (chkRememberMe.isChecked())
                                       {
                           SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
                           loginPreferences.edit().putString(USERNAME, strUserName).putString(PASSWORD, strPassword).commit();
                       } else
                                       {
                           SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
                           loginPreferences.edit().clear().commit();
                                       }
           Intent intent = new Intent(Login.this,HomePage.class);
              intent.putExtra("username",userName.getText().toString());
              startActivity(intent);
                               }
           }**
                   else
                      {
                       Intent i = new Intent(getApplicationContext(), Login.class);
                         startActivity(i);
                      }
                     }



              catch(Exception e){

              }
             }

      }

我希望如果单击该按钮,则会先验证EditText,然后再转到下一个Activity。

请帮助。

1 个答案:

答案 0 :(得分:0)

声明布尔变量 isValidated 。检查用户名或密码字段是否为空。如果其中任何一个为空,请将isValidated设置为“false”。否则,将isValidated设置为“true”

现在,在开始下一个活动之前,请检查isValidated的值。 如果是“true”,则启动新的Intent ;如果未经验证,则执行您想要的任何操作(在这种情况下,isValidated的值将为false)。

    private void loginAction(){

            boolean isUserValidated = true;
            boolean isPasswordValidated = true;

                 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                    EditText userName = (EditText) findViewById(R.id.tf_userName);
                    String user_Name = userName.getText().toString();
                    EditText userPassword = (EditText) findViewById(R.id.tf_password);
                    String user_Password = userPassword.getText().toString();

                  //Pass value for userName variable of the web service
                    PropertyInfo unameProp =new PropertyInfo();
                    unameProp.setName("userName");//Define the variable name in the web service method
                    unameProp.setValue(user_Name);//set value for userName variable
                    unameProp.setType(String.class);//Define the type of the variable
                    request.addProperty(unameProp);//Pass properties to the variable

                  //Pass value for Password variable of the web service
                    PropertyInfo passwordProp =new PropertyInfo();
                    passwordProp.setName("password");
                    passwordProp.setValue(user_Password);
                    passwordProp.setType(String.class);
                    request.addProperty(passwordProp);

                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    envelope.setOutputSoapObject(request);
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                    try{
                        androidHttpTransport.call(SOAP_ACTION, envelope);
                           SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                           String status = response.toString();
                           TextView result = (TextView) findViewById(R.id.tv_status);
                           result.setText(response.toString());

                           if(status.equals("Success!"))
                           {
                               //   ADD  to save  and  read next time
                                   String strUserName = userName.getText().toString().trim();
                                   String strPassword = userPassword.getText().toString().trim();
                                   if (null == strUserName || strUserName.length() == 0)
                                               {
                                       //  showToast("Enter Your Name");
                                     userName.setError( "username is required!" );
            isUserValidated = false;
                                   }
if (null == strPassword || strPassword.length() == 0)
                                               {
                                           //      showToast("Enter Your Password");
            isPasswordValidated = false;
                                     userPassword.setError( "password is required!" );
                                   } 
if(isUserValidated = true && isPasswordValidated = true)
                                               {

                                       if (chkRememberMe.isChecked())
                                                       {
                                           SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
                                           loginPreferences.edit().putString(USERNAME, strUserName).putString(PASSWORD, strPassword).commit();
                                       } else
                                                       {
                                           SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
                                           loginPreferences.edit().clear().commit();
                                                       }
}

            if(isUserValidated && isPasswordValidated)
            {
                           Intent intent = new Intent(Login.this,HomePage.class);
                              intent.putExtra("username",userName.getText().toString());
                              startActivity(intent);
            }
            else
        {
        \\ what ever you want to do if the data in the EditText is not validated.
        \\ Maybe restart the same intent ?    
    \\ Intent i = new Intent(getApplicationContext(), Login.class);    startActivity(i);


        }

                                               }
                           }
                                   else
                                      {
                                       Intent i = new Intent(getApplicationContext(), Login.class);
                                         startActivity(i);
                                      }
                                     }



                              catch(Exception e){

                              }
                             }

                      }