Advertisement
Guest User

leoleohehehe

a guest
Oct 24th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. package com.panares.le10242017;
  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. public class MainActivity extends AppCompatActivity {
  14.  
  15. EditText et_username;
  16. EditText et_password;
  17. Button btn_save;
  18. Button btn_display;
  19. TextView tv_display;
  20. SharedPreferences preferences;
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. et_username = (EditText) findViewById(R.id.editText2);
  27. et_password = (EditText) findViewById(R.id.editText3);
  28. btn_save = (Button) findViewById(R.id.button);
  29. btn_display = (Button) findViewById(R.id.button2);
  30. tv_display = (TextView) findViewById(R.id.textView3);
  31. preferences = (getPreferences(Context.MODE_PRIVATE));
  32.  
  33. }
  34.  
  35. public void saveInfo(View view){
  36. SharedPreferences.Editor editor = preferences.edit();
  37. editor.putString("username", et_username.getText().toString());
  38. editor.putString("password", et_password.getText().toString());
  39. editor.commit();
  40. Toast.makeText(this, "Data Saved!", Toast.LENGTH_SHORT).show();
  41.  
  42. }
  43.  
  44. public void loadInfo(View view){
  45. String user = preferences.getString("username","");
  46. String pwd = preferences.getString("password","");
  47. tv_display.setText("The password of " + user + " is " + pwd);
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement