首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android学习札记-16_采用SharedPreferences保存用户偏好设置参数

2013-03-12 
Android学习笔记---16_采用SharedPreferences保存用户偏好设置参数16_采用SharedPreferences保存用户偏好

Android学习笔记---16_采用SharedPreferences保存用户偏好设置参数

16_采用SharedPreferences保存用户偏好设置参数

Android学习笔记---16_采用SharedPreferences保存用户偏好设置参数2013-03-0816_采用SharedPreferences保存用户偏好设置参数-------------------------------------------------1.eclipse就是通过xml来保存用户的偏好设置-->window-->perfences---------------------------------------2.利用SharedPreferences(参数)保存用户在android软件上设置的偏好;--------------------------------3.android:numeric="integer"限定输入框只能输入整形------------------------------------------------------4.<Button    android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/button"         android:onClick="save"//指定点击的方法,这个方法必须要
在//SharedPreferencesActivity.java中有定义    />-------------------------------------------------------5.如何从SharedPreferences读取数据  软件需求,如果用户曾经设置过参数那么,当用户再次打开的时候,要把用户曾经设置的参数  显示出来---------------------------------------------6. //当软件需要参数设置的时候,要用到这个2013-03-09----------------------------------7.把用户的数据保存到xml文件中,并创建xml文件,和读取xml文件,以显示用户的设置---------------------------------------------------8.所用代码如下: 项目:SharedPreferences com.credream.SharedPreferences.PreferencesService.java package com.credream.SharedPreferences;
import java.util.HashMap;import java.util.Map;
import com.credream.SharedPreferences.R.string;
import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;
public class PreferencesService{private Context context;
public PreferencesService(Context context){ this.context = context;}/** * 保存参数 * @param name 姓名 * @param age 年龄 */public void save(String name, Integer age){ SharedPreferences preferences=context.getSharedPreferences("credream", 
Context.MODE_PRIVATE); //不要添加后缀名,第二个是操作模式 Editor editor=preferences.edit(); editor.putString("name", name); editor.putInt("age", age);//第一个是参数名称,第二个是参数值 //目前是保存在内存中 editor.commit();//把内存中存在的数据写到文件中}/** * 获取各项配置参数 * @return */public Map<String, String> getPreference(){Map<String, String> params=new HashMap<String, String>();SharedPreferences preferences=context.getSharedPreferences("credream", 
Context.MODE_PRIVATE);params.put("name", preferences.getString("name", ""));params.put("age", String.valueOf(preferences.getInt("age", 0)));//第一个是xml中的<name>,第二个参数是默认值""return params;}
}--------------------2./SharedPreferences/src/com/credream/SharedPreferences/SharedPreferencesActivity
.java----------------package com.credream.SharedPreferences;

import java.util.Map;
import android.app.Activity;
import android.os.Bundle;import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class SharedPreferencesActivity extends Activity {
    /** Called when the activity is first created. */    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);//放到oncreate,只需要实例化一次这个对象就
可以了    Map<String, String> params=service.getPreference();    nameText.setText(params.get("name"));    ageText.setText(params.get("age"));}        public void save(View v){    String name=nameText.getText().toString();    String age=ageText.getText().toString();          service.save(name,Integer.valueOf(age));     Toast.makeText(getApplicationContext(), R.string.success, 1).show();    
//this.getPreferences(mode);//默认情况下,会采用    //Activity这个类的名称作为xml文件的名称    //在Activity类中如果需要Preference的时候,可以这样用    //当软件需要参数设置的时候,要用到这个    }}--------------------3./SharedPreferences/res/layout/main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >
    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/name" />       <EditText         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/name" />            <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/age" />    <EditText         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:numeric="integer"        android:id="@+id/age" />     <Button    android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/button"         android:onClick="save"    /></LinearLayout>------------------------------------------------------4./SharedPreferences/res/values/strings.xml<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World, SharedPreferencesActivity!</string>    <string name="app_name">软件参数设置</string>    <string name="name">姓名</string>    <string name="age">年龄</string>    <string name="button">保存参数</string>     <string name="success">保存完成</string></resources>---------------------------------------------------

热点排行