Android ImageButton-确定当前设置的资源
是否有一种方法可以在任何给定时间找到特定ImageButton设置的资源 例如:我有一个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
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);