天天看点

[SwitchPreference]代码中动态修改SwitchPreference的Thumb或Track颜色

所有自定义代码在下面▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼

逻辑就是自定义个class继承SwitchPreference,拿到switch,给switch设置checkChange监听就可以了;

      解释下为何holder.findViewById(16908352)中id是16908352呢?因为我们是继承自support-v14中的SwitchPreference,(注意:不是android.preference.SwitchPreference,这个已经被弃用了),查看SwitchPreference中onBindViewHoldr的代码,它的Switch是局部变量,所以我们是无法继承的,但已知switch的id是16908352,所以我们直接拿过来用就可以了,不用纠结它的id常量名是什么.

[SwitchPreference]代码中动态修改SwitchPreference的Thumb或Track颜色
package com.android.MySettings.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.PreferenceViewHolder;
import android.util.AttributeSet;
import android.widget.CompoundButton;
import android.widget.Switch;

/**
 * When I wrote this,only God and I understood what I was doing.
 * Now, God only knows.
 *
 * @fileName:MySwitchPreference
 * @author:86132
 * @date:2022/12/15 17:56
 */
public class MySwitchPreference extends SwitchPreference {

    private Switch mSwitch;
    private SharedPreferences mSharedPreferences;

    public MySwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public MySwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MySwitchPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MySwitchPreference(Context context) {
        super(context);
    }


    @SuppressLint("ResourceType")
    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        mSwitch = (Switch) holder.findViewById(16908352);
        if(null != mSwitch)
        {
            changeColor(mSwitch.isChecked(),mSwitch.isEnabled());
            mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    changeColor(isChecked,mSwitch.isEnabled());
                }
            });
        }
    }

    private void changeColor(boolean checked, boolean enabled){
        try {
            mSharedPreferences = getContext().getSharedPreferences("settings_data",Context.MODE_PRIVATE);
            int thumbCheckedColor = Color.parseColor("#33ff99");
            int thumbUncheckedColor = Color.parseColor("#ECECEC");
/*            int trackCheckedColor = mSharedPreferences.getInt("theme_color_key",Color.parseColor("#3F51B5"));
            int trackUncheckedColor = Color.parseColor("#B9B9B9");*/
            if(enabled){
                mSwitch.getThumbDrawable().setColorFilter(checked ? thumbCheckedColor : thumbUncheckedColor, PorterDuff.Mode.MULTIPLY);
//                mSwitch.getTrackDrawable().setColorFilter(checked ? trackCheckedColor : trackUncheckedColor, PorterDuff.Mode.MULTIPLY);
            }else {
                mSwitch.getThumbDrawable().setColorFilter(Color.parseColor("#B9B9B9"), PorterDuff.Mode.MULTIPLY);
//                mSwitch.getTrackDrawable().setColorFilter(Color.parseColor("#E9E9E9"), PorterDuff.Mode.MULTIPLY);
            }
        }catch (NullPointerException e){
            e.printStackTrace();
        }
    }
    
}
           

继续阅读