2017-07-22 37 views
0

這是我試圖創建用戶輸入用戶名,emailID和密碼的signUp活動。發生的問題是onClick()只能工作一次。假設密碼和confirmPassword不匹配,那麼textlayout顯示消息,但如果它再次不匹配,則什麼都不會發生。請幫我解決我錯誤的地方。onClick只工作一次

public class SignUp extends AppCompatActivity { 

// will send the data into database and return to the login page with a confirm signup message 

private final AppCompatActivity activity = SignUp.this; 
TextInputEditText usernameText,emailIdText,passwordText,confirmPasswordText; 
NestedScrollView nestedScrollView; 
TextInputLayout usernameTextLayout, emailIdTextLayout, passwordTextLayout, confirmPasswordTextLayout; 
DatabaseManager manager ; 
ErrorsHelper helper; 
Students student; 


@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.sign_up); 
    initViews(); 
    initObjects(); 
} 

//views initializer 
public void initViews(){ 

    nestedScrollView = (NestedScrollView)findViewById(R.id.signUpNestedScrollView); 

    // Text Input Layout views 
    usernameTextLayout = (TextInputLayout) findViewById(R.id.usernameTextLayout); 
    emailIdTextLayout = (TextInputLayout) findViewById(R.id.emailIdTextLayout); 
    passwordTextLayout = (TextInputLayout) findViewById(R.id.passwordTextLayout); 
    confirmPasswordTextLayout = (TextInputLayout) findViewById(R.id.confirmPasswordTextLayout); 

    // Text Input views 
    usernameText = (TextInputEditText)findViewById(R.id.usernameText); 
    emailIdText = (TextInputEditText)findViewById(R.id.emailIdText); 
    passwordText = (TextInputEditText)findViewById(R.id.passwordText); 
    confirmPasswordText = (TextInputEditText)findViewById(R.id.confirmPasswordText); 
} 

// objects initializer 
public void initObjects(){ 
    helper = new ErrorsHelper(activity); 
    manager = new DatabaseManager(activity); 
    student = new Students(); 

} 

//Register Button 
public void onClick(View view){ 

    if(!helper.emptyTextField(usernameText,usernameTextLayout,"Please enter username.")){ 
     return; 
    } 
    if(!helper.emptyTextField(emailIdText,emailIdTextLayout,"Please enter email-id.")){ 
     return; 
    } 
    if(!helper.emptyTextField(passwordText,passwordTextLayout,"Please enter password.")){ 
     return; 
    } 
    if(!helper.emptyTextField(confirmPasswordText,confirmPasswordTextLayout,"Please enter your password to confirm.")){ 
     return; 
    } 
    if(!helper.passwordConfirm(passwordText,confirmPasswordText,confirmPasswordTextLayout,"Confirm password doesn't match!")){ 
     return; 
    } 
    String username = usernameText.getText().toString(); 
    String emailId = emailIdText.getText().toString(); 
    String confirmPassword = confirmPasswordText.getText().toString(); 
    if (manager.checkUsernameExistence(username)) { 
     student.set_username(username); 
     student.set_emailid(emailId); 
     student.set_password(confirmPassword); 
     //add student 
     manager.addStudent(student); 

     Snackbar.make(nestedScrollView,"Registration Successful.",Snackbar.LENGTH_LONG).show(); 
     Intent intent = new Intent(this,SignInMainPage.class); 
     startActivity(intent); 
    } 
    else{ 
     helper.userExists(usernameTextLayout,"Username already exists."); 
    } 
} 
} 

ErrorHelper類有助於在一個位置維護所有基於錯誤的事物。此外,Snackbar沒有顯示出來。

