标签:checkbox复选框
<strong>代码很少很简单,不过还是写的详细点吧</strong> 1.Activity继承PreferenceActivity public class MainActivity extends PreferenceActivity { private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.my_preference); context = this; CheckBoxPreference mCheckBoxPreference0 = (CheckBoxPreference) findPreference("cbp0"); CheckBoxPreference mCheckBoxPreference1 = (CheckBoxPreference) findPreference("cbp1"); // item点击事件 mCheckBoxPreference0 .setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { // TODO Auto-generated method stub // 这里可以监听到这个CheckBox 的点击事件 Toast.makeText(context, preference.getTitle() + "被点击了", Toast.LENGTH_SHORT).show(); return true; } }); // 可获取checkbox中的值 mCheckBoxPreference0 .setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { // TODO Auto-generated method stub // 这里可以监听到checkBox中值是否改变了,并且可以拿到新改变的值 true/false Toast.makeText(context, preference.getTitle()+"的值改变为" +String.valueOf(newValue), Toast.LENGTH_SHORT).show(); return true; } }); } 2.主布局: my_checkbox.xml <?xml version="1.0" encoding="utf-8"?> <CheckBox xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+android:id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/checkbox_checked_style" android:clickable="false" android:focusable="false" /> 3.在res中建drawable文件,放checkbox_checked_style.xml资源 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/capture_tips_ng" android:state_checked="false"/> <item android:drawable="@drawable/capture_tips_g" android:state_checked="true"/> </selector> 4.在res中新建xml文件,存放my_preference.xml 资源 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <CheckBoxPreference android:key="cbp0" android:summaryOff="Off" android:summaryOn="你好" android:title="第1个" android:widgetLayout="@layout/my_checkbox" /> </PreferenceScreen>
标签:checkbox复选框
原文地址:http://blog.csdn.net/wuxin782515516/article/details/46517633