学習日記--PreferenceFragmentテスト
7715 ワード
一、Javaコード
2つのクラスを作成する必要があります.MyFragmentはPreferenceFragmentを継承する必要があります.
1、MyFragment
2、MainActivity
二、
res/xmlディレクトリの下のpreferences.xml
三、
必要なres/values/arrays.xml
2つのクラスを作成する必要があります.MyFragmentはPreferenceFragmentを継承する必要があります.
1、MyFragment
package com.hxzy.preferenceactivity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.widget.Toast;
public class MyFragment extends PreferenceFragment{
private MyOnSharePreferenceChangeListener mOnSharePreferenceChangeListener = new MyOnSharePreferenceChangeListener();
private final String A_KEY = "edittext_key_1";
private final String LIST_KEY = "list_key_1";
private EditTextPreference mEditTextPreference;
private ListPreference mListPreference;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.addPreferencesFromResource(R.xml.preferences);
//
SharedPreferences mSharePreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String s = mSharePreferences.getString("edittext_key_1", " ");
Boolean b = mSharePreferences.getBoolean("checkbox_key_1", true);
Log.d("edittext_key_1", s);
Log.d("checkbox_key_1", b + "");
mSharePreferences.registerOnSharedPreferenceChangeListener(mOnSharePreferenceChangeListener);
// 。 PreferenceActivity
// EditText mEditText=findViewById(R.id.editText),key id;
mEditTextPreference = (EditTextPreference) findPreference(A_KEY);
mEditTextPreference.setSummary(s);
mListPreference = (ListPreference) findPreference(LIST_KEY);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(mOnSharePreferenceChangeListener);
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(mOnSharePreferenceChangeListener);
}
//
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Toast.makeText(getActivity(), preference.getSummary(), Toast.LENGTH_LONG).show();
return false;
}
private class MyOnSharePreferenceChangeListener implements OnSharedPreferenceChangeListener {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Toast.makeText(getActivity(), key, 0).show();
if (key.equals(A_KEY))
{
String s = sharedPreferences.getString(A_KEY, "null");
mEditTextPreference.setSummary(s);
}
if (key.equals(LIST_KEY)) {
String e = mListPreference.getEntry() + "";
String v = mListPreference.getValue() + "";
mListPreference.setSummary(e + ":" + v);
}
}
}
}
2、MainActivity
package com.hxzy.preferenceactivity;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getFragmentManager().beginTransaction()
.replace(android.R.id.content, new MyFragment())
.commit();
}
}
二、
res/xmlディレクトリの下のpreferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="[first group])" >
<EditTextPreference
android:defaultValue="111111"
android:key="edittext_key_1"
android:summary="editview summary"
android:title="edittext title" />
<ListPreference
android:dialogIcon="@drawable/a"
android:dialogTitle="list dialog title"
android:entries="@array/citys"
android:entryValues="@array/citys_code"
android:icon="@drawable/ic_launcher"
android:key="list_key_1"
android:summary="list summary"
android:title="list title" />
<!-- defaultValue true ,false -->
<CheckBoxPreference
android:defaultValue="false"
android:key="checkbox_key_1"
android:summary="checkbox summary"
android:title="checkbox title" />
</PreferenceCategory>
<Preference
android:summary="preference summary"
android:title="preference title" />
<PreferenceCategory
android:defaultValue="aaaaaa"
android:key="secondgroup_key"
android:summary="secondgroup summary"
android:title="[second group]" >
<CheckBoxPreference
android:defaultValue="true"
android:key="checkbox_key_2"
android:summary="checkbox summary"
android:title="checkbox title" />
<EditTextPreference
android:defaultValue="bbbbbbbbb"
android:key="edittext_key_2"
android:summary="edittext summary"
android:title="edittext title" />
</PreferenceCategory>
<Preference
android:summary="http://blog.csdn.net/zhangphil"
android:title=" " >
<intent
android:action="android.intent.action.VIEW"
android:data="http://blog.csdn.net/zhangphil" />
</Preference>
<!-- -->
<PreferenceScreen
android:key="preference_screen_key_2"
android:persistent="true"
android:title="preference screen 2" >
<EditTextPreference
android:defaultValue="cccccc"
android:key="edittext_key_3"
android:summary="second summary"
android:title="second title" />
</PreferenceScreen>
<PreferenceScreen
android:key="preference_screen_key_3"
android:persistent="true"
android:title="preference screen 3" >
<EditTextPreference
android:defaultValue="ccccccd"
android:key="edittext_key_4"
android:summary="second summary"
android:title="second title" />
</PreferenceScreen>
<SwitchPreference
android:defaultValue="fffff"
android:key="SwitchPreference_key"
android:summary="SwitchPreference summary"
android:title="SwitchPreference title" />
</PreferenceScreen>
三、
必要なres/values/arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="citys">
<item> </item>
<item> </item>
<item> </item>
</string-array>
<string-array name="citys_code">
<item>028</item>
<item>010</item>
<item>021</item>
</string-array>
</resources>