[英]Extending Button.class: Drawable and Text change color onTouchEvent()
這是我想出的! 有兩個重要問題:(可能相關)
- 該按鈕將按照其應有的方式運行兩次,
- 首次顯示按鈕時,上傳正確的顏色/屬性存在問題。
如何獲得包含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.