Android学习笔记---用SharedPreferences保存用户偏好

步聚一:配置好要用的字符变量

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">用户偏好设置</string>
    <string name="nametips">姓名</string>
    <string name="action_settings">Settings</string>
	<string name="btntext">保存</string>
	<string name="agetips">年龄</string>
	<string name="success">保存成功</string>
</resources>

步骤二:配置界面

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.sharedprefers.MainActivity" xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/nametips" />
    
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
         />
    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/agetips"
        />

    <EditText
        android:id="@+id/age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal" />

	<Button 
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:id="@+id/btnsave"
	    android:text="@string/btntext"
	    android:onClick="Save"
	    />
</LinearLayout>

步骤三:编写相关操作类

package com.example.Service;

import android.content.*;
import android.content.SharedPreferences.Editor;
import java.util.*;
public class SharedPerefer {
	private Context context;
	public SharedPerefer(Context context) {
		this.context = context;
	}
	
	/*
	 * 用SharedPreferences保存用户偏好
	 */
	public void save(String name, String age) {
		SharedPreferences sp=context.getSharedPreferences("mytest", context.MODE_PRIVATE);
		Editor edit=sp.edit();
		edit.putString("name", name);
		edit.putString("age", age);
		edit.commit();
	}
	
	/*
	 * 从SharedPreferences中取信息
	 */
	public Map<String,String> GetPreference()
	{
		Map<String,String> maps=new HashMap<String,String>();
		SharedPreferences sp=context.getSharedPreferences("mytest", context.MODE_PRIVATE);
		maps.put("name",sp.getString("name", ""));
		maps.put("age", sp.getString("age", "0"));
		return maps;
	}
	
}

步骤四:编写Activity后台代码

package com.example.sharedprefers;

import java.util.Map;

import com.example.Service.SharedPerefer;
import android.view.*;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText txtName;
	private EditText txtAge;
	private SharedPerefer sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		txtName=(EditText)this.findViewById(R.id.name);
		txtAge=(EditText)this.findViewById(R.id.age);
		sp=new SharedPerefer(getApplicationContext());
		
		Map<String,String> maps=sp.GetPreference();
		txtName.setText(maps.get("name"));
		txtAge.setText(maps.get("age"));
	}
	
	public void Save(View v){
		//在Activity中取SharedPreferences对象可以用this.getPreferences(this.MODE_PRIVATE),只是保存的文件名固定为当前activity的名称
		
		String name=txtName.getText().toString();
		String age=txtAge.getText().toString();
		
		sp.save(name,age);
		Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值