获取图像的可绘制按钮 [英] Get drawable of image button

查看:62
本文介绍了获取图像的可绘制按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得图像按钮的可绘制对象以进行比较,如果可绘制对象为A,则为B?非常感谢.

How can I get the drawable of an Image Button to compare and do something if the drawable is A and something if is B?. Thank you so much.

    switch(getDrawableId(buttonRepeat)) {

        case R.drawable.a:
            mediaPlayer.setLooping(true);

             break;
        case R.drawable.b:
                mediaPlayer.setLooping(false);

             break;
        default:

        break;
          }

推荐答案

在ImageButton中使用getDrawable()方法,并使用.getConstantState().equals()

Use getDrawable() method in ImageButton and compare them using .getConstantState().equals()

示例代码:

ImageButton btn = (ImageButton) findViewById(R.id.myImageBtn);
Drawable drawable = btn.getDrawable();
if (drawable.getConstantState().equals(getResources().getDrawable(R.drawable.myDrawable).getConstantState())){
   //Do your work here
}

参考文献:

http://developer.android.com/reference/android/widget/ImageButton.html

在android中比较两个可绘制对象

这篇关于获取图像的可绘制按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