首页 > 代码库 > Android中SharePreferences的简单实现

Android中SharePreferences的简单实现

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);
            }
        });





    }
}