Java 安卓onClick线性布局
我有以下代码:Java 安卓onClick线性布局,java,android,xml,android-layout,android-linearlayout,Java,Android,Xml,Android Layout,Android Linearlayout,我有以下代码: <?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"
<?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>
我想在上添加一个onCLick侦听器LinearLayout@+id/列表项。这可能吗?在触发onClick之后,我想获取子Textview@+id/github\uURL的内容 是的,在您的用例中,您可以将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();
}
});
这是在ListView还是RecyclerView中?如果是,您不在linearlayout元素上设置click listener是在listView中设置的,那么您可以使用listView.setOnItemClickListener来单击适配器项,而不是在linearlayout元素上设置click listener确定,非常感谢