Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package farenza.tutorial.simplelogin;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.inputmethod.EditorInfo;
- import android.widget.EditText;
- import android.widget.TextView;
- public class RegisterActivity extends AppCompatActivity {
- private EditText vuser,vpassword, vrepassword;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- vuser=findViewById(R.id.et_emailSignup);
- vpassword=findViewById(R.id.et_passwordSignup);
- vrepassword=findViewById(R.id.et_passwordSignup2);
- vrepassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
- @Override
- public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
- if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_NULL) {
- razia();
- return true;
- }
- return false;
- }
- });
- findViewById(R.id.button_signupSignup).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- razia();
- }
- });
- }
- private void razia(){
- //Mereset semua Error dan fokus menjadi default
- vuser.setError(null);
- vpassword.setError(null);
- vrepassword.setError(null);
- View fokus = null;
- boolean cancel = false;
- //Mengambil String dari EditText
- String repassword = vrepassword.getText().toString();
- String user = vuser.getText().toString();
- String password = vpassword.getText().toString();
- if (TextUtils.isEmpty(user)){
- //Jika form Username kosong
- vuser.setError("This field is required");
- fokus = vuser;
- cancel = true;
- }else if(cekUser(user)){
- //Jika nama User sama dengan nama User yang sudah Ter registrasi sebelumnya
- vuser.setError("This Username is already exist");
- fokus = vuser;
- cancel = true;
- }
- if (TextUtils.isEmpty(password)){
- //Jika form password kosong
- vpassword.setError("This field is required");
- fokus = vpassword;
- cancel = true;
- }else if (!cekPassword(password,repassword)){
- //Jika password tidak sama antara Confirm Password dengan Password
- vrepassword.setError("This password is incorrect");
- fokus = vrepassword;
- cancel = true;
- }
- if (cancel){
- //Memindahkan Fokus ke View yang ada Errornya
- fokus.requestFocus();
- }else{
- //Mengubah data yang sebelumnya sudah ter registrasi menjadi baru
- Preferences.setKey_user(getBaseContext(),user);
- Preferences.setKey_pass(getBaseContext(),password);
- finish();
- }
- }
- //cek User dengan Data yang sebelumnya sudah ter registrasi
- //cek Password dengan Confirm Password sama atau tidak
- private boolean cekPassword(String password, String repassword){
- return password.equals(repassword);
- }
- private boolean cekUser(String user){
- return user.equals(Preferences.getKey_user(getBaseContext()));
- }
- }
Add Comment
Please, Sign In to add comment