android中SharedPreferences的简单例子

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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

波哥的技术积累

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值