运行截图:

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;}/** * 在对话框弹出时执行取数据并给进度条和复选框设置值的操作 */@Overrideprotected View onCreateDialogView() {//通过上下方取得SharedPreferences对象SharedPreferences sharedPreferences = con.getSharedPreferences("seek", Context.MODE_PRIVATE);//从seek.xml文件中取出之前保存的数据,如之前没有保存过数据,则默认进度为50,checkbox默认为trueint ringtone = sharedPreferences.getInt("ringtone", 50);int media = sharedPreferences.getInt("media", 50);int alarm = sharedPreferences.getInt("alarm", 50);boolean isChecked = sharedPreferences.getBoolean("notifications", true);//通过转换器将布局转换成View对象并returnView 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默认为truesRingtone.setProgress(ringtone);sMedia.setProgress(media);sAlarm.setProgress(alarm);cNotifications.setChecked(isChecked);return view;}/** * 在点击对话框ok按钮时取得进度条的值和复选框的状态,保存到seek.xml文件中 */@Overrideprotected 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.MySeekBarPreferenceandroid: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>


更多相关文章

  1. android原生SQLite数据库的使用
  2. Android自动化测试
  3. android Dialog弹在底部核心代码
  4. android webview js交互 (java和js交互)
  5. android 设置缓存工具类
  6. android 代码混淆之后 微信分享不起作用
  7. Android(安卓)近百个项目的源代码,覆盖Android开发的每个领域
  8. Android(安卓)手势滑动,多点触摸放大缩小图片
  9. 在android 中开发java.net.SocketException: socket failed: EAC

随机推荐

  1. Android小游戏-数独(附源码)
  2. android 获取经纬度
  3. (1) Android-5.0源码分析计划
  4. Android中得Static关键字
  5. 【整理】Android中使用XML自定义组件各种
  6. Android(安卓)Native 程序逆向入门(一)——
  7. 未来谁能引领Android大势
  8. php学习小结(php程序的运行原理、短标签的
  9. Android(安卓)代理模式
  10. Android(安卓)- 智能 wifi 插座控制开关