Android自定义SeekBarPreference

本文介绍如何在Android中自定义一个SeekBarPreference,以实现音量控制功能。通过继承DialogPreference,创建XML布局文件并声明控件属性,最终在PreferenceFragment中展示自定义布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于系统自带的Preference只支持CheckBoxPreference ListPreference editPreference等几种偏好选项,假如我们需要在Preference中添加SeekBar来实现一个音量控制条的功能,这时候就需要自定义一个类.

这里我们通过继承DialogPreference来实现.

首先定义一个xml布局文件来描述seekbar的布局

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" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="111" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="222" />
    </RelativeLayout>

    <SeekBar
        android:id="@+id/seekbar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="333" />

</LinearLayout>

接着我们需要定义2个seekbar的属性,minValue和maxValue来分别描述进度条的最小值和最大值.

我们知道自定义控件的属性需要在attrs.xml中声明:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="com.example.dialogpreferencesseekbar.SeekBarPreferences">
        <attr name="minValue" format="integer" />
        <attr name="maxValue" format="integer" />
    </declare-styleable>
</resources>

再来看看我们自定义SeekBarPreference继承于DialogPreference的类SeekBarPreferences.java:

package com.example.dialogpreferencesseekbar;


import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class SeekBarPreferences extends DialogPreference implements
		OnSeekBarChangeListener {

	private static final String PREFERENCE_NS = "http://schemas.android.com/apk/res/com.example.dialogpreferencesseekbar";
	private static final String ANDROID_NS = "http://schemas.android.com/apk/res/android";

	private static final String ATTR_DEFAULT_VALUE = "defaultValue";
	private static final String ATTR_MIN_VALUE = "minValue";
	private static final String ATTR_MAX_VALUE = "maxValue";

	private int mMinValue;
	private int mMaxValue;
	private int mDefaultValue;
	private int mCurrentValue;

	private SeekBar mSeekBar;
	private TextView mValueText;

	public SeekBarPreferences(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mMinValue = attrs
				.getAttributeIntValue(PREFERENCE_NS, ATTR_MIN_VALUE, 0);
		mMaxValue = attrs.getAttributeIntValue(PREFERENCE_NS, ATTR_MAX_VALUE,
				100);
/********也可以通过Context的obtainStyledAttributes获得自定义的属性值***********/
//		TypedArray array = context.obtainStyledAttributes(attrs,
//				R.styleable.com_example_dialogpreferencesseekbar_SeekBarPreferences);
//		mMaxValue = array.getInteger(R.styleable.com_example_dialogpreferencesseekbar_SeekBarPreferences_maxValue, 100);
//		mMinValue = array.getInteger(R.styleable.com_example_dialogpreferencesseekbar_SeekBarPreferences_minValue, 0);
		mDefaultValue = attrs.getAttributeIntValue(ANDROID_NS,
				ATTR_DEFAULT_VALUE, 50);
		
//		array.recycle();
	}

	@Override
	protected View onCreateDialogView() {
		// TODO Auto-generated method stub
		// Get current value from settings
		mCurrentValue = getPersistedInt(mDefaultValue);

		// Inflate layout
		LayoutInflater inflater = (LayoutInflater) getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.seek, null);

		// Put minimum and maximum
		((TextView) view.findViewById(R.id.text1)).setText(Integer
				.toString(mMinValue));
		((TextView) view.findViewById(R.id.text2)).setText(Integer
				.toString(mMaxValue));

		// Setup SeekBar
		mSeekBar = (SeekBar) view.findViewById(R.id.seekbar1);
		mSeekBar.setMax(mMaxValue - mMinValue);
		mSeekBar.setProgress(mCurrentValue - mMinValue);
		mSeekBar.setOnSeekBarChangeListener(this);

		// Put current value
		mValueText = (TextView) view.findViewById(R.id.text3);
		mValueText.setText(Integer.toString(mCurrentValue));

		return view;
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub
		mCurrentValue = progress + mMinValue;
		mValueText.setText(Integer.toString(mCurrentValue));

	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}

}

这里包含SeekBar的DialogPreference就自定义好了

这时我们需要定义整个Preference的布局文件了dialog_slider.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:example="http://schemas.android.com/apk/res/com.example.dialogpreferencesseekbar" >

    <PreferenceCategory android:title="第一层" >

        <com.example.dialogpreferencesseekbar.SeekBarPreferences
            android:defaultValue="25"
            android:dialogTitle="音量进度条"
            android:key="seekBarPreference"
            android:persistent="true"
            android:summary="touch it"
            android:title="音量调节"
            example:maxValue="100"
            example:minValue="0" />
    </PreferenceCategory>

    <PreferenceCategory android:title="第二层" >

        <CheckBoxPreference
            android:key="checkbox_preference"
            android:summary="this is demo"
            android:title="勾选" />
    </PreferenceCategory>

</PreferenceScreen>

包含了两层 第一层是我们上边自定义的呼出SeekBar的偏好选项 下边一层是一个CheckBoxPreference


我们PreferenceFragment将dialog_slider.xml包含进去.

MyDialogFragment.java:

package com.example.dialogpreferencesseekbar;

import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyDialogFragment extends PreferenceFragment {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.layout.dialog_slider);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return super.onCreateView(inflater, container, savedInstanceState);
	}

}

通过FragmentTransaction的replace方法就可以打开我们的自定义的偏好设置了.

效果图:

       


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值