• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Android下PreferenceActivity使用示例

PHP 水墨上仙 2740次浏览 0个评论

Android下PreferenceActivity使用示例

MainActivity如下:


package cn.testpreferenceactivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
/**
 * Demo描述:
 * PreferenceActivity的使用示例
 * 
 * 注意事项:
 * 1 不要ListPreference的属性android:dialogMessage
 *   若设置则无法弹出选择列表.
 *   这和单选,多选对话框类似不能设置其Message.
 * 2 不要ListPreference的属性android:positiveButtonText
 *   即使设置也无效
 * 3 注意ListPreference的默认值的设定
 *   android:defaultValue="222"
 *   该值必须是 android:entryValues="@array/values_array"
 *   这个数组中包含的值!!!!!!!!!!!!!
 *   
 *   
 * 参考资料:
 * 1 http://www.cnblogs.com/adm1989/archive/2012/08/01/2618403.html
 * 2 http://www.eoeandroid.com/forum.php?mod=viewthread&tid=188859
 *   Thank you very much
 */
public class MainActivity extends PreferenceActivity {
   private CheckBoxPreference mCheckBoxPreference;
   private ListPreference mListPreference;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferenceactivity);
		init();
	}
	private void init(){
		mCheckBoxPreference=
	    (CheckBoxPreference) findPreference(getResources().getString(R.string.first_key));
		mCheckBoxPreference.setOnPreferenceChangeListener(new PreferenceChangeListenerImpl());
		mCheckBoxPreference.setOnPreferenceClickListener(new PreferenceClickListenerImpl());
		
		mListPreference=
	    (ListPreference) findPreference(getResources().getString(R.string.listpreference_key));
		mListPreference.setOnPreferenceChangeListener(new PreferenceChangeListenerImpl());
		mListPreference.setOnPreferenceClickListener(new PreferenceClickListenerImpl());
	}
	
	private class PreferenceClickListenerImpl implements OnPreferenceClickListener{
		@Override
		public boolean onPreferenceClick(Preference preference) {
			String preferenceKey=preference.getKey().toString();
			if (preferenceKey.equals(getResources().getString(R.string.first_key))) {
				System.out.println("点击了CheckBoxPreference");
			}
			if (preferenceKey.equals(getResources().getString(R.string.listpreference_key))) {
				System.out.println("点击了ListPreference");
			}
			return true;
		}
	}
	
	/**
	 * 注意事项:
	 * 在OnPreferenceChangeListener的onPreferenceChange()
	 * 可知改变后的值,但该值还未真正存入尚在内存中贮存.
	 * 所以此时调用getValuesFromSP得到的只是改变前的值.
	 *
	 */
	private class  PreferenceChangeListenerImpl implements OnPreferenceChangeListener{
		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			String preferenceKey=preference.getKey().toString();
			if (preferenceKey.equals(getResources().getString(R.string.first_key))) {
				System.out.println("切换后值为:"+newValue.toString());
			}
			if (preferenceKey.equals(getResources().getString(R.string.listpreference_key))) {
				System.out.println("切换后值为:"+newValue.toString());
			}
			return true;
		}
		
	}
	
	@Override
	public void onBackPressed() {
		getValuesFromSP();
		super.onBackPressed();
	}
	
	// 从PreferenceActivity的SharedPreferences中取数据
	private void getValuesFromSP(){
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
		Boolean first_value = sp.getBoolean(getResources().getString(R.string.first_key), false);
		String listpreference_key = sp.getString(getResources().getString(R.string.listpreference_key), null);
		System.out.println("从SharedPreferences中取出的是:" + first_value);
		System.out.println("从SharedPreferences中取出的是:" + listpreference_key);
	}
}

preferenceactivity.xml如下:


<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <CheckBoxPreference
        android:key="@string/first_key"
        android:summary="@string/first_default"
        android:summaryOff="@string/first_off"
        android:summaryOn="@string/first_on"
        android:title="@string/first_title" 
    />

    <ListPreference
        android:dialogIcon="@drawable/ic_launcher"
        android:dialogTitle="@string/listpreference_title"
        android:entries="@array/names_array"
        android:entryValues="@array/values_array"
        android:key="@string/listpreference_key"
        android:negativeButtonText="@string/listpreference_no"
        android:summary="@string/listpreference_summary"
        android:title="@string/listpreference_title"
        android:defaultValue="222"
    />

</PreferenceScreen>

strings.xml如下:


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">TestPreferenceActivity</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    
    <string name="first_key">first_key</string>
    <string name="first_on">first_on</string>
    <string name="first_off">first_off</string>
    <string name="first_title">first_title</string>
    <string name="first_default">first_default</string>
    
    <string name="listpreference_key">listpreference_key</string>
    <string name="listpreference_title">listpreference_title</string>
    <string name="listpreference_summary">listpreference_summary</string>
    <string name="listpreference_message">listpreference_message</string>
    <string name="listpreference_ok">OK</string>
    <string name="listpreference_no">NO</string>

</resources>


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Android下PreferenceActivity使用示例
喜欢 (0)

您必须 登录 才能发表评论!

加载中……