如何在android中的LinearLayout上获取视图的ID

发布于 2024-12-23 08:03:24 字数 631 浏览 5 评论 0原文

我在线性布局中有很多视图(textview) 当我点击视图时我想要每个视图ID 但我不想为每个视图设置点击侦听器,

对我来说有什么想法吗?

谢谢

<LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textViewd1"
        android:layout_width="46dp"
        android:layout_height="40dp"
        android:background="#111111"
        android:gravity="center"             
        android:text="SUN"
        android:textAppearance="?android:attr/textAppearanceSmall" />
...
many textviews
...

 </LinearLayout>

i have many views (textview) in linearlayout
i want each view id when i click on view
but i dont want to setonclicklistener with every view

anyidea for me ?

thanks

<LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textViewd1"
        android:layout_width="46dp"
        android:layout_height="40dp"
        android:background="#111111"
        android:gravity="center"             
        android:text="SUN"
        android:textAppearance="?android:attr/textAppearanceSmall" />
...
many textviews
...

 </LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2024-12-30 08:03:25

将属性 android:onClick 添加到每个视图。 (您可能还应该显式设置 android:clickable="true"。)它可能会节省为所有这些设置样式的输入,特别是如果其他一些属性在许多 TextView 之间共享的话。在您的 Activity 中,定义一个方法:

public void methodNameUsedInOnClickAttribute(View view) {
    int id = view.getId();
    // do something with ID
}

Add the attribute android:onClick to each view. (You should also probably explicitly set android:clickable="true".) It might save typing to set up a style for all this, particularly if some of the other attributes are shared among the many TextViews. In your Activity, define a method:

public void methodNameUsedInOnClickAttribute(View view) {
    int id = view.getId();
    // do something with ID
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文