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

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

jsc9年前 (2016-04-05)Android3022

采用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://jsc0.com/post/59.html

分享给朋友:

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

Android Location在GPS中的应用(一)

Android Location在GPS中的应用(一)

新建AndroidProject,注意选择Google APIs:  打开AndroidManifest.xml,在其中加入GPS使用权限: <uses-permission android:name="android.permission.ACCESS_COARSE_LO...

android上传图片至服务器

本实例实现了android上传手机图片至服务器,服务器进行保存服务器servlet代码publicvoid doPost(HttpServletRequest request, HttpServletResponse response)  ...

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

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

Android SDK支持那些文件存储技术? 1. 使用SharedPreferences保存key-value类型的数据 2. 流文件存储(使用openFileOutput和openFileInput方法,或FileInputStream和FileO...

Android SharedPreferences PreperenceScreen 偏好数据存取

SharedPreferences是一个接口,程序是无法创建SharedPreferences实例的,1、Context.getSharedPreferences(String name,int mode)来得到一个指定name的SharedPreferences实例2、PreferenceMana...

Android利用JSON发送数据到服务器

new Thread() { @Override public void run() { // TODO Auto-generated method stub Looper.prepare();&nbs...

安卓对话框之----多选框(CheckBox)

安卓对话框之----多选框(CheckBox)

简单讲解一下原理:每个多选框都是独立的,可以通过迭代所有多选框,然后根据其状态是否被选中再获取其值。 CheckBox.setChecked(true);//设置成选中状态。 CheckBox.getText();//获取多选框的值 调用setOnCheck...