Android 10 Settings应用如何自定义Preference

一、创建一个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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义Preference的布局,可以按照以下步骤操作: 1.创建一个XML布局文件,该文件应包含您希望在Preference中显示的UI元素。例如,如果您希望显示一个文本框和一个开关,可以创建一个包含这些元素的布局文件。 2.在Preference类的子类中,覆盖onCreateView()方法。在这个方法中,使用LayoutInflater加载您的布局文件。 3.在onCreateView()方法中,使用findViewByld()方法获取您的UI元素的引用,并设置它们的值和行为(例如,将文本框的文本设置为Preference存储的值)。 4.如果您希望在用户更改Preference值时更新UI元素,请覆盖onPreferenceChange()方法并在其中更新您的UI元素。 下面是一个示例代码: ```java public class MyPreference extends Preference { private EditText mEditText; private Switch mSwitch; public MyPreference(Context context, AttributeSet attrs) { super(context, attrs); } @Override public View onCreateView(ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.my_preference_layout, parent, false); mEditText = (EditText) view.findViewById(R.id.edit_text); mSwitch = (Switch) view.findViewById(R.id.switch_button); mEditText.setText(getPersistedString("default value")); mSwitch.setChecked(getPersistedBoolean(false)); return view; } @Override public void onPreferenceChange(Preference preference, Object newValue) { if (preference == this) { mEditText.setText((String) newValue); mSwitch.setChecked((Boolean) newValue); } } } ``` 在这个例子中,我们创建了一个名为my_preference_layout.xml的布局文件,其中包含一个EditText和一个Switch。然后,我们在MyPreference类中覆盖了onCreateView()方法来加载这个布局文件,查找EditText和Switch元素,并设置它们的值和行为。我们还覆盖了onPreferenceChange()方法来更新UI元素,以反映Preference值的更改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值