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

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

jsc10年前 (2016-04-05)Android3412

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

android悬浮窗口的实现

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

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

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

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

Android开发之资源文件存储

在android开发中,资源文件是我们使用频率最高的,无论是string,drawable,还是layout,这些资源都是我们经常使用到的,而且为我们的开始提供很多方便,不过我们平时接触的资源目录一般都是下面这三个。        /res...

获取Android手机中SD卡存储信息

SD卡作为手机的扩展存储设备,在手机中充当硬盘角色,可以让我们手机存放更多的数据以及多媒体等大体积文件。因此查看SD卡的内存就跟我们查看硬盘的剩余空间一样,是我们经常操作的一件事,那么在Android开发中,我们如何能获取SD卡的内存容量呢?首先,要获取SD卡上面的信息,必须先对SD卡有访问的权限,...

android客户端和java服务端之间用socket来传输图片

一、从服务端向客户端发送图片:服务端的代码:import java.io.DataOutputStream;   import java.io.FileInputStream;   import java.io.IOExcep...