java - 单击时如何更改图像按钮背景?

标签 java android imagebutton

我有一个图像按钮作为开/关按钮,我想在点击时更改它的源图像以在开和关之间切换,我所要做的就是在我的点击监听器中放置一个比较和一个开关操作,开关正常,这里的问题是比较,我尝试了百万种方法但都没有用,

例如我试过:

public void onClick(View v) {

        if (v == power) {

             Drawable fDraw = power.getBackground();
             Drawable sDraw = getResources().getDrawable(R.drawable.acoffdormant);

             Bitmap bitmap = ((BitmapDrawable)fDraw).getBitmap();
             Bitmap bitmap2 = ((BitmapDrawable)sDraw).getBitmap();

            if (bitmap == bitmap2) {
                power.invalidateDrawable(getResources().getDrawable(R.drawable.acoffdormant));
                power.setImageResource(R.drawable.acondormant);
            } else {
                power.invalidateDrawable(getResources().getDrawable(R.drawable.acondormant));
                power.setImageResource(R.drawable.acoffdormant);
            }
        }

    }
};

还有:

if (getResources().getDrawable(R.drawable.acoffdormant).hashCode() == power.getBackground().hashCode())

还有:

v.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.acoffdormant).getConstantState())

还有:

                Drawable state = power.getBackground();
                Bitmap statebit = ((BitmapDrawable)state).getBitmap();
                Bitmap off = BitmapFactory.decodeResource(power.getResources(),
                        R.drawable.acoffdormant);

                if (statebit == off) {
                    power.invalidateDrawable(getResources().getDrawable(R.drawable.acoffdormant));
                    power.setImageResource(R.drawable.acondormant);
                } else {
                    power.invalidateDrawable(getResources().getDrawable(R.drawable.acondormant));
                    power.setImageResource(R.drawable.ac_fan);
                }

还有:

        if (power.getBackground().getConstantState().equals
                (getResources().getDrawable(R.drawable.acoffdormant).getConstantState()))

还有我的XML:

    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:layout_marginBottom="10dp"
                            android:layout_marginTop="20dp"
                            android:background="@android:color/transparent"
                            android:text="@string/power"
                            android:textColor="#00CCCC"
                            android:textSize="20dp"
                            android:textStyle="bold" />

                        <ImageButton
                            android:id="@+id/power"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:layout_weight="1"
                            android:clickable="true"
                            android:adjustViewBounds="true"
                            android:background="@android:color/transparent"
                            android:scaleType="centerInside"
                            android:src="@drawable/acoffdormant" />
</LinearLayout>

最佳答案

您可以使用选择器来动态更改图像按钮/按钮

选择器.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:state_selected="true" android:drawable="@drawable/existing_image" />
    <item android:drawable="@drawable/new_image" />
</selector> 

或者您可以通过点击按钮将可见性设置为GONE/VISIBLE

关于java - 单击时如何更改图像按钮背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20349894/

相关文章:

java - 事务回滚异常

java - junit 的可执行 jar 文件

java - 如何将图像从 URL 转换为十六进制字符串?

Java8 Effectively Final compile time error on non final variable

android - 如何检测手指何时从 ImageButton 上滑落?

android - 无法在 Android 的简单适配器中设置评级栏

android - 在 samsung s2 上测试应用程序时获取 Resources$NotFoundException

Android开发LCD字体

android - imagebutton 给我错误 : [Accessibility] Missing contentDescription attribute on image

Silverlight 或 WP7 图像按钮