安卓如何获取ImageButton的背景资源

3

我需要获取一些ImageButton的背景资源,以便稍后重复使用。为了设置背景,我使用了setBackgroundResource方法。但我找不到任何获取背景资源的方法。

private void AddImage(int img){
    ImageButton imgact1 = (ImageButton)findViewById(R.id.imgact1);
    ImageButton imgact2 = (ImageButton)findViewById(R.id.imgact2);
    ImageButton imgact3 = (ImageButton)findViewById(R.id.imgact3);
    imgact3.setBackgroundResource(img);
}

感谢您的帮助。

http://developer.android.com/reference/android/widget/ImageButton.html - Raghunandan
1个回答

4

对于任何 View,您应该使用 getBackground(),它返回一个 Drawable


@Da-monk,不用谢。如果你觉得有帮助,请不要忘记接受答案。 - Egor
我该如何将该值存储在字符串中?如果图像是在android:src中定义的,我能做同样的事情吗?谢谢!附言:我已经点赞了。 - Ruchir Baronia
这是因为我正在尝试比较值。例如:如果图像是“红色圆圈”,则执行此操作,如果图像是“蓝色圆圈”,则执行此操作。 - Ruchir Baronia
我已经尝试过 if(buttonBackground.equals(@drawable/mydrawable)) 但它不起作用。这就是 Drawable buttonBackground = myButton.getBackground(); 的作用。 - Ruchir Baronia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接