Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.patrick.mysharedpref;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.w3c.dom.Text;
- public class MainActivity extends AppCompatActivity {
- EditText et_username;
- EditText et_password;
- Button btn_save;
- Button btn_display;
- TextView tv_display;
- SharedPreferences preferences;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- et_username = (EditText) findViewById(R.id.eTusername);
- et_password = (EditText) findViewById(R.id.eTpassword);
- btn_save = (Button) findViewById(R.id.saveBtn);
- btn_display = (Button) findViewById(R.id.displayBtn);
- tv_display = (TextView) findViewById(R.id.tVdisplay);
- preferences = (getPreferences(Context.MODE_PRIVATE));
- }
- public void saveInfo(View view){
- SharedPreferences.Editor editor = preferences.edit();
- editor.putString("username", et_username.getText().toString());
- editor.putString("password", et_password.getText().toString());
- editor.commit();
- Toast.makeText(this, "Data Saved!", Toast.LENGTH_SHORT).show();
- }
- public void loadInfo(View view){
- String user = preferences.getString("username","");
- String pwd = preferences.getString("password","");
- tv_display.setText("The password of " + user + " is " + pwd);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement