gpt4 book ai didi

java - 扩展Button.class : Drawable and Text change color onTouchEvent()

转载 作者:行者123 更新时间:2023-12-02 13:40:40 25 4
gpt4 key购买 nike

Here is what I came up with! There are two important problems:(might be related)

  1. It takes two clicks before the button will behave the way it should
  2. There is a problem with uploading the correct colors/attributes when the buttons first show up.

How do I get the button to be created with xml values included? I initialize it in the constructors, but there must be a better way. Any suggestions?

图片链接! 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 并相应地设置颜色即可。(顺便说一句,我很确定您只需要带有两个参数的构造函数。)

关于java - 扩展Button.class : Drawable and Text change color onTouchEvent(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42752025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com