Android文件存储--采用SharedPreferences保存用户偏好设置参数和读取设置参数

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

Android文件存储--采用SharedPreferences保存用户偏好设置参数和读取设置参数


Android SDK支持那些文件存储技术?

1. 使用SharedPreferences保存key-value类型的数据

2. 流文件存储(使用openFileOutput和openFileInput方法,或FileInputStream和FileOutputStream)

3. XML半结构化存储

4. 用JSON保存数组和对象

5.用数据库保存结构化数据

6. 用第三方的面向对象数据库直接保存Java对象。


这篇博文主要介绍用SharedPreferences保存key-value对的步骤和读取设置参数的方法

1. 使用Context.getSharedPreferences方法获取SharedPreferences对象,其中存储key-value的文件的名称有getSharedPreferences方法第一个参数指定。

2. 使用SharedPreference.edit方法获取SharedPreferences.Editor对象。

3. 通过SharedPreference.Editor接口的putXxx方法保存key-value对。

4. 通过SharedPreference.Editor.commit方法提交要保存的key-value对。



实例:SharedPreferences



MainActivity.java

package com.wwj.setting;import java.util.Map;import com.wwj.service.PreferencesService;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends Activity private EditText nameText;  //姓名框 private EditText ageText;  //年龄框 private RadioGroup radioGroup; //单选框组  //业务逻辑类 private PreferencesService service;     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        nameText = (EditText)findViewById(R.id.nameText);        ageText = (EditText)findViewById(R.id.ageText);        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);        service = new PreferencesService(this);                Map<String, String> params = service.getPerferences();        nameText.setText(params.get("name"));        ageText.setText(params.get("age"));        radioGroup.check(Integer.valueOf(params.get("sex"))); //设置选择的单选按钮            }        /**     * 在布局中按钮控件指定的onClick方法     * @param v     */    public void save(View v) {     String name = nameText.getText().toString();     String age = ageText.getText().toString();     Integer sex = radioGroup.getCheckedRadioButtonId();     service.save(name, Integer.valueOf(age), sex);     Toast.makeText(getApplicationContext(), R.string.success, 1).show();    }    }


PreferencesService.java

package com.wwj.service;import java.util.HashMap;import java.util.Map;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 年龄   * @param sex 性别  */ public void save(String name, Integer age, Integer sex) {  //获得SharedPreferences对象  SharedPreferences preferences = context.getSharedPreferences("wwj", Context.MODE_PRIVATE);  Editor editor = preferences.edit();  editor.putString("name", name);  editor.putInt("age", age);  editor.putInt("sex", sex);  editor.commit(); } /**  * 获取各项参数  * @return  */ public Map<String, String> getPerferences() {  Map<String, String> params = new HashMap<String, String>();  SharedPreferences preferences = context.getSharedPreferences("wwj", Context.MODE_PRIVATE);  params.put("name", preferences.getString("name", ""));  params.put("age", String.valueOf(preferences.getInt("age", 0)));  params.put("sex", String.valueOf(preferences.getInt("sex", 0)));  return params; }   }


布局文件activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" > <TextView      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/name"/> <EditText      android:id="@+id/nameText"     android:layout_width="match_parent"     android:layout_height="wrap_content"     /> <TextView      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/age"/> <EditText      android:id="@+id/ageText"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:numeric="integer"     /> <RadioGroup        android:id="@+id/radioGroup"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:contentDescription="性别" >        <RadioButton            android:id="@+id/radioButton1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/male"             android:checked="true"/>        <RadioButton            android:id="@+id/radioButton2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/female" />    </RadioGroup>  <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="save"        android:text="@string/saveBtn" /> </LinearLayout>










           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值