【问题标题】:How do I compare a background Image resource TextView with a R.drawable.bg_image for switch如何将背景图像资源 TextView 与 R.drawable.bg_image 进行比较以进行切换
【发布时间】:2011-09-12 16:13:51
【问题描述】:

对不起,我的英语不好。

我有一个 TextView,它有一个背景图片资源。但是当我点击这个 TextView 时,我需要 Android 将当前的背景图像资源与可绘制文件夹中的 R.drawable.bg_images 之一进行比较。

bg_image_1 bg_image_2

如果 TextView setBackgroundImageResource 是 bg_image_1 并且我点击它,将 TextView 背景图片切换到 gb_image_2 资源。

怎么做?

谢谢

【问题讨论】:

    标签: android textview


    【解决方案1】:

    将drawable设置为背景后,您无法获取drawable的资源ID。但是您可以在TextView 之外的某个地方存储某种标志甚至资源ID,或者可能在其标签字段中。在这种情况下,您将能够获取它并与另一个 id 进行比较。

    Object tag = textView.getTag();
    int backgroundId = R.drawable.bg_image_2;
    if( tag != null && ((Integer)tag).intValue() == backgroundId) {
        backgroundId = R.drawable.bg_image_1;
    }
    textView.setTag(backgroundId);
    textView.setBackgroundResource(backgroundId);
    

    【讨论】:

    • 坦克回答,TextView 在 GridView 里面我尝试在方法 setOnItemClickListener 中得到这个,我得到了 TextView..我如何使用这个标志?
    • 我对这段代码有疑问。当 TextView 超出可视化屏幕时,我需要滚动 gridview 并单击 textview,我在 Object tag = textView.getTag(); 中有一个错误;在 setOnItemClickListener 内的 getChildrenAtPosition(position) 中,没有得到这个子位置 = 47。textview = null。
    • 我认为你必须检查textView是否为null
    • ((Integer)tag).intValue() 可以替换为上述代码中的(Integer)tag
    猜你喜欢
    • 1970-01-01
    • 2011-09-15
    • 2015-01-23
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    相关资源
    最近更新 更多