【问题标题】:Android ImageButton - determine what resource is currently setAndroid ImageButton - 确定当前设置了什么资源
【发布时间】:2010-06-30 07:43:49
【问题描述】:

有没有办法可以在任何给定时间找到特定 ImageButton 设置的资源?

例如:我有一个设置为R.drawable.btn_on onCreate 的ImageButton。后来,在某个时候,ImageButton 被设置为R.drawable.btn_off。我希望能够检查 ImageButton 在我的代码中设置的资源。

谢谢 克里斯

【问题讨论】:

    标签: android resources imagebutton drawable


    【解决方案1】:

    只需使用setTag()getTag() 为您的ImageView 关联和检索自定义数据。

    【讨论】:

      【解决方案2】:

      您可以将自己的类定义为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() 设置过。

      【讨论】:

      • 也就是说,我同意 slup - 如果你想测试按钮的状态,还有更合乎逻辑的方法来做。
      【解决方案3】:

      我不知道如何直接访问资源,但是对于您尝试实现的目标,仅获取状态就足够了吗?

          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");
              }
          }
      

      http://developer.android.com/reference/android/R.attr.html#state_pressed

      【讨论】:

        【解决方案4】:

        android docs 上的文档不正确。这里声明pickDropPoint.getDrawableState()[android.R.attr.state_pressed] 返回truefalse,但它返回10,一个**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" />
        

        按下感觉的可绘制 xml

        <?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>
        

        在代码中,您不需要 @slup 建议的 for 循环

        whichPoint = (pickDropPoint.getDrawableState()[android.R.attr.state_pressed] > 1 ? PICKUP : DROP);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-08
          • 1970-01-01
          • 2013-03-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多