Advertisement
Guest User

Untitled

a guest
Apr 1st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. import android.content.Intent;
  2. import android.content.SharedPreferences;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.CheckBox;
  7. import android.widget.EditText;
  8. import android.widget.TextView;
  9.  
  10. public class LoginActivity extends AppCompatActivity {
  11. public static final String PREF_FILE_NAME = "loginPref";
  12. public static final String USERNAME = "USERNAME";
  13. public static final String PASSWORD = "PASSWORD";
  14. private SharedPreferences preferences;
  15. private EditText usernameField;
  16. private EditText passwordField;
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_login);
  22.  
  23. preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
  24. usernameField = findViewById(R.id.login_username_edittext);
  25. passwordField = findViewById(R.id.login_password_edittext);
  26. String username = preferences.getString(USERNAME, null);
  27. String password = preferences.getString(PASSWORD, null);
  28. if (username != null && password != null) {
  29. usernameField.setText(username);
  30. passwordField.setText(password);
  31. }
  32. }
  33.  
  34. public void loginButtonClicked(View view) {
  35. String username = usernameField.getText().toString();
  36. String password = passwordField.getText().toString();
  37. boolean ok = PasswordChecker.Check(username, password);
  38. if (ok) {
  39. CheckBox checkBox = findViewById(R.id.login_remember_checkbox);
  40. SharedPreferences.Editor editor = preferences.edit();
  41. if (checkBox.isChecked()) {
  42. editor.putString(USERNAME, username);
  43. editor.putString(PASSWORD, password);
  44. } else {
  45. editor.remove(USERNAME);
  46. editor.remove(PASSWORD);
  47. }
  48. editor.apply();
  49. Intent intent = new Intent(this, MainActivity.class);
  50. intent.putExtra(USERNAME, username);
  51. startActivity(intent);
  52. } else {
  53. usernameField.setError("Wrong username or password");
  54. TextView messageView = findViewById(R.id.login_message_textview);
  55. messageView.setText("Username + password not valid");
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement