开发者

Android ImageButton - determine what resource is currently set

开发者 https://www.devze.com 2023-01-05 00:49 出处:网络
Is there a way I can find what resource a particular ImageButton is set to, at any given time? For eg: I have an ImageButton that I set to R.drawable.btn_on onCreate. Later, at some point, the ImageB

Is there a way I can find what resource a particular ImageButton is set to, at any given time?

For eg: I have an ImageButton that I set to R.drawable.btn_on onCreate. Later, at some point, the ImageButton gets set to R.drawable.btn_off. I want to be able to check what resource the ImageButton is set to in my code.

Thanks C开发者_开发百科hris


Just use setTag() and getTag() to associate and retrieve custom data for your ImageView.


You could define your own class as a child of ImageButton, add a private int variable and set it when setImageResource(int) is called. Something like:

public class MyImageButton extends ImageButton {

    private int mImageResource = 0;

    @Override
    public void setImageResource (int resId) {
        mImageResource = resId;
        super.setImageResource(resId);
    }

    public int getImageResource() {
        return mImageResource;
    }
}

I didn't test it, but you get the idea - then you can call getImageResource() on your button, assuming it has been previously set with setImageResource().


I don't know how to access the resource directly, but for what you try to achieve, wouldn't it suffice to just get the state?

    ImageButton btn = (ImageButton) findViewById(R.id.btn);

    int [] states = btn.getDrawableState();
    for (int i : states) {
        if (i == android.R.attr.state_pressed) {
            Log.v("btn", "Button in pressed state");
        }
    }

http://developer.android.com/reference/android/R.attr.html#state_pressed


The documentation on android docs is incorrect. Here it states that pickDropPoint.getDrawableState()[android.R.attr.state_pressed] returns true or false, but instead it returns 1 or 0, an **int**.

I had to do the following to get it working

            <ImageButton
            android:id="@+id/pickDropPoint"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="6"
            android:background="#EEeeeee4"
            android:contentDescription="pick or drop point"
            android:src="@drawable/pickupdrop" />

The drawable xml for pressed feel

<?xml version="1.0" encoding="utf-8"?>

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

    <item android:src="@drawable/start32" android:state_pressed="true"/>
    <item android:src="@drawable/end32" android:state_pressed="false"/>
    <corners
        android:bottomLeftRadius="3dp"
        android:bottomRightRadius="3dp"
        android:topLeftRadius="3dp"
        android:topRightRadius="3dp" />

</selector>

In code, you dont need the for loop as suggested by @slup

whichPoint = (pickDropPoint.getDrawableState()[android.R.attr.state_pressed] > 1 ? PICKUP : DROP);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号