dd

android中SharedPreferences的简单例子

jerry Android 2015年11月23日 收藏

1:界面布局

<?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"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/name"
    />
<EditText
    android:id="@+id/name"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/age"
    />
<EditText
    android:id="@+id/age"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:numeric="integer"
    />

<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button"
   />
<Button
    android:id="@+id/resume"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/resume"
   />
</LinearLayout>
</LinearLayout>

2:用到的字符串资源strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">软件参数保存</string>
    <string name="name">网名</string>
    <string name="age">年龄</string>
    <string name="button">保存参数</string>
    <string name="success">参数保存成功</string>
    <string name="resume">恢复参数</string>
</resources>

3:界面如下图所示

程序界面如上图所示,用户在网名和年龄的文本框中输入相应的值,点击保存参数按钮,然后参数值就会保存到sharedpreferecs中,当两个EditText中没有值时,点击

恢复参数按钮会从sharedpreferences文件中读取name和age的值并显示在相应的控件上

4:MainActivity的内容

package cn.itcast.sharedpreferences;

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

public class MainActivity extends Activity {
    private EditText nameEditText = null;
    private EditText ageEditText = null;
    private Button saveButton = null;
    private Button resumeButton = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        nameEditText = (EditText) findViewById(R.id.name);
        ageEditText = (EditText) findViewById(R.id.age);
        saveButton = (Button) findViewById(R.id.button);
        resumeButton = (Button) findViewById(R.id.resume);

        saveButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = getSharedPreferences("test", Context.MODE_WORLD_READABLE);
                Editor editor = sharedPreferences.edit();
                editor.putString("name", nameEditText.getText().toString());
                editor.putInt("age", new Integer(ageEditText.getText()
                        .toString()));
                editor.commit();
                Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_LONG)
                .show();
            }
        });
        
        resumeButton.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = getSharedPreferences("test", Context.MODE_PRIVATE);
                String name = sharedPreferences.getString("name", "");
                int age = sharedPreferences.getInt("age", 20);
                nameEditText.setText(name);
                ageEditText.setText(String.valueOf(age));
            }
        });
    }
}

5:生成的test.xml的内容为:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="name">zhangbo</string>
<int name="age" value="24" />
</map>

6:注意当从外部程序对sharedprefences文件进行读取时,需要使用的程序为:

public void testSharedPreference() {
    try {
        Context context = getContext().createPackageContext("cn.itcast.sharedpreferences", Context.CONTEXT_IGNORE_SECURITY);
        SharedPreferences sharedPreference = context.getSharedPreferences("test", Context.MODE_PRIVATE);
        String name = sharedPreference.getString("name", "");
        int age = sharedPreference.getInt("age", 0);
            
        Log.i(TAG, "name:" + name + ", age:" + age);            
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
}
dd