android Preference Demo

最终实现效果




 


 

项目目录结构


 

 

PreferenceDemoActivity.java

package com.royal.perferenceDemo;

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

public class PreferenceDemoActivity extends PreferenceActivity implements OnPreferenceClickListener, OnPreferenceChangeListener {

	private CheckBoxPreference cbp = null;
	private EditTextPreference etp = null;
	private ListPreference lp = null;

	private String cbpKey = null;
	private String etpKey = null;
	private String lpKey = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 加入布局文件
		addPreferencesFromResource(R.xml.preferences);

		cbpKey = getResources().getString(R.string.cbp_key);
		etpKey = getResources().getString(R.string.etp_key);
		lpKey = getResources().getString(R.string.lp_key);

		cbp = (CheckBoxPreference) this.findPreference(cbpKey);
		etp = (EditTextPreference) this.findPreference(etpKey);
		lp = (ListPreference) this.findPreference(lpKey);

		//设置监听
		setUpListener();
	}

	/**
	 * 设置监听
	 */
	private void setUpListener() {
		//点击监听
		cbp.setOnPreferenceClickListener(this);
		etp.setOnPreferenceClickListener(this);
		lp.setOnPreferenceClickListener(this);
		//状态改变监听
		cbp.setOnPreferenceChangeListener(this);
		etp.setOnPreferenceChangeListener(this);
		lp.setOnPreferenceChangeListener(this);
	}

	/**
	 * 当监听的选项状态发生了改变的时候
	 */
	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		if (preference.getKey().equals(cbpKey)) {
			System.out.println(newValue.toString());
		}else if (preference.getKey().equals(etpKey)) {
			System.out.println(newValue.toString());
		}else if (preference.getKey().equals(lpKey)) {
			System.out.println(newValue.toString());
		}
		return true;
	}

	/**
	 * 当监听的选项被点击的时候
	 */
	@Override
	public boolean onPreferenceClick(Preference preference) {
		if (preference.getKey().equals(cbpKey)) {
			System.out.println("点击CheckBoxPreference");
		} else if (preference.getKey().equals(etpKey)) {
			System.out.println("点击EditTextPreference");
		} else if (preference.getKey().equals(lpKey)) {
			System.out.println("点击ListPreference");
		}
		return true;
	}
}

 

preferences.xml

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

    <!-- CheckBoxPreference -->
    <CheckBoxPreference
        android:defaultValue="false"
        android:key="@string/cbp_key"
        android:summaryOff="@string/cbp_summary_off"
        android:summaryOn="@string/cbp_summary_on"
        android:title="@string/cbp_title" />

    <!-- EditTextPreference -->
    <EditTextPreference
        android:defaultValue="默认值"
        android:dialogTitle="@string/etp_dialogTitle"
        android:key="@string/etp_key"
        android:summary="@string/etp_summary"
        android:title="@string/etp_title" />

    <!-- ListPreference -->
    <ListPreference
        android:dialogTitle="@string/lp_dialogTitle"
        android:entries="@array/lp_name"
        android:entryValues="@array/lp_name"
        android:key="@string/lp_key"
        android:negativeButtonText="@string/lp_btn_cancle"
        android:summary="@string/lp_summary"
        android:title="@string/lp_title" />

</PreferenceScreen>
 

array.xml

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

    <string-array name="lp_name">
        <item>曹操</item>
        <item>刘备</item>
        <item>孙权</item>
    </string-array>

</resources>

 

strings.xml

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

    <string name="app_name">PreferenceDemo</string>
    <string name="preference_title">title-比如可以叫设置</string>
    <string name="cbp_key">cbp</string>
    <string name="cbp_title">CheckBoxPreference</string>
    <string name="cbp_summary_on">开启CheckBoxPreference</string>
    <string name="cbp_summary_off">关闭CheckBoxPreference</string>
    <string name="etp_key">etp</string>
    <string name="etp_title">EditTextPreference</string>
    <string name="etp_summary">这个东东是EditTextPreference</string>
    <string name="etp_dialogTitle">修改EditTextPreference</string>
    <string name="lp_key">lp</string>
    <string name="lp_dialogTitle">请选择</string>
    <string name="lp_title">ListPreference</string>
    <string name="lp_summary">这个东东是ListPreference</string>
    <string name="lp_btn_cancle">取消</string>

</resources>
 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值