【问题标题】:Android onClick on Linear LayoutAndroid onClick线性布局
【发布时间】:2017-08-05 18:40:05
【问题描述】:

我有以下代码:

<?xml version="1.0" encoding="utf-8"?>    
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="horizontal"
    android:clickable="true"
    android:onClick="openGithubUrl">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/github_icon"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/github_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/github_url"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>


</LinearLayout>

我想在 LinearLayout(@+id/list-item) 上添加一个 onCLick 监听器。这可能吗? onClick 触发后,我想获取子 Textview @+id/github_url 的内容。

【问题讨论】:

  • 这是在 ListView 还是 RecyclerView 中?如果是这样,您不要在线性布局元素上设置点击侦听器
  • 是的,它在 ListView 中
  • 然后你使用listView.setOnItemClickListener,用于点击适配器项,而不是在LinearLayout上设置点击监听
  • 好的,非常感谢

标签: java android xml android-layout android-linearlayout


【解决方案1】:

是的,您可以在您的用例中将onClickListener 设置为线性布局, listItem.setOnClickListener() 在活动中。

LinearLayout listItem;
TextView tvGithubUrl;


tvGithubUrl = (TextView) findViewById(R.id.github_url)
listItem = (LinearLayout) findViewById(R.id.list_item)

listItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Get the github_url text here
        tvGithubUrl.getText().toString();
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-21
    • 2013-05-19
    • 2016-05-31
    • 1970-01-01
    • 2014-12-24
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多