繁体   English   中英

扩展Button.class:Drawable和Text更改onTouchEvent()的颜色

[英]Extending Button.class: Drawable and Text change color onTouchEvent()

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

  1. 该按钮将按照其应有的方式运行两次,
  2. 首次显示按钮时,上传正确的颜色/属性存在问题。

如何获得包含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"
            />

简单修复

这个

isSelected = !isSelected;

必须先来

updateColor(isSelected);

切换按钮

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM