-1

我通过 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。

请帮忙。

4

1 回答 1

0

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

现在,在开始下一个活动之前检查 isValidated 的值。如果为“真”,则启动新的 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){

                              }
                             }

                      }
于 2012-07-18T12:34:48.963 回答