Android Button实现Checkable不会改变drawable

时间:2016-05-05 16:22:43

标签: android button checkbox

我必须创建一组代表星期几且可以检查的视图,以选择和取消选择当天的可用性。 Days buttons layout

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

问题是点击时它不会改变drawable。我是否需要在监听器中指定它必须更改drawable?它还不是一个Checkable属性?

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

CheckableButton

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>

0 个答案:

没有答案