<span style="font-size:18px;">package com.example.testpreference;
import java.util.Iterator;
import java.util.Set;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.MultiSelectListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainActivity extends PreferenceActivity implements OnPreferenceClickListener , OnPreferenceChangeListener{
private static final String TAG = null;
Intent intentTest;
EditTextPreference editTextPreference ;
ListPreference listPreference ;
MultiSelectListPreference multiSelectListPreference;
CheckBoxPreference boxPreference;
SwitchPreference preference;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.text_preference);
ComponentName componentName = new ComponentName("com.yunos.osupdate",
"com.yunos.osupdate.front.UpdateActivity11");
intentTest = new Intent();
intentTest.setComponent(componentName);
findPreference();
}
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private void findPreference() {
editTextPreference = (EditTextPreference) findPreference("edit0");
if (null != editTextPreference){
editTextPreference.setOnPreferenceClickListener(this);
editTextPreference.setOnPreferenceChangeListener(this);
}
listPreference = (ListPreference) findPreference("ListPreference");
if (null != listPreference){
listPreference.setOnPreferenceClickListener(this);
listPreference.setOnPreferenceChangeListener(this);
}
multiSelectListPreference = (MultiSelectListPreference) findPreference("MultiSelectListPreferencekey");
if (null != multiSelectListPreference){
multiSelectListPreference.setOnPreferenceClickListener(this);
multiSelectListPreference.setOnPreferenceChangeListener(this);
}
boxPreference = (CheckBoxPreference) findPreference("CheckBoxPreferenceKey");
if (null != boxPreference){
boxPreference.setOnPreferenceClickListener(this);
boxPreference.setOnPreferenceChangeListener(this);
}
}
@Override
public boolean onPreferenceClick(Preference preference) {
String key = preference.getKey();
if (key.equals("edit0")){
String contect = ((EditTextPreference)preference).getText();
Log.i(TAG , "contect = " + contect);
}else if (key.equals("ListPreference")){
String entry = (String) listPreference.getEntry();
String value = (String) listPreference.getValue();
Log.i(TAG, "entry = " + entry + " value = " + value);
}else if (key.equals("MultiSelectListPreferencekey")){
<span style="color:#cc0000;">Set<String> temp = ((MultiSelectListPreference)preference).getValues();
Iterator<String> iterator = temp.iterator();
while(iterator.hasNext()){
String item = iterator.next();
Log.i(TAG , " valuse : " + item);
}</span>
}
return false;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String key = preference.getKey();
if (key.equals("edit0")){
String temp = (String) newValue;
((EditTextPreference)preference).setText(temp);
Log.i(TAG , "onPreferenceChange newValue = " + temp);
}else if (key.equals("ListPreference")){
String temp = (String) newValue;
((ListPreference)preference).setValue(temp);
Log.i(TAG , "onPreferenceChange newValue = " + temp);
}else if (key.equals("MultiSelectListPreferencekey")){
<span style="color:#990000;">Set<String> temp = (Set<String>) newValue;
Iterator<String> iterator = temp.iterator();
while(iterator.hasNext()){
String item = iterator.next();
Log.i(TAG , "newValue " + item);
}</span>
((MultiSelectListPreference)preference).setValues(temp);
}
return false;
}
}
</span>
<span style="font-size:18px;">XMl :</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="DialogPreference" >
<EditTextPreference
android:key="edit0"
android:summary="EditTextPreference summary"
android:title="EditTextPreference" />
<ListPreference
android:entries="@array/myentry"
android:entryValues="@array/myentryvalues"
android:key="ListPreference"
android:summary="ListPreference summary"
android:title="ListPreference" />
<MultiSelectListPreference
android:entries="@array/myentry"
android:entryValues="@array/myentryvalues"
android:key="MultiSelectListPreferencekey"
android:summary="MultiSelectListPreference summary"
android:title="MultiSelectListPreference" />
</PreferenceCategory>
<PreferenceCategory android:title="RingtonePreference" >
<RingtonePreference
android:key="RingtonePreferencekey"
android:summary="RingtonePreference "
android:title="RingtonePreference" />
</PreferenceCategory>
<PreferenceCategory android:title="TwoStatePreference" >
<SwitchPreference
android:key="SwitchPreferencekey"
android:summary="SwitchPreference summary"
android:summaryOff="SwitchPreference summaryOff"
android:summaryOn="SwitchPreference summaryOn"
android:switchTextOff="SwitchPreference switchTextOff"
android:switchTextOn="SwitchPreference switchTextOn"
android:title="SwitchPreference" />
<CheckBoxPreference
android:key="CheckBoxPreferenceKey"
android:summary="CheckBoxPreference summary"
android:summaryOff="CheckBoxPreference summaryOff"
android:summaryOn="CheckBoxPreference summaryOn"
android:title="CheckBoxPreference" />
</PreferenceCategory>
</PreferenceScreen></span>
<span style="font-size:18px;"><resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string-array name="myentry">
<item >enrty0</item>
<item >enrty1</item>
<item >enrty2</item>
<item >enrty3</item>
<item >enrty4</item>
</string-array>
<string-array name="myentryvalues">
<item >enrty0_v</item>
<item >enrty1_v</item>
<item >enrty2_v</item>
<item >enrty3_v</item>
<item >enrty4_v</item>
</string-array>
</resources></span>
注意要手动调用setValue 才可以持久化保存 ,而且一定要设置key