当前位置:首页 > Android > 正文内容

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

jsc9年前 (2016-04-05)Android3037

采用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"
    />www.2cto.com
</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>


扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://www.jsc0.com/post/59.html

分享给朋友:

“采用SharedPreferences保存用户偏好设置参数” 的相关文章

Android--SQLite(增,删,改,查)操作

            需要5个类:1.实体类:Person.java 2.抽象类:SQLOperate.java(封装了对数据库的操作) 3.助手类:DBOpenHelper.j...

Android中使用PULL方式解析XML文件

Android中使用PULL方式解析XML文件

 Pull解析器的运行方式与 SAX 解析器相似。它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素 并触发相应事件。跟SAX不同的是, Pull解析器产生的事件是一个数字,而非方法,因此可以使用一个s...

Android权限问题整理

Android权限系统非常庞大,我们在Android系统中做任何操作都需要首先获取Android系统权限,本文记录了所有的Android权限问题,整理一下分享给大家。访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES读取或写入登记check-in数...

Android采用SharedPreferences保存用户登录信息

Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用 SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared...

Android应用加入微信分享

Android应用加入微信分享

一、申请你的AppIDhttp://open.weixin.qq.com/  友情提示:推荐使用eclipse打包软件最后一步的MD5值去申请AppID二、官网下载libammsdk.jar包http://open.weixin.qq.com/download/?lang=zh_...

Android 学习之 开源项目PullToRefresh的使用

Android 学习之 开源项目PullToRefresh的使用

首先 下载 Android-PullToRefresh-master 下载地址  https://github.com/chrisbanes/Android-PullToRefresh 下载之...