Android ImageButton处于选定状态?

如果我将ImageButton与背景选择器一起使用,是否可以更改状态以使其外观发生变化?现在,我可以按一下它来更改图像,但是似乎没有“突出显示”或“选定”或类似的状态可以让我随意切换其外观。


这是我的XML;仅在按下时会更改外观。


 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/map_toolbar_details_selected" />

<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/map_toolbar_details_selected" />

<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/map_toolbar_details_selected" />

<item android:drawable="@drawable/map_toolbar_details" />


慕雪6442864
浏览 836回答 3
3回答

手掌心

ToggleImageButton它实现Checkable接口,并支持OnCheckedChangeListener和android:checkedXML属性:public class ToggleImageButton extends ImageButton implements Checkable {&nbsp; &nbsp; private OnCheckedChangeListener onCheckedChangeListener;&nbsp; &nbsp; public ToggleImageButton(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; super(context);&nbsp; &nbsp; }&nbsp; &nbsp; public ToggleImageButton(Context context, AttributeSet attrs) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs);&nbsp; &nbsp; &nbsp; &nbsp; setChecked(attrs);&nbsp; &nbsp; }&nbsp; &nbsp; public ToggleImageButton(Context context, AttributeSet attrs, int defStyle) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs, defStyle);&nbsp; &nbsp; &nbsp; &nbsp; setChecked(attrs);&nbsp; &nbsp; }&nbsp; &nbsp; private void setChecked(AttributeSet attrs) {&nbsp; &nbsp; &nbsp; &nbsp; TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ToggleImageButton);&nbsp; &nbsp; &nbsp; &nbsp; setChecked(a.getBoolean(R.styleable.ToggleImageButton_android_checked, false));&nbsp; &nbsp; &nbsp; &nbsp; a.recycle();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean isChecked() {&nbsp; &nbsp; &nbsp; &nbsp; return isSelected();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void setChecked(boolean checked) {&nbsp; &nbsp; &nbsp; &nbsp; setSelected(checked);&nbsp; &nbsp; &nbsp; &nbsp; if (onCheckedChangeListener != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onCheckedChangeListener.onCheckedChanged(this, checked);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void toggle() {&nbsp; &nbsp; &nbsp; &nbsp; setChecked(!isChecked());&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean performClick() {&nbsp; &nbsp; &nbsp; &nbsp; toggle();&nbsp; &nbsp; &nbsp; &nbsp; return super.performClick();&nbsp; &nbsp; }&nbsp; &nbsp; public OnCheckedChangeListener getOnCheckedChangeListener() {&nbsp; &nbsp; &nbsp; &nbsp; return onCheckedChangeListener;&nbsp; &nbsp; }&nbsp; &nbsp; public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {&nbsp; &nbsp; &nbsp; &nbsp; this.onCheckedChangeListener = onCheckedChangeListener;&nbsp; &nbsp; }&nbsp; &nbsp; public static interface OnCheckedChangeListener {&nbsp; &nbsp; &nbsp; &nbsp; public void onCheckedChanged(ToggleImageButton buttonView, boolean isChecked);&nbsp; &nbsp; }}res / values / attrs.xml:<?xml version="1.0" encoding="utf-8"?><resources>&nbsp; &nbsp; <declare-styleable name="ToggleImageButton">&nbsp; &nbsp; &nbsp; &nbsp; <attr name="android:checked" />&nbsp; &nbsp; </declare-styleable></resources>

噜噜哒

没有更多图像的最佳方法是:public static void buttonEffect(View button){&nbsp; &nbsp; button.setOnTouchListener(new OnTouchListener() {&nbsp; &nbsp; &nbsp; &nbsp; public boolean onTouch(View v, MotionEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (event.getAction()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_DOWN: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v.getBackground().setColorFilter(0xe0f47521,PorterDuff.Mode.SRC_ATOP);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v.invalidate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_UP: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v.getBackground().clearColorFilter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v.invalidate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android