简体   繁体   English

单击后如何更改图像按钮的背景?

[英]How to change the image button background upon clicking?

I have an image button as a on/off button, i want to change the source image of it upon clicking to switch between on and off, all i have to do is to place a comparison in my click listener and a switch operation, the switch goes fine, the problem here is with the comparison, i tried like million ways and all didn't work, 我有一个图像按钮作为开/关按钮,我想在单击时更改它的源图像以在开和关之间进行切换,我要做的就是在点击侦听器和切换操作中进行比较,开关运行正常,这里的问题是比较,我尝试了数百万种方式,但都没有效果,

for example i tried: 例如我试过:

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);
            }
        }

    }
};

And also: 并且:

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

And Also: 并且:

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

And Also : 并且 :

                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);
                }

And Also: 并且:

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

And my XML : 还有我的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>

You can use selector in order to change image button / button dynamically 您可以使用选择器来动态更改图像按钮

Selector.xml Selector.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> 

Or you can to set visibilty to GONE / VISIBLE on click of a button 或者你可以设置公开程度来GONE / 可见按钮的点击

Use one flag variable like this, 这样使用一个标志变量,

    boolean flag = false; 

    public void onClick(View v) {

    if(v == power) {

        if (boolean) {
            power.setImageResource(R.drawable.acondormant);
            boolean = false;
        } else {
            power.setImageResource(R.drawable.acoffdormant);
            boolean = true;
        }

   }
};

Have you tried : 你有没有尝试过 :

power.setBackgroundResource(R.drawable.yourResource)

Also remove the background resource from xml 同时从xml中删除背景资源

remove android:src="@drawable/acoffdormant" 删除android:src="@drawable/acoffdormant"

Instead Set 改为设置

power.setBackgroundResource(R.drawable.acoffdormant) //in oncreate power.setBackgroundResource(R.drawable.acoffdormant) //在创建时

You can use the tag attribute available for imageButton .. 您可以使用imageButton可用的tag attribute

By default in your xml give the tag as off .. 默认情况下,在您的xml中,标记为off ..

<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"
                            android:tag="off" />

Then on button click: 然后在按钮上单击:

public void onClick(View v) {

String tag = power.getTag().toString().trim();

if(tag.equals("off"))
{
    power.setImageResource(R.drawable.acondormant);
    power.setTag("on");
}
else if(tag.equals("on"))
{
    power.setImageResource(R.drawable.acoffdormant);
    power.setTag("off");
}


    }
};

There are two ways of doing this. 有两种方法可以做到这一点。

B_cancelSearch.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:

        v.setBackgroundResource(R.drawable.button_pressed);

        break;
    case MotionEvent.ACTION_UP: {

        v.setBackgroundResource(R.drawable.button_normal);

    }
        break;
    }
    return false;
    }

});

And the other is by using a drawable selector as AndroidHacker mentioned in his answer. 另一种是通过使用可绘制的选择器,如AndroidHacker在其答案中提到的那样。

All you need to do is call invalidate() : 您需要做的就是调用invalidate()

power.invalidate();

or 要么

power.postInvalidate();

after the if-else block just near the end of the method. 在方法末尾附近的if-else块之后。

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

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