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

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

jsc10年前 (2016-04-05)Android3376

采用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悬浮窗口的实现

android悬浮窗口的实现

       当我们在手机上使用360安 全卫士时,手机屏幕上时刻都会出现一个小浮动窗口,点击该浮动窗口可跳转到安全卫士的操作界面,而且该浮动窗口不受其他activity的覆盖影响仍然可 见(多米音乐也有相关的和主界面交互的...

google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果

google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果

    了解二维码这个东西还是从微信中,当时微信推出二维码扫描功能,自己感觉挺新颖的,从一张图片中扫一下竟然能直接加好友,不可思议啊,那时候还不了解二维码,呵呵,然后做项目的时候,老板说要加上二维码扫描功能,然后自己的屁颠屁颠的去百度,google...

android json解析及简单例子

JSON的定义:       一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换。JSON采用兼容性很高的文本格式...

实现应用程序只有在第一次启动时显示引导界面

第一次安装启动:启动页--->导航页-->主页面之后启动:启动页-->主页面实现的原理就是:在启动页面用做一个文件保存的状态,保存程序是不是第一次启动的状态。因为只是要保存一个状态,我们将这个程序是第一次打开就将他设为true,当他进入 主页面之后将他的状态未为false,因为都...

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

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

Android ImageView的scaleType属性与adjustViewBounds属性

Android ImageView的scaleType属性与adjustViewBounds属性

ImageView的scaleType的属性有好几种,分别是matrix(默认)、center、centerCrop、centerInside、fitCenter、fitEnd、fitStart、fitXY android:sca...