-1

SOAP Web サービスを介して MySQL データベース接続を使用するログイン フォームを開発しました。ここで、EditText(username,password) ボックスを検証します。これは基本的な質問ですが、このコードを開発することはできません。お願い助けて。

私のコードは次のとおりです。

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 が最初に検証されてから、次のアクティビティにのみ移動することを望みます。

助けてください。

4

1 に答える 1

0

ブール変数isValidatedを宣言します。ユーザー名またはパスワードフィールドが空かどうかを確認します。それらのいずれかが空の場合は、isValidatedを「false」に設定します。それ以外の場合は、isValidatedを「true」に設定します。

ここで、次のアクティビティを開始する前に、isValidatedの値を確認してください。「true」の場合は、新しいインテントを開始します。それ以外の場合は、検証されていない場合は必要な処理を行います(その場合、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 に答える