SharedPreferences(保存偏好参数)

初次接触SharedPreferences,借助code-pig的博客写的小demo。实现每次打开app自动填充上次推出时输入的名字和年龄。


工具类SharedService

package com.example.sharedpreference;

import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.Toast;

public class SharedService {

    private Context context ;
    public SharedService(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context ;
    }
    
    public void save(String name,String age) {
        SharedPreferences sp = context.getSharedPreferences("base", Context.MODE_WORLD_READABLE) ;
        Editor edit = sp.edit() ;
        edit.putString("name", name) ;
        edit.putString("age", age) ;
        edit.commit() ;
        Toast.makeText(context, "信息已经成功写入", Toast.LENGTH_LONG) ;
    }
    
    public Map<String,String> read() {
        Map<String,String> map = new HashMap<String, String>() ;
        SharedPreferences sp = context.getSharedPreferences("base", Context.MODE_WORLD_READABLE) ;
        map.put("name", sp.getString("name", "")) ;
        map.put("age", sp.getString("age", "")) ;
        return map ;
    }

}



主activity

package com.example.sharedpreference;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;  
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

	private EditText name ;
	private EditText age ;
	private Button ok ;
	private SharedService ss ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.name = (EditText) findViewById(R.id.name) ;
		this.age = (EditText)findViewById(R.id.age) ;
		this.ok = (Button) findViewById(R.id.ok) ;
		ss = new SharedService(this) ;
		this.name.setText(ss.read().get("name"));
		this.age.setText(ss.read().get("age"));
		
		this.ok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ss.save(name.getText().toString(), age.getText().toString());
				
			}
		});
		
	}

}


布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.sharedpreference.MainActivity" >

   <EditText 
       android:id="@+id/name"
       android:layout_width="fill_parent"
       android:hint="输入姓名"
       android:layout_height="wrap_content"/>
   
   <EditText 
       android:id="@+id/age"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:hint="输入年龄"/>
   
   <Button 
       android:id="@+id/ok"
       android:layout_height="wrap_content"
       android:layout_width="fill_parent"
       android:text="确定"/>

</LinearLayout>


原文地址:https://www.cnblogs.com/emoji/p/4436829.html