android开发笔记之SharedPreferences的使用

1.SharedPreferences人使用样例,

      SharedPreferences主要是存储一些简单的基本数据类型在xml文件中,并且采用内容观察者模式来监听数据变化,从而进行相应的操作。

2.code样例:

(1)activity_main.xml

<RelativeLayout 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: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=".MainActivity" >

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <Button 
		android:id="@+id/myButton"
		android:layout_below="@id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请点击我" 
        />

</RelativeLayout>


(2)MySharedPreferences.java

package com.example.testsharedpreferencedemo;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class MySharedPreferences { 
	
	public static final String MySharedPreferencesName= "shared_preferences_data"; 
      
    private  Context context = null;  
    private  SharedPreferences sp = null;  
    private Editor editor = null;  
          
    public static final String Key_Int_Count = "Key_Int_Count";  
    public static final int Value_Int_Count = 0;  
                     
    public MySharedPreferences(Context context){  
        this.context = context;  
        this.sp = this.context.getSharedPreferences(MySharedPreferencesName,0);  
        this.editor =  sp.edit();  
    }  
          
    public int getIntCount()  
    {                
        return sp.getInt(Key_Int_Count, Value_Int_Count);     
    }  
      
    public void setIntCount(int intCount)  
    {         
        editor.putInt(Key_Int_Count, intCount);  
        editor.commit();  
    }    
    
    public SharedPreferences getSharedPrefere(){
    	return sp;
    }
} 

(3)MainActivity.java

package com.example.testsharedpreferencedemo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;

public class MainActivity extends Activity implements OnClickListener,
											OnSharedPreferenceChangeListener{
	public static final String TAG = "testsharedpreferencedemo";
	
	private TextView myTextView = null;
	private Button myButton = null;
	private MySharedPreferences mySharedPre = null;
	
	public static final String clicl_count = "您点击了Button的次数为:";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        init();
    }

	private void init() {
		// TODO Auto-generated method stub
		mySharedPre = new MySharedPreferences(this);
		mySharedPre.setIntCount(0);
		mySharedPre.getSharedPrefere().registerOnSharedPreferenceChangeListener(this);
				
		myTextView = (TextView) findViewById(R.id.myTextView);		
		myButton = (Button) findViewById(R.id.myButton);
		myButton.setOnClickListener(this);
		
		myTextView.setText(clicl_count + mySharedPre.getIntCount());		
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if(view == myButton){
			setXMLData();
		}
	}

	private void setXMLData() {
		// TODO Auto-generated method stub
		mySharedPre.setIntCount(mySharedPre.getIntCount()+1);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPre, String key) {
		// TODO Auto-generated method stub
		Log.i(TAG, "onSharedPreferenceChanged");
		if(key.equals(MySharedPreferences.Key_Int_Count)){
			updateUI();
		}
	}

	private void updateUI() {
		// TODO Auto-generated method stub
		myTextView.setText(clicl_count + mySharedPre.getIntCount());	
	} 
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mySharedPre.getSharedPrefere().unregisterOnSharedPreferenceChangeListener(this);
	}
}

3.代码下载地址:

http://download.csdn.net/detail/hfreeman2008/7808941


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hfreeman2008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值