Advertisement
Guest User

for clark/dianne

a guest
Oct 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. package com.patrick.mysharedpref;
  2.  
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import org.w3c.dom.Text;
  14.  
  15. public class MainActivity extends AppCompatActivity {
  16.  
  17. EditText et_username;
  18. EditText et_password;
  19. Button btn_save;
  20. Button btn_display;
  21. TextView tv_display;
  22. SharedPreferences preferences;
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28. et_username = (EditText) findViewById(R.id.eTusername);
  29. et_password = (EditText) findViewById(R.id.eTpassword);
  30. btn_save = (Button) findViewById(R.id.saveBtn);
  31. btn_display = (Button) findViewById(R.id.displayBtn);
  32. tv_display = (TextView) findViewById(R.id.tVdisplay);
  33. preferences = (getPreferences(Context.MODE_PRIVATE));
  34.  
  35. }
  36.  
  37. public void saveInfo(View view){
  38. SharedPreferences.Editor editor = preferences.edit();
  39. editor.putString("username", et_username.getText().toString());
  40. editor.putString("password", et_password.getText().toString());
  41. editor.commit();
  42. Toast.makeText(this, "Data Saved!", Toast.LENGTH_SHORT).show();
  43.  
  44. }
  45.  
  46. public void loadInfo(View view){
  47. String user = preferences.getString("username","");
  48. String pwd = preferences.getString("password","");
  49. tv_display.setText("The password of " + user + " is " + pwd);
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement