【发布时间】:2013-04-19 18:02:27
【问题描述】:
嗨,我试图获取我在每个线性布局上设置的 ID,但我却得到了
android.widget.LinearLayout@41032a40 或类似的,对我来说用处不大。
我已将 id 设置为 row1,这就是我想要返回的内容。
我确定我以前做过类似的事情,所以我无法弄清楚为什么它会返回上述内容。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/row1"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="@android:color/black"
android:onClick="xmlClickHandler" />
<LinearLayout
android:id="@+id/row2"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="@android:color/white"
android:onClick="xmlClickHandler" />
<LinearLayout
android:id="@+id/row3"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="@android:color/black"
android:onClick="xmlClickHandler" />
<LinearLayout
android:id="@+id/row4"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="@color/white"
android:onClick="xmlClickHandler" />
<LinearLayout
android:id="@+id/row5"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="@color/yellow"
android:onClick="xmlClickHandler" />
<LinearLayout
android:id="@+id/row6"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="@color/blue"
android:onClick="xmlClickHandler" />
public void xmlClickHandler(View v) {
Log.d("CLICK ROW", String.valueOf(v));
}
【问题讨论】:
-
你的意思是你只想在每一行中显示字符串“row(n)”吗?
-
为什么需要 id 作为字符串?
-
因为我想知道具体点击了哪些线性布局,也许我应该为此使用一个表格,但由于在触摸模式下没有突出显示,所以方法保持不变。我希望用户根据问题触摸每个布局(即触摸红色和黑色条),并且我需要保存已在某处按下的布局。作为开发人员,将 id 作为数字返回几乎没有上下文。我需要查看第 1 行、第 3 行、第 4 行,而不是让事情变得复杂的数字。
-
定义一个 id 将始终创建一组字符以与可读字符串相关联,例如在您的情况下,row1、row2 等。它们是在您的 R.java 文件中自动生成的。使用
android:text@string/row1会给你实际的字符串。我认为您无法更改 R.java 中的 ID ...