Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. package com.mdp.tugas;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.inputmethod.EditorInfo;
  8. import android.widget.Button;
  9. import android.widget.CheckBox;
  10. import android.widget.CompoundButton;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15.  
  16. private EditText edtUsername, edtPassword;
  17. private Data dataUser;
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. dataUser = new Data();
  24. AddEventCheckBoxShowPwd();
  25. AddEventRegisterButton();
  26. AddEventLoginButton();
  27. }
  28.  
  29. private void AddEventLoginButton() {
  30. Button btnLogin = (Button) findViewById(R.id.btnLogin);
  31. btnLogin.setOnClickListener(new View.OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. edtUsername = findViewById(R.id.edtUsername);
  35. edtPassword = findViewById(R.id.edtPassword);
  36. String u = edtUsername.getText().toString().trim();
  37. String p = edtPassword.getText().toString().trim();
  38. if (dataUser.isValidLogin(u, p)) {
  39. Intent i = new Intent(getApplicationContext(), HomeActivity.class);
  40. i.putExtra("username", u);
  41. Toast.makeText(getApplicationContext(), "Login berhasil", Toast.LENGTH_SHORT).show();
  42. } else {
  43. Toast.makeText(getApplicationContext(), "Username atau Password" +
  44. "tidak terdaftar", Toast.LENGTH_SHORT).show();
  45. }
  46. }
  47. });
  48. }
  49.  
  50. private void AddEventRegisterButton() {
  51. Button btnRegister = (Button) findViewById(R.id.btnRegister);
  52. btnRegister.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. edtUsername = (EditText) findViewById(R.id.edtUsername);
  56. edtPassword = (EditText) findViewById(R.id.edtPassword);
  57. if (!dataUser.cekUsername(edtUsername.getText().toString())) {
  58. dataUser.addData(edtUsername.getText().toString(), edtPassword.getText().toString());
  59. Toast.makeText(getApplicationContext(), "Register Berhasil", Toast.LENGTH_SHORT).show();
  60. } else {
  61. Toast.makeText(getApplicationContext(), "Username sudah terdaftar", Toast.LENGTH_SHORT).show();
  62. }
  63. }
  64. });
  65. }
  66.  
  67. private void AddEventCheckBoxShowPwd() {
  68. final CheckBox chkShowPwd = (CheckBox) findViewById(R.id.chkShowPwd);
  69. chkShowPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  70. @Override
  71. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  72. if (!isChecked) {
  73. chkShowPwd.setChecked(false);
  74. edtPassword.setInputType(EditorInfo.TYPE_CLASS_TEXT);
  75. } else {
  76. chkShowPwd.setChecked(true);
  77. edtPassword.setInputType(EditorInfo.TYPE_CLASS_TEXT |
  78. EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
  79. }
  80. }
  81. });
  82. }
  83. }
  84. package com.mdp.tugas;
  85.  
  86. import java.util.ArrayList;
  87.  
  88. /**
  89. * Created by user on 4/3/18.
  90. */
  91.  
  92. public class Data {
  93. private ArrayList<String> username;
  94. private ArrayList<String> password;
  95. Data(){
  96. username = new ArrayList<>();
  97. password = new ArrayList<>();
  98. }
  99. public void addData(String u,String p){
  100. username.add(u);
  101. password.add(p);
  102. }
  103. public boolean isValidLogin(String u,String p){
  104. Boolean cek = false;
  105. for(int i=0;i<username.size();i++){
  106. if(username.get(i).equalsIgnoreCase(u) && password.get(i).equalsIgnoreCase(p)){
  107. cek = true;
  108. }
  109. }
  110. return cek;
  111. }
  112. public boolean cekUsername(String u){
  113. Boolean cek = false;
  114. for(int i=0;i<username.size();i++){
  115. if(username.get(i).equalsIgnoreCase(u)){
  116. cek = true;
  117. }
  118. }
  119. return cek;
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement