Advertisement
oim_trust

MainActivitySharedPreferences

Dec 30th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package com.rohim.otrust.appsharedpref;
  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.     private EditText textName, textPhone, textEmail;
  16.     private TextView textOut;
  17.     private Button saveBtn;
  18.  
  19.     public static final String MyPREFERENCES = "MyPrefs" ;
  20.     public static final String Name = "nameKey";
  21.     public static final String Phone = "phoneKey";
  22.     public static final String Email = "emailKey";
  23.  
  24.     SharedPreferences sharedPreferences;
  25.  
  26.     @Override
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.activity_main);
  30.  
  31.         textName    = (EditText) findViewById(R.id.et_name);
  32.         textPhone   = (EditText) findViewById(R.id.et_phonet);
  33.         textEmail   = (EditText) findViewById(R.id.et_emailt);
  34.         saveBtn     = (Button)   findViewById(R.id.btn_save);
  35.         textOut     = (TextView) findViewById(R.id.tv_output);
  36.  
  37.         sharedPreferences   = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
  38.  
  39.         saveBtn.setOnClickListener(new View.OnClickListener() {
  40.             @Override
  41.             public void onClick(View v) {
  42.                 String show     = "";
  43.  
  44.                 String strName  = textName.getText().toString();
  45.                 String strPhone = textPhone.getText().toString();
  46.                 String strEmail = textEmail.getText().toString();
  47.  
  48.                 SharedPreferences.Editor editor = sharedPreferences.edit();
  49.  
  50.                 editor.putString(Name, strName);
  51.                 editor.putString(Phone, strPhone);
  52.                 editor.putString(Email, strEmail);
  53.  
  54.                 show    = "Your Data : \n";
  55.                 show    += strName + "\n" + strPhone + "\n" + strEmail;
  56.  
  57.                 Toast.makeText(MainActivity.this, "Successfully Saved", Toast.LENGTH_LONG).show();
  58.                 textOut.setText(show);
  59.  
  60.             }
  61.         });
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement