如何在即时贴视图上添加onClick事件

如何在即时贴视图上添加onClick事件

本文介绍了如何在即时贴视图上添加onClick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I Am very beginner in Android development and it's my first post on stackoverflow . And I to want ask that how I can add an click Event on card view so that whenever I click on each card the respective layout open

<?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>

推荐答案

如果要在用户单击卡时触发任何内容,则需要在卡上放置OnClickListener。

If you want to trigger anything when user clicks the card, you need to put a OnClickListener on the card.

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.)
        }
    });

这篇关于如何在即时贴视图上添加onClick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:37