我有以下代码:

<?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的内容。

最佳答案

是的,您可以在用例中为线性布局设置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();
        }
    });

关于java - Android onClick线性布局,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45525285/

10-12 02:36