2013-04-24 191 views
0

我正在做一個Android應用程序的註冊。最初,只要密碼和確認密碼不一致,就會提示「密碼&確認密碼不匹配」併成功運行。但是,如果密碼&確認密碼與空白匹配,則應該顯示此語句,「不要將任何字段留空」,不幸的是它會失敗。登錄註冊爲Android

幫我檢查是否有錯誤。

btn2.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View v) 
      { 
       String username, password, cpassword, fullname, nric, address, phone, email; 
       username = tf3.getText().toString(); 
       password = tf4.getText().toString(); 
       cpassword = tf5.getText().toString(); 
       fullname = tf6.getText().toString(); 
       nric = tf7.getText().toString(); 
       address = tf8.getText().toString(); 
       phone = tf9.getText().toString(); 
       email = tf10.getText().toString(); 

       if(password != cpassword) 
       { 
        tv1.setText("Password & Confirm Password does not match."); 
       } 
       else if(username.equals("") || password.equals("") || cpassword.equals("") || fullname.equals("") || nric.equals("") || address.equals("") || phone.equals("") || email.equals("")) 
       { 
        tv1.setText("Do not leave any field empty."); 
       } 
       else 
       { 
        try 
        { 
         db.beginTransaction(); 
         db.execSQL("insert into Members values('"+username+"','"+password+"','"+fullname+"','"+nric+"','"+address+"','"+phone+"','"+email+"');"); 
         db.setTransactionSuccessful(); 
         db.endTransaction();  
        } 
        catch(Exception e) 
        { 
        } 
         tv1.setText("Register Complete."); 
       } 
      } 
     }); 

回答

0

您比較它在錯誤的方式,只是比較密碼&確認密碼如下,

if(!password.equals(cpassword)) 

在Java中,當你想比較兩個字符串變量,你需要使用字符串的equals()方法類。

 btn2.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View v) 
     { 
      String username, password, cpassword, fullname, nric, address, phone, email; 
      username = tf3.getText().toString(); 
      password = tf4.getText().toString(); 
      cpassword = tf5.getText().toString(); 
      fullname = tf6.getText().toString(); 
      nric = tf7.getText().toString(); 
      address = tf8.getText().toString(); 
      phone = tf9.getText().toString(); 
      email = tf10.getText().toString(); 

      if(!password.equals(cpassword)) // Change is here 
      { 
       tv1.setText("Password & Confirm Password does not match."); 
      } 
      else if(username.equals("") || password.equals("") || cpassword.equals("") || fullname.equals("") || nric.equals("") || address.equals("") || phone.equals("") || email.equals("")) 
      { 
       tv1.setText("Do not leave any field empty."); 
      } 
      else 
      { 
       try 
       { 
        db.beginTransaction(); 
        db.execSQL("insert into Members values('"+username+"','"+password+"','"+fullname+"','"+nric+"','"+address+"','"+phone+"','"+email+"');"); 
        db.setTransactionSuccessful(); 
        db.endTransaction();  
       } 
       catch(Exception e) 
       { 
       } 
        tv1.setText("Register Complete."); 
      } 
     } 
    }); 
+0

感謝您的幫助。 – XiAnG 2013-04-24 04:13:09

+0

不錯.......... :) – Lucifer 2013-04-24 04:14:21

0

比較密碼和確認密碼字段如下。

if(!password.equals(cpassword)){ 

    tv1.setText("Password & Confirm Password does not match."); 

} 

希望hepls ..總是使用的equals()比較兩個字符串。