要在Android中比较按钮的可绘制图像,可以使用equals方法。首先,在res/drawable目录中创建两个按钮图像,例如button1.png和button2.png。然后,使用以下代码将它们转换为Drawable对象并将其设置为ImageButton视图的背景:
Resources res = getResources(); Drawable drawable1 = res.getDrawable(R.drawable.button1); Drawable drawable2 = res.getDrawable(R.drawable.button2);
ImageButton button1 = findViewById(R.id.button1); ImageButton button2 = findViewById(R.id.button2);
button1.setBackground(drawable1); button2.setBackground(drawable2);
现在可以使用equals方法比较这些Drawable对象,如下所示:
if (drawable1.equals(drawable2)) { // The two drawables are the same } else { // The two drawables are different }
这将比较两个Drawable对象并确定它们是否相同。