10 January 2017

Android: SharedPreferences Save and Load Data

MainActivity.java
Java Android 2017
package com.example.lonely.savefile;

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.Toast;

public class MainActivity extends AppCompatActivity {
    Button SAVE, LOAD;
    EditText NAME, MOB;
    SharedPreferences prf;
    String Name, Mob;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Button click
        SAVE = (Button) findViewById(R.id.save);
        LOAD = (Button) findViewById(R.id.load);
        //EditText
        NAME = (EditText) findViewById(R.id.name);
        MOB = (EditText) findViewById(R.id.mob);
        //Set OnClick Save vào SharedFreferences
        SAVE.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Name = NAME.getText().toString();
                Mob = MOB.getText().toString();
                prf = getSharedPreferences("my_details", MODE_PRIVATE);
                SharedPreferences.Editor edit = prf.edit();
                edit.putString("key_name", Name);
                edit.putString("key_mob", Mob);
                edit.commit();
                Toast.makeText(getBaseContext(), "Data Succeessfully saved", Toast.LENGTH_LONG).show();
            }
        });
        //Set onClick đọc từ SharedFreferences
        LOAD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                prf = getSharedPreferences("my_details", MODE_PRIVATE);
                String getName = prf.getString("key_name", "");
                String getMob = prf.getString("key_mob", "");
                Toast.makeText(getBaseContext(), "Name " + getName + " Mob : " + getMob, Toast.LENGTH_LONG).show();
            }
        });
    }
}
activity_main.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lonely.savefile.MainActivity">

    <EditText
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="29dp"
        android:ems="10"
        android:gravity="center"
        android:hint="Enter Name"/>
    <EditText
        android:id="@+id/mob"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name"
        android:layout_below="@+id/name"
        android:layout_marginTop="28dp"
        android:ems="10"
        android:gravity="center"
        android:hint="Enter Mob" />

    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mob"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="SAVE DATA" />

    <Button
        android:id="@+id/load"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/save"
        android:layout_below="@+id/save"
        android:layout_marginTop="24dp"
        android:text="LOAD DATA" />
</RelativeLayout>

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang