项目完整压缩包:http://download.csdn.net/detail/iwanghang/9606408
manifests.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.iwanghang.sharedpreferencesdemo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>MainActivity.java:
package com.iwanghang.sharedpreferencesdemo; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.util.Map; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** * 任意key及对应value写入,全部key和value读取 */ final EditText editText_key = (EditText) findViewById(R.id.editText_key); final EditText editText_value = (EditText) findViewById(R.id.editText_value); Button button_write = (Button) findViewById(R.id.button_write); Button button_read_all = (Button) findViewById(R.id.button_read_all); /** * name的value写入,name的value读取 */ final EditText editText_name = (EditText) findViewById(R.id.editText_name); Button button_write_name = (Button) findViewById(R.id.button_write_name); Button button_read_name = (Button) findViewById(R.id.button_read_name); /** * age的value写入,age的value读取 */ final EditText editText_age = (EditText) findViewById(R.id.editText_age); Button button_write_age = (Button) findViewById(R.id.button_write_age); Button button_read_age = (Button) findViewById(R.id.button_read_age); /** * 内容显示 */ final TextView textView_content = (TextView) findViewById(R.id.textView_content); //1、获取SharedPreferences对象。SharedPreferences是一个接口,程序无法直接创建SharedPreferences对象,只能通过Context提供的getSharedPreferences()方法。 final SharedPreferences preference = getSharedPreferences("test", MODE_PRIVATE); //2、获取SharedPreferences。Editor对象,用于写数据。本步骤只对写数据有必要。 final SharedPreferences.Editor editor = preference.edit(); button_write.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { String key = editText_key.getText().toString().trim(); String value = editText_value.getText().toString().trim(); //3、写入数据并commit。 editor.putString(key, value); editor.commit(); } }); /** * 读取所有数据 */ button_read_all.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { String content = ""; Map<String, ?> allContent = preference.getAll(); // 注意遍历map的方法 for(Map.Entry<String, ?> entry : allContent.entrySet()){ content += ("key:" + entry.getKey()+ "=" +entry.getValue() + ", "); } textView_content.setText(content); } }); /** * name写入 */ button_write_name.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { String key = "name"; String value = editText_name.getText().toString().trim(); //3、写入数据并commit。 editor.putString(key, value); editor.commit(); } }); /** * name读取 */ button_read_name.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { String content = preference.getString("name", "null"); textView_content.setText(content); } }); /** * age写入 */ button_write_age.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { String key = "age"; String value = editText_age.getText().toString().trim(); //3、写入数据并commit。 editor.putString(key, value); editor.commit(); } }); /** * age读取 */ button_read_age.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { String content = preference.getString("age", "17"); textView_content.setText(content); } }); } }activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 写入任意key --> <LinearLayout android:id="@+id/write_key_layout" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/insert_text_setOut" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入任意key:" android:textSize="20dp" /> <EditText android:id="@+id/editText_key" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <!-- 写入任意value --> <LinearLayout android:id="@+id/write_value_layout" android:layout_below="@+id/write_key_layout" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/insert_text_setOut2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入对应value:" android:textSize="20dp" /> <EditText android:id="@+id/editText_value" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <!-- 写入读取按钮 --> <RelativeLayout android:id="@+id/write_read_layout" android:layout_below="@+id/write_value_layout" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button_write" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="key写入"/> <Button android:id="@+id/button_read_all" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全部读取"/> </RelativeLayout> <!-- name --> <LinearLayout android:id="@+id/write_name_layout" android:layout_below="@+id/write_read_layout" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/insert_text_setOut3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入name的value:" android:textSize="20dp" /> <EditText android:id="@+id/editText_name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <!-- name写入读取按钮 --> <RelativeLayout android:id="@+id/name_write_read_layout" android:layout_below="@+id/write_name_layout" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button_write_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name写入"/> <Button android:id="@+id/button_read_name" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name读取"/> </RelativeLayout> <!-- age --> <LinearLayout android:id="@+id/write_age_layout" android:layout_below="@+id/name_write_read_layout" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/insert_text_setOut4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入age的value:" android:textSize="20dp" /> <EditText android:id="@+id/editText_age" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <!-- name写入读取按钮 --> <RelativeLayout android:id="@+id/age_write_read_layout" android:layout_below="@+id/write_age_layout" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button_write_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="age写入"/> <Button android:id="@+id/button_read_age" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="age读取"/> </RelativeLayout> <TextView android:id="@+id/textView_content" android:layout_below="@id/age_write_read_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取内容显示" /> </RelativeLayout>