android 偏好设置,SharedPreferences保存、显示用户偏好设置

例如要保存用户的姓名和年龄两个参数,如下图布局

0121ae81210417c30daed88af9a48eac.png

1.在strings.xml文件中声明要使用到的字符串

ea469efbe4f6b6678d869d0dc669b239.png

2.在布局文件中添加,,控件,实现上图的布局

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/name" />

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/name" />

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/age" />

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:numeric="integer"

android:id="@+id/age" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/button"

android:onClick="save"

/>

3.MainActivity的代码如下public class MainActivity extends Activity {

private EditText nameText;

private EditText ageText;

private PreferencesService service;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

nameText = (EditText)this.findViewById(R.id.name);

ageText = (EditText)this.findViewById(R.id.age);

service = new PreferencesService(this);//每次点击保存都会实例化该方法

}

public void save(View v) {

String name = nameText.getText().toString();

String age = ageText.getText().toString();

//PreferencesService service = new PreferencesService(this);//每次点击保存都会实例化,放到启动方法中

service.save(name,Integer.valueOf(age));

Toast.makeText(getApplicationContext(), R.string.success, 1).show();

}

}

4.PreferencesService类的代码如下

public class PreferencesService {

private Context context;

public PreferencesService(Context context) {//得到上下对象

this.context = context;

}

/**

* 保存参数

* @param name

* @param age

*/

public void save(String name, Integer age) {

//取得SharePreferences对象,通过上下文环境得到

SharedPreferences preferences = context.getSharedPreferences("gao", Context.MODE_PRIVATE);

Editor editor = preferences.edit();//得到编辑器对象

editor.putString("name", name);

editor.putInt("age", age);//到此数据保存在内存中

editor.commit();//把内存中的数据提交到文件中

}

}

运行结果产生的xml文件中的数据

cb80c269b59bc95d26c98a6197632291.png

以上即实现了用户自己对软件偏好参数的保存,那么如何读取用户的偏好参数呢?如用户打开上述软件时,显示用户的参数设置,如下图

fa796c0fde6e9960fbc1d0035626f4ab.png

实现方法是,在PreferencesService类中添加getPreferences()方法,具体代码如下

/**

* 获取各项配置参数

* @return 参数值

*/

public Map getPreferences(){

Map params = new HashMap();

//取得SharePreferences对象,通过上下文环境得到,"gao"是之前保存好的数据名称,注意不带后缀名

SharedPreferences preferences = context.getSharedPreferences("gao", Context.MODE_PRIVATE);

params.put("name", preferences.getString("name", "you name"));

params.put("age", String.valueOf(preferences.getInt("age", 0)));

return params;

}

在MainActivity类的OnCreate()方法中添加如下代码

//第一次运行时显示参数

Map params = service.getPreferences();

nameText.setText(params.get("name"));

ageText.setText(params.get("age"));

当再次执行时,用户输入新的数据并点击保存,那么就会保存最近的用户输入的数据。

注:附件文章中的代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值