public class ErrorsHelper { 

Context context; 

public ErrorsHelper(Context context) { 
    this.context = context; 
} 

public boolean emptyTextField(TextInputEditText editText, TextInputLayout inputLayout, String errorMessage) { 
    String value = editText.getText().toString().trim(); 
    if (value.isEmpty()) { 
     inputLayout.setError(errorMessage); 
     hideKeyboardFrom(editText); 
     return false; 
    } else { 
     inputLayout.setErrorEnabled(false); 
    } 

    return true; 
} 

; 

public boolean passwordConfirm(TextInputEditText password, TextInputEditText confirmPassword, TextInputLayout confirmPasswordLayout, String errorMessage) { 

    String pass = password.getText().toString(); 
    String cPass = confirmPassword.getText().toString(); 
    if (pass != cPass) { 
     confirmPasswordLayout.setError(errorMessage); 
     return false; 
    } else { 
     confirmPasswordLayout.setErrorEnabled(false); 
    } 
    return true; 
} 

private void hideKeyboardFrom(View view) { 
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(view.getWindowToken(), WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
} 

public void userExists(TextInputLayout confirmPasswordLayout, String errorMessage){ 
    confirmPasswordLayout.setError(errorMessage); 
} 

}

這是我的註冊頁面的XML佈局文件

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/signUpNestedScrollView" 
android:paddingTop="100dp"> 

<LinearLayout 
    android:id="@+id/signUpLinearLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

<android.support.design.widget.TextInputLayout 
    android:id="@+id/usernameTextLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center"> 
<android.support.design.widget.TextInputEditText 
    android:id="@+id/usernameText" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="100dp" 
    android:hint="Username here" 
    android:fontFamily="casual"/> 
</android.support.design.widget.TextInputLayout> 

<android.support.design.widget.TextInputLayout 
    android:id="@+id/emailIdTextLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center"> 

<android.support.design.widget.TextInputEditText 
    android:id="@+id/emailIdText" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="20dp" 
    android:hint="Email-ID here" 
    android:fontFamily="casual"/> 
</android.support.design.widget.TextInputLayout> 

<android.support.design.widget.TextInputLayout 
    android:id="@+id/passwordTextLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center"> 

<android.support.design.widget.TextInputEditText 
    android:id="@+id/passwordText" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="20dp" 
    android:hint="Password" 
    android:inputType="textPassword" 
    android:fontFamily="casual"/> 
</android.support.design.widget.TextInputLayout> 

<android.support.design.widget.TextInputLayout 
    android:id="@+id/confirmPasswordTextLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center"> 

<android.support.design.widget.TextInputEditText 
    android:id="@+id/confirmPasswordText" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="20dp" 
    android:hint="Confirm Password" 
    android:inputType="textPassword" 
    android:fontFamily="casual"/> 
</android.support.design.widget.TextInputLayout> 

<Button 
    android:id="@+id/signupButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="20dp" 
    android:fontFamily="casual" 
    android:onClick="onClick" 
    android:text="Sign-Up" 
    android:textStyle="bold" 
    android:layout_gravity="center"/> 

</LinearLayout> 
</android.support.v4.widget.NestedScrollView> 
+1

你怎麼加的onclick監聽器按鈕,我沒有看到在代碼 –

+0

對不起,我忘了添加我的XML佈局文件,我已經通過XML聲明onClick – Veloxigami

回答

0
public void onClick(View view){ 

     // they should be here 
      String username = usernameText.getText().toString(); 
      String emailId = emailIdText.getText().toString(); 
      String confirmPassword = confirmPasswordText.getText().toString(); 

     if(!username.isEmpty()&&!emailId.isEmpty()&&!confirmPassword.isEmpty()) 
     {  
      if(!helper.emptyTextField(usernameText,usernameTextLayout,"Please enter username.")){ 
       return; 
      } 
      if(!helper.emptyTextField(emailIdText,emailIdTextLayout,"Please enter email-id.")){ 
       return; 
      } 
      if(!helper.emptyTextField(passwordText,passwordTextLayout,"Please enter password.")){ 
       return; 
      } 
      if(!helper.emptyTextField(confirmPasswordText,confirmPasswordTextLayout,"Please enter your password to confirm.")){ 
       return; 
      } 
      if(!helper.passwordConfirm(passwordText,confirmPasswordText,confirmPasswordTextLayout,"Confirm password doesn't match!")){ 
       return; 
      } 

      if (manager.checkUsernameExistence(username)) { 
       student.set_username(username); 
       student.set_emailid(emailId); 
       student.set_password(confirmPassword); 
       //add student 
       manager.addStudent(student); 


       //Snackbar.make(findViewById(android.R.id.content),"Registration Successful.",Snackbar.LENGTH_LONG).show(); 
       Snackbar s = Snackbar.make(findViewById(android.R.id.content),"Registration Successful.",Snackbar.LENGTH_LONG); 
       s.show(); 
       Intent intent = new Intent(this,SignInMainPage.class); 
       startActivity(intent); 
      } 
      else{ 
       helper.userExists(usernameTextLayout,"Username already exists."); 
      } 
     } 
     else 
      Toast.makeText(getApplicationContext,"Empty",Toast.LENGTH_LONG).show; 

    } 
+0

它仍然無法正常工作 – Veloxigami

+0

手動使用ID轉換按鈕然後button.setOnClickListener .... – Salman500