Android 自定義SeekBarPreference

在 PreferenceScreen 佈局裏面,做Android系統應用的,比如Settings模塊,會用到PreferenceScreen,(可以先學習一下:https://blog.csdn.net/sk719887916/article/details/42437253?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task)
但是Android又沒有PreferenceScreen控件,所以就必須自定義:
seekbar.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">

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop = "400dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="10dp"
        android:max="100"
        android:progress="60"></SeekBar>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="TextView"></TextView>
</LinearLayout>

在所需的PreferenceScreen 中引用:
sound.xml

<?xml version="1.0" encoding="utf-8"?><
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/device_sound">

    <SeekBarPreference
        android:dialogLayout="@layout/seekbar"
        android:dialogTitle="@string/volume_treble"
        android:key="string/volume_treble"
        android:title="@string/volume_treble"></SeekBarPreference>


</PreferenceScreen>

自定義控件SeekBarPreference.java



package com.android.tv.settings.device.sound;

import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import com.android.tv.settings.R;


public class SeekBarPreference extends DialogPreference implements
        OnSeekBarChangeListener {
    private SeekBar seekBar;
    private TextView textView;
    private Context mContext;

    public SeekBarPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setLayoutResource(R.layout.seekbar);

    }


    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        seekBar = (SeekBar) view.findViewById(R.id.seekBar1);
        textView = (TextView) view.findViewById(R.id.textView1);
        seekBar.setOnSeekBarChangeListener(this);
    }


    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            Log.i("Dialog closed", "You click positive button");
        } else {
            Log.i("Dialog closed", "You click negative button");
        }
    }


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        textView.setText(progress + "% " + progress + "/100");
    }


    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }


    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章