一、创建一个TestPreference.java类,然后继承Preference,TestPreference类的构造函数中通过setLayoutResource()方法来加载布局,然后重写父类onBindViewHolder()方法,然后通过holder中holder.itemView来访问子View,示例见代码
package com.android.settings.system;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import android.content.ContentResolver;
import android.content.Context;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import com.android.settings.R;
public class TestPreferenceextends Preference implements OnSeekBarChangeListener{
private static final String TAG = "WakeupSensitivityPreference";
private static final boolean DEBUG = true;
public static final String SENSOR_LEVEL_PATH = "/sys/class/input/input1/threshold";
public static final String SENSOR_VALID_PATH = "/sys/class/input/input1/int2_enable";
private TextView summary;
private ContentResolver mResolver;
private RadioGroup mRadioGroup;
public static final String SENSOR_LEVEL = "sensor_level";
public TestPreference(Context context) {
super(context);
mResolver = context.getContentResolver();
setLayoutResource(R.layout.wakeup_test);
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
summary = holder.itemView.findViewById(R.id.sensitivity_tv);
int level = Settings.Global.getInt(mResolver, SENSOR_LEVEL, 2);
summary.setText(String.valueOf(level));
mRadioGroup = holder.itemView.findViewById(R.id.rg_wakeup_sensor);
RadioButton radioButton = (RadioButton) mRadioGroup.getChildAt(level);
if (radioButton!=null) {
radioButton.setChecked(true);
}
mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_off:
Settings.Global.putInt(mResolver, SENSOR_LEVEL, 0);
summary.setText(String.valueOf(0));
break;
case R.id.rb_low:
Settings.Global.putInt(mResolver, SENSOR_LEVEL, 1);
summary.setText(String.valueOf(1));
break;
case R.id.rb_middle:
Settings.Global.putInt(mResolver, SENSOR_LEVEL, 2);
summary.setText(String.valueOf(2));
break;
case R.id.rb_hight:
Settings.Global.putInt(mResolver, SENSOR_LEVEL, 3);
summary.setText(String.valueOf(3));
break;
default:
break;
}
setSensorLevel();
}
});
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
summary.setText(String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
Settings.Global.putInt(mResolver, SENSOR_LEVEL, progress);
}
private void setSensorLevel() {
int level =Settings.Global.getInt(mResolver, SENSOR_LEVEL, 2);
if(DEBUG) Log.i(TAG, "setSensorLevel() level = " + level);
try {
Writer levelWriter = new FileWriter(SENSOR_LEVEL_PATH);
levelWriter.write(String.valueOf(level));
levelWriter.flush();
levelWriter.close();
Writer validWriter = new FileWriter(SENSOR_VALID_PATH);
if (0 == level){
validWriter.write(String.valueOf(0));
}else {
validWriter.write(String.valueOf(1));
}
validWriter.flush();
validWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、创建一个wakeup_test.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="80dp"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:gravity="center_vertical">
<TextView
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="@string/wakeup_sensitivity"
android:textColor="@android:color/black"/>
<RadioGroup
android:id="@+id/rg_wakeup_sensor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.65"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_off"
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="18sp"
android:layout_height="wrap_content"
android:text="@string/wakeup_sensor_off"/>
<RadioButton
android:id="@+id/rb_low"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="@string/wakeup_sensor_low"/>
<RadioButton
android:id="@+id/rb_middle"
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="18sp"
android:layout_height="wrap_content"
android:text="@string/wakeup_sensor_middle"/>
<RadioButton
android:id="@+id/rb_hight"
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="18sp"
android:layout_height="wrap_content"
android:text="@string/wakeup_sensor_hight"/>
</RadioGroup>
<TextView
android:id="@+id/sensitivity_tv"
android:layout_width="0dp"
android:layout_weight="0.1"
android:layout_height="wrap_content"
android:textSize="16sp"
android:visibility="gone"
android:textColor="@android:color/black"
android:layout_marginRight="50dp"/>
</LinearLayout>
|