Android按钮实现可检查但不更改可绘制

Android按钮实现可检查但不更改可绘制,android,button,checkbox,Android,Button,Checkbox,我必须创建一组表示一周天数的视图,这些视图是可检查的,以选择和取消选择当天的可用性 我创建了一个自定义视图,它扩展了按钮并实现了Checkable。我为它指定了一个可绘制的选择器,以便在选中时更改颜色 问题是,单击时,它不会更改drawable。我是否需要在侦听器中指定它必须更改drawable?它还不是可检查的财产 我不能使用任何其他可检查的视图,比如Toggle、ChechedTextView,因为我需要这个视图的特殊样式 可检查按钮 布局部分 选择器 您的代码是来自android/widg

我必须创建一组表示一周天数的视图,这些视图是可检查的,以选择和取消选择当天的可用性

我创建了一个自定义视图,它扩展了按钮并实现了Checkable。我为它指定了一个可绘制的选择器,以便在选中时更改颜色

问题是,单击时,它不会更改drawable。我是否需要在侦听器中指定它必须更改drawable?它还不是可检查的财产

我不能使用任何其他可检查的视图,比如Toggle、ChechedTextView,因为我需要这个视图的特殊样式

可检查按钮

布局部分

选择器


您的代码是来自android/widget/CheckedTextView.java的某种形式的^C/^V吗?如果是这样的话,您是否完全按照原样进行了尝试,没有进行任何更改,然后应用您的更改?@pskink我遵循了本教程:只需在按钮中更改LinearLayout,即可获得完整的类代码。我不需要关于传播子状态更改的部分。对不起,我不知道你在说什么mean@pskink我的评论不清楚吗?我在上一篇评论中的链接中遵循了该教程,只是将其调整为一个按钮。但它不起作用。好吧,我拿了你的代码。。。。它很简单,只需通过调用setOnClickListener设置click listener,并在onClick call Toggle中设置您的代码是来自android/widget/CheckedTextView.java的某种形式的^C/^V吗?如果是这样的话,您是否完全按照原样进行了尝试,没有进行任何更改,然后应用您的更改?@pskink我遵循了本教程:只需在按钮中更改LinearLayout,即可获得完整的类代码。我不需要关于传播子状态更改的部分。对不起,我不知道你在说什么mean@pskink我的评论不清楚吗?我在上一篇评论中的链接中遵循了该教程,只是将其调整为一个按钮。但它不起作用。好吧,我拿了你的代码。。。。它很简单,只需通过调用setOnClickListener和内部onClick调用切换来设置click侦听器
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.Checkable;

public class CheckableButton extends Button implements Checkable {


private static final int[] CHECKED_STATE_SET =   {android.R.attr.state_checked};

private boolean mChecked = false;

private OnCheckedChangeListener mOnCheckedChangeListener;

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

public boolean isChecked() {
    return mChecked;
}

public void setChecked(boolean b) {
    if (b != mChecked) {
        mChecked = b;
        refreshDrawableState();

        if (mOnCheckedChangeListener != null) {
            mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
        }
    }
}

public void toggle() {
    setChecked(!mChecked);
}

@Override
public int[] onCreateDrawableState(int extraSpace) {
    final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
    if (isChecked()) {
        mergeDrawableStates(drawableState, CHECKED_STATE_SET);
    }
    return drawableState;
}


public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
    mOnCheckedChangeListener = listener;
}

public static interface OnCheckedChangeListener {

    void onCheckedChanged(View checkableView, boolean isChecked);
}
}
 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal">


            <eu.ecomind.pitagora.utils.CheckableButton
                android:id="@+id/mon"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/day_selection"
                android:gravity="center"
                android:text="L"
                android:textColor="@color/text_background_dark_customer" />

            <eu.ecomind.pitagora.utils.CheckableButton
                android:id="@+id/tue"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/day_selection"
                android:gravity="center"
                android:text="M"
                android:textColor="@color/text_background_dark_customer" />

            <eu.ecomind.pitagora.utils.CheckableButton
                android:id="@+id/wed"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/day_selection"
                android:gravity="center"
                android:text="M"
                android:textColor="@color/text_background_dark_customer" />

            <eu.ecomind.pitagora.utils.CheckableButton
                android:id="@+id/thu"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/day_selection"
                android:gravity="center"
                android:text="G"
                android:textColor="@color/text_background_dark_customer" />

            <eu.ecomind.pitagora.utils.CheckableButton
                android:id="@+id/fri"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/day_selection"
                android:gravity="center"
                android:text="V"
                android:textColor="@color/text_background_dark_customer" />

            <eu.ecomind.pitagora.utils.CheckableButton
                android:id="@+id/sat"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/day_selection"
                android:gravity="center"
                android:text="S"
                android:textColor="@color/text_background_dark_customer" />

            <eu.ecomind.pitagora.utils.CheckableButton
                android:id="@+id/sun"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/day_selection"
                android:gravity="center"
                android:text="D"
                android:textColor="@color/text_background_dark_customer" />



        </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/day_selected"
    android:state_checked="true"/>
<item android:drawable="@drawable/day_not_selected"/>
</selector>