扩展Button.class:可绘制和文本更改颜色onTouchEvent()

时间:2017-03-12 19:26:34

标签: java android button android-custom-view

  

这是我想出来的! 有两个重要问题:   (可能是相关的)

     
      
  1. 按钮的行为方式需要两次点击
  2.   
  3. 首次显示按钮时,上传正确的颜色/属性时出现问题。
  4.         

    如何使用包含的xml值创建按钮?我在构造函数中初始化它,但必须有更好的方法。 有任何建议吗?

图片链接! https://plus.google.com/110648189687088745317/posts/4eR1Yn6jA2S

public class SmartButton extends android.support.v7.widget.AppCompatButton {

    Drawable[] drawables;
    boolean DEFAULT_STATE;

    int COLOR_SELECTED;
    int COLOR_UNSELECTED;

    boolean isSelected;
    TypedArray a;

    public SmartButton(Context context) {
        super(context);
        a = context.obtainStyledAttributes(R.styleable.SmartButton);
        initialize();
    }

    public SmartButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SmartButton, 0, 0);
        initialize();
    }

    public SmartButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SmartButton, 0, 0);
        initialize();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            updateState();
        }

        return super.onTouchEvent(event);
    }

    /**
     * ALL THREE (DEFAULT_STATE, COLOR_SELECTED, COLOR_UNSELECTED) can be overridden in XML
     *
     * DEFAULT_STATE is Unselected, if not specified in XML
     * COLOR_SELECTED is the color to be used in the Button's Selected State. Shows icon & text color with BLACK tint, if not specified in XML
     * COLOR_UNSELECTED is the color to be used in the Button's Unselected State. Shows icon & text color with a LIGHT GRAY tint
     */
    private void initialize() {
        try {
            drawables = getCompoundDrawables();
            DEFAULT_STATE = a.getBoolean(R.styleable.SmartButton_defaultState, false);
            COLOR_SELECTED = a.getColor(R.styleable.SmartButton_colorSelected, Color.BLACK);
            COLOR_UNSELECTED = a.getColor(R.styleable.SmartButton_colorUnselected, Color.LTGRAY);

            isSelected = DEFAULT_STATE;
            updateColor(isSelected);
        } finally {
            a.recycle();
        }
    }

    private void updateState() {

        updateColor(isSelected);

        isSelected = !isSelected;
    }

    private void updateColor(boolean isSelected) {

        int currentColor;

        if (isSelected) {
            currentColor = COLOR_SELECTED;
        } else {
            currentColor = COLOR_UNSELECTED;
        }

        if (getText() != null)
            setTextColor(currentColor);

        for (int x = 0; x < drawables.length; x++) {
            if (drawables[x] != null)
                drawables[x].setColorFilter(currentColor, PorterDuff.Mode.SRC_ATOP);
        }

        invalidate();
    }
}

SmartButton的属性资源文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="SmartButton">
            <attr name="colorSelected" format="color"/>
            <attr name="colorUnselected" format="color"/>
            <attr name="defaultState" format="boolean"/>
        </declare-styleable>
</resources>

如何在xml中创建

<com.apotheoking.SmartButton
            android:id="@+id/smart_button_example"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:drawableTop="@drawable/your_drawable"
            android:text = "your text here"

            app:defaultState="true";
            app:colorSelected="@color/yourColorSelected"
            app:colorUnselected="@color/yourColorUnselected"
            />

1 个答案:

答案 0 :(得分:1)

简单修复

isSelected = !isSelected;

必须来

updateColor(isSelected);

切换按钮

Android提供了一个可以扩展的ToggleButton。只需听取onCheckedChanged并相应地设置颜色。 (顺便说一下。我很确定你只需要带有两个参数的构造函数。)