android学习 --SharePreference数据储存和提取

android手机储存数据之一就是利用SharePreference,比较适合用于手机的一些轻型的设置。以下是一个例子:

1、在res/xml文件夹下新建一个xml文件(mypreference.xml)

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <PreferenceCategory
        android:key="checkbox"
        android:title="设置CheckBox值"
        >
        
        <CheckBoxPreference
            android:key="checkbox1"
            android:title="CHECKBOX_1"
            android:summary="请设置CHECKBOX1的值"
            android:defaultValue="true">
        </CheckBoxPreference>
        
         <CheckBoxPreference
            android:key="checkbox2"
            android:title="CHECKBOX_2"
            android:summary="请设置CHECKBOX1的值"
            android:defaultValue="false">
        </CheckBoxPreference>
        
    </PreferenceCategory>
    
    <PreferenceCategory
        android:title="设置EditTextPreference"
        android:key="edittext">
        
        <EditTextPreference 
            android:title="你的电话号码"
            android:key="phone"
            android:summary="请输入电话号码"
            android:defaultValue="电话"
            />
        
    </PreferenceCategory>
    
    <PreferenceCategory 
        android:title="设置ListPreference"
        android:key="ListPreferenc"
        >
        
        <ListPreference
            android:title="部门选择"
            android:key="list"
            android:dialogTitle="部门信息"
            android:entries="@array/department"
            android:entryValues="@array/department_value"
            ></ListPreference>
        
    </PreferenceCategory>


</PreferenceScreen>

2、在value文件夹下新建一个array文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    
<string-array name="department">
 <item>综合部</item>
 <item>外贸部</item>
 <item>行政部</item>
</string-array>

<string-array name="department_value">
 <item>001</item>
 <item>002</item>
 <item>003</item>
</string-array>


</resources>

3、起一个PreferenceActivity

package com.example.preference;


import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.widget.Toast;


public class MyPreference extends PreferenceActivity{


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

this.addPreferencesFromResource(R.xml.mypreference);

//取得checkbox的控件
CheckBoxPreference checkbox1 = (CheckBoxPreference)findPreference("checkbox1");
//监听第一个checkbox
checkbox1.setOnPreferenceClickListener(new OnPreferenceClickListener() {

@Override
public boolean onPreferenceClick(Preference preference) {
// TODO Auto-generated method stub
return true;
}

});

EditTextPreference edittext = (EditTextPreference)this.findPreference("phone");

//设置输入框的按钮的值
edittext.setPositiveButtonText("确定");
edittext.setNegativeButtonText("取消");



checkbox1.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
System.out.println(newValue);
//如果选中了选项
if(newValue.equals(true)){
Toast.makeText(MyPreference.this, "你钩了这个选项", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MyPreference.this, "你去掉了这个选项", Toast.LENGTH_LONG).show();
}
return true;
}

});
}

}

4、在MainActivity中取得设置的信息

package com.example.preference;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {

private static final int EXIT = 0;
private static final int SET_ALL = 1;

private TextView showmessage;
private Button get;



@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

showmessage = (TextView)this.findViewById(R.id.showmessage);

get = (Button)this.findViewById(R.id.get);
get.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String message = "";
//得到储存信息的句柄
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
//根据key取值
boolean checkbox1 = sp.getBoolean("checkbox1", true);
boolean checkbox2 = sp.getBoolean("checkbox2", true);
message += "checkbox1的值:" + checkbox1 + "\n";
message += "checkbox2的值:" + checkbox2 + "\n";
//getString的第二参数不明白,设为空值就行了
String phonenumber = sp.getString("phone", "");
message += "电话号码:" + phonenumber + "\n";
String list = sp.getString("list", "");
message += "选择的部门:" + list;
showmessage.setText(message);
}

});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
menu.add(0, EXIT, 0, "退出");
menu.add(0, SET_ALL, 0, "设置全部");
return true;
}


@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {

switch(item.getItemId()){
case EXIT : 
this.finish();
break;
case SET_ALL : 
Intent intent = new Intent(MainActivity.this,MyPreference.class);
startActivity(intent);
break;
}

return super.onMenuItemSelected(featureId, item);
}

}




你可能感兴趣的:(android,Preference,sharepreference,数据储存)