Guest User

Untitled

a guest
Jun 28th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. package farenza.tutorial.simplelogin;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.text.TextUtils;
  6. import android.view.KeyEvent;
  7. import android.view.View;
  8. import android.view.inputmethod.EditorInfo;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11.  
  12. public class RegisterActivity extends AppCompatActivity {
  13.  
  14. private EditText vuser,vpassword, vrepassword;
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_register);
  20.  
  21. vuser=findViewById(R.id.et_emailSignup);
  22. vpassword=findViewById(R.id.et_passwordSignup);
  23. vrepassword=findViewById(R.id.et_passwordSignup2);
  24. vrepassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  25. @Override
  26. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  27. if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_NULL) {
  28. razia();
  29. return true;
  30. }
  31. return false;
  32. }
  33. });
  34. findViewById(R.id.button_signupSignup).setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. razia();
  38. }
  39. });
  40. }
  41.  
  42. private void razia(){
  43. //Mereset semua Error dan fokus menjadi default
  44. vuser.setError(null);
  45. vpassword.setError(null);
  46. vrepassword.setError(null);
  47. View fokus = null;
  48. boolean cancel = false;
  49.  
  50. //Mengambil String dari EditText
  51. String repassword = vrepassword.getText().toString();
  52. String user = vuser.getText().toString();
  53. String password = vpassword.getText().toString();
  54.  
  55. if (TextUtils.isEmpty(user)){
  56. //Jika form Username kosong
  57. vuser.setError("This field is required");
  58. fokus = vuser;
  59. cancel = true;
  60. }else if(cekUser(user)){
  61. //Jika nama User sama dengan nama User yang sudah Ter registrasi sebelumnya
  62. vuser.setError("This Username is already exist");
  63. fokus = vuser;
  64. cancel = true;
  65. }
  66.  
  67. if (TextUtils.isEmpty(password)){
  68. //Jika form password kosong
  69. vpassword.setError("This field is required");
  70. fokus = vpassword;
  71. cancel = true;
  72. }else if (!cekPassword(password,repassword)){
  73. //Jika password tidak sama antara Confirm Password dengan Password
  74. vrepassword.setError("This password is incorrect");
  75. fokus = vrepassword;
  76. cancel = true;
  77. }
  78.  
  79. if (cancel){
  80. //Memindahkan Fokus ke View yang ada Errornya
  81. fokus.requestFocus();
  82. }else{
  83. //Mengubah data yang sebelumnya sudah ter registrasi menjadi baru
  84. Preferences.setKey_user(getBaseContext(),user);
  85. Preferences.setKey_pass(getBaseContext(),password);
  86. finish();
  87. }
  88. }
  89.  
  90. //cek User dengan Data yang sebelumnya sudah ter registrasi
  91. //cek Password dengan Confirm Password sama atau tidak
  92. private boolean cekPassword(String password, String repassword){
  93. return password.equals(repassword);
  94. }
  95. private boolean cekUser(String user){
  96. return user.equals(Preferences.getKey_user(getBaseContext()));
  97. }
  98. }
Add Comment
Please, Sign In to add comment