Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ImageButton-确定当前设置的资源_Android_Resources_Imagebutton_Drawable - Fatal编程技术网

Android ImageButton-确定当前设置的资源

Android ImageButton-确定当前设置的资源,android,resources,imagebutton,drawable,Android,Resources,Imagebutton,Drawable,是否有一种方法可以在任何给定时间找到特定ImageButton设置的资源 例如:我有一个ImageButton,在创建时设置为R.drawable.btn_。稍后,ImageButton会被设置为R.drawable.btn\u off。我希望能够检查ImageButton在我的代码中设置为什么资源 谢谢 Chris我不知道如何直接访问资源,但是对于您试图实现的目标,仅仅获取状态就足够了吗 ImageButton btn = (ImageButton) findViewById(R.id

是否有一种方法可以在任何给定时间找到特定ImageButton设置的资源

例如:我有一个ImageButton,在创建时设置为
R.drawable.btn_。稍后,ImageButton会被设置为
R.drawable.btn\u off
。我希望能够检查ImageButton在我的代码中设置为什么资源

谢谢
Chris

我不知道如何直接访问资源,但是对于您试图实现的目标,仅仅获取状态就足够了吗

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

您可以将自己的类定义为
ImageButton
的子类,添加一个私有int变量,并在调用
setImageResource(int)
时进行设置。比如:

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;
    }
}
我没有对它进行测试,但是你明白了-然后你可以在你的按钮上调用getImageResource(),假设它以前是用setImageResource()设置的。

只需使用
setTag()
getTag()
来关联和检索你的
ImageView
上的文档是不正确的。这里它声明
pickdropoint.getDrawableState()[android.R.attr.state\u pressed]
返回
true
false
,但它返回
1
0
,一个
**int**

为了让它工作,我必须做以下几件事

            <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" />

也就是说,我同意slup——如果你想测试你的按钮的状态,有更多合乎逻辑的方法。
<?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>
whichPoint = (pickDropPoint.getDrawableState()[android.R.attr.state_pressed] > 1 ? PICKUP : DROP);