1、自定义
package com.cn.daming.deskclock.preference; import com.cn.daming.deskclock.R; import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.graphics.drawable.Drawable; import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceClickListener; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.widget.ImageView; public class GetupPeridPreference extends ListPreference implements OnPreferenceClickListener{ private Drawable icon; private static final String SEPARATOR = "u0fly_@#asdf*&_ylf0u"; private boolean[] mClickedDialogEntryIndices; String[] weeks; public GetupPeridPreference(Context context, AttributeSet attrs) { super(context, attrs); this.setLayoutResource(R.layout.icon_list_preference); // TODO Auto-generated constructor stub icon = context.getResources().getDrawable(R.drawable.ic_launcher); weeks = GetupPeridPreference.this.getContext() .getResources().getStringArray(R.array.getup_perid_week); mClickedDialogEntryIndices = new boolean[weeks.length]; // TODO Auto-generated constructor stub } public GetupPeridPreference(Context context) { super(context); this.setLayoutResource(R.layout.icon_list_preference); // TODO Auto-generated constructor stub icon = context.getResources().getDrawable(R.drawable.ic_launcher); weeks = GetupPeridPreference.this.getContext() .getResources().getStringArray(R.array.getup_perid_week); mClickedDialogEntryIndices = new boolean[weeks.length]; // TODO Auto-generated constructor stub } public boolean[] getmClickedDialogEntryIndices() { return mClickedDialogEntryIndices; } public void setmClickedDialogEntryIndices(boolean[] mClickedDialogEntryIndices) { this.mClickedDialogEntryIndices = mClickedDialogEntryIndices; } @Override protected void onBindView(View view) { // TODO Auto-generated method stub super.onBindView(view); final ImageView imageView = (ImageView)view.findViewById(R.id.icon); if(imageView!=null &&icon!= null){ imageView.setImageDrawable(icon); } } public Drawable getIcon() { return icon; } public void setIcon(Drawable icon) { if((icon==null &&icon !=null)||(icon!=null&&!icon.equals(icon))){ this.icon = icon; notifyChanged(); } } public void setIcon(int icon) { if(icon != R.drawable.ic_launcher){ this.icon = getContext().getResources().getDrawable(icon); notifyChanged(); } } @Override public void setEntries(CharSequence[] entries) { // TODO Auto-generated method stub super.setEntries(entries); mClickedDialogEntryIndices = new boolean[weeks.length]; } @Override public boolean onPreferenceClick(Preference preference) { // TODO Auto-generated method stub return false; } }
2、
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="screen_list" android:title="标题" android:summary="说明摘要"> <com.cn.daming.deskclock.preference.GetupPeridPreference android:key="getup_perid" android:title="响铃周期" android:summary="周二" android:layout="@layout/icon_list_preference" android:entries="@array/getup_perid_array" android:entryValues="@array/getup_perid_array" android:dialogTitle="响铃周期" android:widgetLayout="@layout/sec_pref_widget_more" android:defaultValue="@array/getup_perid_array" ></com.cn.daming.deskclock.preference.GetupPeridPreference> <com.cn.daming.deskclock.preference.GetupPeridPreference android:key="getup_putoff" android:title="小睡次数" android:summary="不允许推迟" android:layout="@layout/icon_list_preference" android:entries="@array/getup_putoff_array" android:entryValues="@array/getup_putoff_array" android:dialogTitle="小睡次数" android:widgetLayout="@layout/sec_pref_widget_more" android:defaultValue="@array/getup_putoff_array" ></com.cn.daming.deskclock.preference.GetupPeridPreference> <com.cn.daming.deskclock.preference.BasePreference android:key="getup_ringtone" android:title="铃声设置" android:summary="不允许推迟"> <intent android:targetPackage="com.cn.daming.deskclock" android:targetClass="com.cn.daming.deskclock.RingSetActivity" /> </com.cn.daming.deskclock.preference.BasePreference> </PreferenceScreen>
3、
package com.cn.daming.deskclock; import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceClickListener; import android.preference.EditTextPreference; import android.preference.PreferenceActivity; import android.preference.PreferenceManager; import android.preference.PreferenceScreen; import android.util.Log; import android.view.View; import android.widget.Button; import com.cn.daming.deskclock.preference.GetupPeridPreference; public class GetupActivity extends PreferenceActivity implements OnPreferenceChangeListener,OnPreferenceClickListener { /** Called when the activity is first created. */ // Time changed flag private boolean timeChanged = false; String[] weeks; // private boolean timeScrolled = false; private static final String TAG = "PreferenceActivity"; private Context mCtx; private boolean multiChecked[]; private SharedPreferences mPreferences; private GetupPeridPreference perpreference; private GetupPeridPreference putoffference; private RepeatPreference mRepeatPref; private EditTextPreference mLabel; private AlarmPreference mAlarmPref; private int mId; private int mHour; private int mMinutes; private boolean mTimePickerCancelled; private Alarm mOriginalAlarm; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ly_getup); addPreferencesFromResource(R.xml.getup_settings); weeks = getResources().getStringArray(R.array.getup_perid_week); multiChecked = new boolean[weeks.length]; initPre(); } private void initPre(){ PreferenceManager manager = getPreferenceManager(); mPreferences = manager.getSharedPreferences(); perpreference = (GetupPeridPreference) manager.findPreference("getup_perid"); perpreference.setIcon(R.drawable.alarm_icon_credit); perpreference.setOnPreferenceChangeListener(this); perpreference.setSummary(perpreference.getEntryValues()[0]); putoffference = (GetupPeridPreference) manager.findPreference("getup_putoff"); putoffference.setIcon(R.drawable.alarm_icon_credit); putoffference.setOnPreferenceChangeListener(this); putoffference.setSummary(putoffference.getEntryValues()[0]); } @Override public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { return super.onPreferenceTreeClick(preferenceScreen, preference); } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { // TODO Auto-generated method stub String tag = preference.getKey(); if("getup_perid".equals(tag)){ String value = mPreferences.getString("getup_perid", "自定义周期"); if("自定义周期".equals(newValue)){ Builder builder = new Builder(GetupActivity.this); builder.setMultiChoiceItems(weeks, multiChecked,new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // TODO Auto-generated method stub multiChecked[which] = isChecked; } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub StringBuffer summary = new StringBuffer(); for(int i=0;i<weeks.length;i++){ if(multiChecked[i]){ summary.append(weeks[i]).append(""); } } perpreference.setSummary(summary); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub for(int i=0;i<multiChecked.length;i++){ multiChecked[i] = false; } } } ); builder.setTitle("响铃周期"); builder.create().show(); }else { perpreference.setSummary(newValue.toString()); } }else if("getup_putoff".equals(tag)){ putoffference = (GetupPeridPreference)preference; putoffference.setSummary(newValue.toString()); } return true;//表示将新写入的值存入sharedpreference } @Override public boolean onPreferenceClick(Preference preference) { // TODO Auto-generated method stub return false; } }
(在F:\java\ctdesktop2\DeskClockApp-4-10)