如何在卡片视图上添加onClick事件

Arb*_*kdr 0 android view click

我是 Android 开发的初学者,这是我在stackoverflow上发表的第一篇文章。我想问如何在卡片视图上添加单击事件,以便每当我单击每张卡片时都会打开相应的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView               xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:id="@+id/card_view"
xmlns="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="false"
    android:orientation="vertical"
    android:padding="12dp">

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:elevation="12dp"
        android:src="@drawable/profile"
        tools:ignore="ContentDescription,UnusedAttribute" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Arbaz"
        android:textSize="20sp"
        tools:ignore="HardcodedText" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Arbaz"
        android:textSize="18sp"
        tools:ignore="HardcodedText" />


</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

小智 5

如果您想在用户单击卡片时触发任何内容,您需要在卡片上放置一个 OnClickListener。

CardView card_view = (CardView) findViewById(R.id.card_view); // creating a CardView and assigning a value.

card_view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do whatever you want to do on click (to launch any fragment or activity you need to put intent here.)
        }
    });
Run Code Online (Sandbox Code Playgroud)