android应用各参数或配置是用SharedPreference存储管理的,但是它没有界面,如果想用界面,
系统在3.0以上用PreferenceFragment(推荐用它),以下用PreferenceActivity
它们默认有listview显示各配置项,给它们指定一个配置xml文件就可.
下面是示例,它们用相同的xml.
1.效果
2.配置xml: res/xml/my_preference.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <CheckBoxPreference android:key="checkbox" android:title="是否开启" android:summaryOn="开启" android:summaryOff="关闭" /> <!-- entries是ListPreference列表项 ,entryValues指定列表项对应的各值--> <ListPreference android:key="list" android:title="请选择一个选项" android:summary="请点击选择" android:entries="@array/preference_entries" android:entryValues="@array/preference_values" /> <EditTextPreference android:key="text" android:title="请输入" android:dialogTitle="请输入名字" android:dialogMessage="有劳了" android:summary="请再次输入"/> </PreferenceScreen>
其中用到了list.下面是这个 list各item与各item对应的值. res/values/my_preference.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="preference_entries"> <item >Java</item> <item >C++</item> <item >Assume</item> </string-array> <string-array name="preference_values"> <item >eclipse</item> <item >visual studio</item> <item >notepad++</item> </string-array> </resources>
3.PreferenceFragment.java
import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.widget.Toast; public class MyPreferenceFrgment extends PreferenceFragment { PreferenceManager prefernceMgr; CheckBoxPreference checkPreference; ListPreference listPreference; EditTextPreference editPreference; /* * 读取各属性 */ void readPreference() { Toast.makeText(getActivity(), "当前选中状态为:" + checkPreference.isChecked(), 0).show(); Toast.makeText( getActivity(), "当前选中的列表项为:" + listPreference.getEntry() + "当前选中的列表项的值为:" + listPreference.getValue(), 1).show(); Toast.makeText(getActivity(), "当前选中的列表项为:" + "输入的内容为:" + editPreference.getText(), 1).show(); } void init() { addPreferencesFromResource(R.xml.my_preference);//关键代码 prefernceMgr = getPreferenceManager(); checkPreference = (CheckBoxPreference) prefernceMgr .findPreference("checkbox");//checkbox是在xml中定义的android:key listPreference = (ListPreference) prefernceMgr.findPreference("list"); editPreference = (EditTextPreference) prefernceMgr .findPreference("text"); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); } // @Override // public View onCreateView(LayoutInflater inflater, ViewGroup container, // Bundle savedInstanceState) { // //注意,如果指定自己的layout,那么该layout必有listview // View v = inflater.inflate(R.layout.frgmt_perference, container, false); // return v; // } }
4.MyPreferenceActivity.java
import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.PreferenceActivity; import android.preference.PreferenceManager; import android.preference.RingtonePreference; import android.widget.Toast; public class MyPreferenceActivity extends PreferenceActivity { PreferenceManager prefernceMgr; /* * PreferenceActivity主要使用的就是下面几个子preference */ CheckBoxPreference checkPreference; ListPreference listPreference; EditTextPreference editPreference; RingtonePreference rp; /* * 读取各属性 */ private void readPreferences() { Toast.makeText(getApplicationContext(), "当前选中状态为:" + checkPreference.isChecked(), 0).show(); Toast.makeText( getApplicationContext(), "当前选中的列表项为:" + listPreference.getEntry() + "当前选中的列表项的值为:" + listPreference.getValue(), 1).show(); Toast.makeText(getApplicationContext(), "当前选中的列表项为:" + "输入的内容为:" + editPreference.getText(), 1).show(); } private void init(){ //指定偏好设置的xml文件既可,不用在指定layout文件,PreferenceActivity用listview显示各偏好 addPreferencesFromResource(R.xml.my_preference); prefernceMgr = getPreferenceManager(); checkPreference = (CheckBoxPreference) prefernceMgr .findPreference("checkbox");//checkbox是在xml中定义的android:key listPreference = (ListPreference) prefernceMgr.findPreference("list"); editPreference = (EditTextPreference) prefernceMgr .findPreference("text"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); } }
时间: 2024-10-20 11:56:46