运行截图:
java代码:
package cn.mrzhu.test23; import android.os.Bundle; import android.preference.PreferenceActivity; /** * 继承PreferenceActivity,设置布局 * @author ZLQ * */ public class Main extends PreferenceActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.myseek); } }
自定义控件代码:
package cn.mrzhu.test23; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.preference.DialogPreference; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.CheckBox; import android.widget.SeekBar; /** * 继承DialogPreference,自定义SeekBarPreference控件 * @author ZLQ * */ public class MySeekBarPreference extends DialogPreference{ Context con; SeekBar sRingtone,sMedia,sAlarm; CheckBox cNotifications; public MySeekBarPreference(Context context, AttributeSet attrs) { super(context, attrs); //在构造器中为con赋值 con = context; } /** * 在对话框弹出时执行取数据并给进度条和复选框设置值的操作 */ @Override protected View onCreateDialogView() { //通过上下方取得SharedPreferences对象 SharedPreferences sharedPreferences = con.getSharedPreferences("seek", Context.MODE_PRIVATE); //从seek.xml文件中取出之前保存的数据,如之前没有保存过数据,则默认进度为50,checkbox默认为true int ringtone = sharedPreferences.getInt("ringtone", 50); int media = sharedPreferences.getInt("media", 50); int alarm = sharedPreferences.getInt("alarm", 50); boolean isChecked = sharedPreferences.getBoolean("notifications", true); //通过转换器将布局转换成View对象并return View view = LayoutInflater.from(con).inflate(R.layout.seek, null); //取得进度条和复选框对象 sRingtone = (SeekBar) view.findViewById(R.id.ringtone); sMedia = (SeekBar) view.findViewById(R.id.media); sAlarm = (SeekBar) view.findViewById(R.id.alarm); cNotifications = (CheckBox) view.findViewById(R.id.notifications); //将从seek.xml文件中取出的数据设置给进度条和复选框 ,如之前没有保存过数据,则默认进度为50,checkbox默认为true sRingtone.setProgress(ringtone); sMedia.setProgress(media); sAlarm.setProgress(alarm); cNotifications.setChecked(isChecked); return view; } /** * 在点击对话框ok按钮时取得进度条的值和复选框的状态,保存到seek.xml文件中 */ @Override protected void onDialogClosed(boolean positiveResult) { //positiveResult为true则表示点击了ok按钮 if(positiveResult){ //取得进度条当前的进度和复选框的状态 int ringtone = sRingtone.getProgress(); int media = sMedia.getProgress(); int alarm = sAlarm.getProgress(); boolean isChecked = cNotifications.isChecked(); SharedPreferences sharedPreferences = con.getSharedPreferences("seek", Context.MODE_PRIVATE); Editor editor = sharedPreferences.edit(); editor.putInt("ringtone", ringtone); editor.putInt("media", media); editor.putInt("alarm", alarm); editor.putBoolean("notifications", isChecked); //提交数据 editor.commit(); } super.onDialogClosed(positiveResult); } }
主界面myseek.xml代码:右键layou,新建Android XML File文件,选择Preference,在res文件夹下会自动生成xml文件夹
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 因为是自定义的控件,所以必须是包名点类名,否则系统找不到此控件 --> <cn.mrzhu.test23.MySeekBarPreference android:key="seek" android:dialogIcon="@drawable/icon" android:title="System Sound" /> </PreferenceScreen>
弹出的对话框里的布局seek.xml的代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView style="@style/text" android:text="Ringtone" /> <SeekBar android:id="@+id/ringtone" style="@style/seek" /> <TextView style="@style/text" android:text="Media" /> <SeekBar android:id="@+id/media" style="@style/seek" /> <TextView style="@style/text" android:text="Alarm" /> <SeekBar android:id="@+id/alarm" style="@style/seek" /> <CheckBox android:id="@+id/notifications" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:checked="true" android:text="Use incoming call volume for notifications" /> </LinearLayout>
values文件夹下style.xml代码:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="text"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:paddingLeft">15dp</item> </style> <style name="seek" parent="@style/text"> <item name="android:paddingRight">15dp</item> <item name="android:paddingTop">5dp</item> <item name="android:paddingBottom">5dp</item> </style> </resources>