Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.rohim.otrust.appsharedpref;
- 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;
- public class MainActivity extends AppCompatActivity {
- private EditText textName, textPhone, textEmail;
- private TextView textOut;
- private Button saveBtn;
- public static final String MyPREFERENCES = "MyPrefs" ;
- public static final String Name = "nameKey";
- public static final String Phone = "phoneKey";
- public static final String Email = "emailKey";
- SharedPreferences sharedPreferences;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textName = (EditText) findViewById(R.id.et_name);
- textPhone = (EditText) findViewById(R.id.et_phonet);
- textEmail = (EditText) findViewById(R.id.et_emailt);
- saveBtn = (Button) findViewById(R.id.btn_save);
- textOut = (TextView) findViewById(R.id.tv_output);
- sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
- saveBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String show = "";
- String strName = textName.getText().toString();
- String strPhone = textPhone.getText().toString();
- String strEmail = textEmail.getText().toString();
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putString(Name, strName);
- editor.putString(Phone, strPhone);
- editor.putString(Email, strEmail);
- show = "Your Data : \n";
- show += strName + "\n" + strPhone + "\n" + strEmail;
- Toast.makeText(MainActivity.this, "Successfully Saved", Toast.LENGTH_LONG).show();
- textOut.setText(show);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement