【发布时间】:2013-10-19 10:42:18
【问题描述】:
是否可以在运行时更改id 组件的名称?
<ImageButton android:id="@+id/ImageButton01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" android:background="@drawable/single_square"
android:onClick="itemSelected"
/>
onClick 方法添加随机背景图像。后来,当它第二次点击时,我想知道添加了什么背景图片。为此,我想在第一次单击时将此组件的 id 名称 (ImageButton1) 更改为设置的图像名称(例如,key_image)。我该怎么做?
【问题讨论】:
-
为什么要改id?如果您只是想跟踪图像名称,则可以只使用字符串变量,或者(可能更可取)按钮上的 setTag 和 getTag 方法。
-
感谢 Mike M。你的想法比我的好。我怎样才能接受您的评论作为答案?
-
另外,如果您能详细说明,因为我以前从未使用过它们