欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SharedPreferences 数据存储

程序员文章站 2022-05-16 17:22:04
...
MainActivity.java代码

package cn.itcast.settings;

import java.util.Map;

import cn.itcast.service.PreferencesService;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    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);
        Map<String, String> params = service.getPreferences();
        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();
    }
    
    
}


layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<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="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button"
    android:onClick="save"
    />
</LinearLayout>



value/string.xml 配置文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">软件参数设置</string>
    <string name="name">姓名</string>
    <string name="age">年龄</string>
    <string name="button">保存参数</string>
    <string name="success">保存完成</string>
</resources>


核心操作类 PerferencesServer.java

package cn.itcast.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 年龄
	 */
	public void save(String name, Integer age) {
		SharedPreferences preferences = context.getSharedPreferences("itcast", Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putString("name", name);
		editor.putInt("age", age);
		editor.commit();
	}
	/**
	 * 获取各项配置参数
	 * @return
	 */
	public Map<String, String> getPreferences(){
		Map<String, String> params = new HashMap<String, String>();
		SharedPreferences preferences = context.getSharedPreferences("itcast", Context.MODE_PRIVATE);
		params.put("name", preferences.getString("name", ""));
		params.put("age", String.valueOf(preferences.getInt("age", 0)));
		return params;
	}
}