Android中SharePreferences的簡單實現

weixin_30488085發表於2020-04-06

Android中提供SharePreferences這種輕量級的資料儲存模式,這種模式能夠儲存少量資料,並能為自身和其他應用提供資料介面。相對於其他資料儲存方式,SharePreferences更加輕量。以下是整個SharePreferences實現的程式碼:

xml佈局檔案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText" android:width="200dp"/>
    <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/editText2"/>
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="儲存"
            android:id="@+id/button"/>
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView"/>
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView2"/>
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顯示"
            android:id="@+id/button2"/>
</LinearLayout>

 

java主檔案:

package com.example.sharepreferencesTest;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    private EditText user;
    private EditText address;
    private Button login;
    private Button show;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        user = (EditText)findViewById(R.id.editText);
        address = (EditText)findViewById(R.id.editText2);
        login = (Button)findViewById(R.id.button);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //寫入SharedPreferences中
                SharedPreferences user_ino = getSharedPreferences("user_ino", Context.MODE_PRIVATE);  //使用MODE_PRIVATE模式
                SharedPreferences.Editor editor = user_ino.edit();

                String ename = user.getText().toString();
                String eaddress = address.getText().toString();

                editor.putString("name",ename);
                editor.putString("address", eaddress);
                editor.commit();
            }
        });

        show = (Button)findViewById(R.id.button2);
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //讀入SharedPreferences中的值
                SharedPreferences user_ino2 = getSharedPreferences("user_ino", Context.MODE_PRIVATE);
                String name1 = user_ino2.getString("name","");
                String age1 = user_ino2.getString("address","");

                //顯示出讀出的值
                TextView tv1 = (TextView)findViewById(R.id.textView);
                TextView tv2 = (TextView)findViewById(R.id.textView2);
                tv1.setText(name1);
                tv2.setText(age1);
            }
        });





    }
}

 

轉載於:https://www.cnblogs.com/starwolf/p/3739327.html

相關文章