Cardview点击未打开活动 [英] Cardview Click is not opening Activty

查看:81
本文介绍了Cardview点击未打开活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:

  1. 该项目没有错误,我可以成功运行.

  1. no error in this project, i can run successful.

我的按钮可以在特定时间单击.

my button can click at a certain time.

请帮帮我.

下面是我的Java文件: Dashboard.java

Below is my Java file : Dashboard.java

package com.example.whatisthat;

import androidx.cardview.widget.CardView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class Dashboard extends Activity implements View.OnClickListener
{

private CardView LoginBtn, CaptureBtn, AboutBtn, FeedbackBtn, IgBtn,     HelpBtn;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    //DEFINE CARDS
    //ADD CLICK LISTENER TO THE CARDS
    LoginBtn = findViewById(R.id.Login);
    LoginBtn.setOnClickListener(this);

    CaptureBtn = findViewById(R.id.Capture);
    CaptureBtn.setOnClickListener(this);

    AboutBtn = findViewById(R.id.About);
    AboutBtn.setOnClickListener(this);

    FeedbackBtn = findViewById(R.id.Feedback);
    FeedbackBtn.setOnClickListener(this);

    IgBtn = findViewById(R.id.Ig);
    IgBtn.setOnClickListener(this);

    HelpBtn = findViewById(R.id.Help);
    HelpBtn.setOnClickListener(this);
}

@Override
public void onClick(View view)
{
    Intent i;

    switch (view.getId())
    {
        case R.id.Login : i = new Intent(this,Login1.class);
        startActivity(i);
        break;
        case R.id.Capture : i = new Intent(this, Capture.class);
        startActivity(i);
        break;
        case R.id.About : i = new Intent(this, About.class);
        startActivity(i);
        break;
        case R.id.Feedback : i = new Intent(this, Feedback.class);
        startActivity(i);
        break;
        case R.id.Ig : i = new Intent(this, Instagram.class);
        startActivity(i);
        break;
        case R.id.Help : i = new Intent(this, Help.class);
        startActivity(i);
        break;
        default:break;
    }
}
}

下面是我的.xml文件: activity_dashboard.xml

Below is my .xml file : activity_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Dashboard">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="230dp"
                android:orientation="vertical"
                android:gravity="center"
                android:background="#6A287E">

                <ImageView
                    android:layout_width="90dp"
                    android:layout_height="90dp"
                    android:src="@drawable/iconfyp"
                    android:layout_gravity="center"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="DASHBOARD"
                    android:textColor="@color/colorAccent"
                    android:layout_gravity="center"
                    android:textStyle="bold"
                    android:textSize="18sp"
                    android:layout_marginTop="10dp"
                    />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginTop="215dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="135dp"
                    android:orientation="horizontal"
                    android:layout_marginBottom="10dp">

                    <androidx.cardview.widget.CardView
                        android:foreground="?   android:attr/selectableItemBackground"
                        android:focusable="true"
                        android:clickable="true"
                        android:id="@+id/Login"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:layout_marginRight="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:layout_width="50dp"
                                android:id="@+id/imageView1"
                                android:layout_height="50dp"
                                android:src="@drawable/people"
                                android:layout_centerInParent="true"/>

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="LOGIN"
                                android:textSize="16sp"
                                android:layout_below="@+id/imageView1"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"/>

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:id="@+id/buttonLogin"
                                    android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        android:foreground="?    android:attr/selectableItemBackground"
                        android:focusable="true"
                        android:clickable="true"
                        android:id="@+id/Capture"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:layout_marginLeft="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:layout_width="50dp"
                                android:id="@+id/imageView2"
                                android:layout_height="50dp"
                                android:src="@drawable/cameraa"
                                android:layout_centerInParent="true"/>

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="CAPTURE"
                                android:textSize="16sp"
                                android:layout_below="@+id/imageView2"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"/>

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:id="@+id/buttonCapture"
                                    android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="135dp"
                    android:orientation="horizontal"
                    android:layout_marginBottom="10dp">

                    <androidx.cardview.widget.CardView
                        android:foreground="?    android:attr/selectableItemBackground"
                        android:focusable="true"
                        android:clickable="true"
                        android:id="@+id/About"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:layout_marginRight="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:layout_width="50dp"
                                android:id="@+id/imageView3"
                                android:layout_height="50dp"
                                android:src="@drawable/aboutt"
                                android:layout_centerInParent="true"/>

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="ABOUT"
                                android:textSize="16sp"
                                android:layout_below="@+id/imageView3"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"/>

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:id="@+id/buttonAbout"
                                    android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        android:foreground="?    android:attr/selectableItemBackground"
                        android:focusable="true"
                        android:clickable="true"
                        android:id="@+id/Feedback"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:layout_marginLeft="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:layout_width="50dp"
                                android:id="@+id/imageView4"
                                android:layout_height="50dp"
                                android:src="@drawable/feedback"
                                android:layout_centerInParent="true"/>

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="FEEDBACK "
                                android:textSize="16sp"
                                android:layout_below="@+id/imageView4"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"/>

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:id="@+id/buttonFeedback"
                                    android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="135dp"
                    android:orientation="horizontal"
                    android:layout_marginBottom="10dp">

                    <androidx.cardview.widget.CardView
                        android:foreground="?    android:attr/selectableItemBackground"
                        android:focusable="true"
                        android:clickable="true"
                        android:id="@+id/Ig"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:layout_marginRight="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:layout_width="50dp"
                                android:id="@+id/imageView5"
                                android:layout_height="50dp"
                                android:src="@drawable/instagram"
                                android:layout_centerInParent="true"/>

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="INSTAGRAM"
                                android:textSize="16sp"
                                android:layout_below="@+id/imageView5"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"/>

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:id="@+id/buttonIg"
                                    android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                    <androidx.cardview.widget.CardView
                        android:foreground="?    android:attr/selectableItemBackground"
                        android:focusable="true"
                        android:clickable="true"
                        android:id="@+id/Help"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:layout_marginLeft="5dp"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <ImageView
                                android:layout_width="50dp"
                                android:id="@+id/imageView6"
                                android:layout_height="50dp"
                                android:src="@drawable/help"
                                android:layout_centerInParent="true"/>

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="HELP"
                                android:textSize="16sp"
                                android:layout_below="@+id/imageView6"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"/>

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:id="@+id/buttonHelp"
                                    android:background="@android:color/transparent"/>

                        </RelativeLayout>
                    </androidx.cardview.widget.CardView>
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</ScrollView>
</RelativeLayout>

推荐答案

好,您必须首先从 android:foreground =?android:attr/selectableItemBackground" 删除空间并将其添加到所有按钮

Well You have to First Remove space from this android:foreground="?android:attr/selectableItemBackground" and add this to all the button

android:clickable ="false"

如果要使用 Button 打开活动,则必须将 setOnClickListener 放到Button中.否则,如果要使用 Cardview 打开活动,您必须将 setOnClickListener 放入cardview.

If you want to Open Activity With Button Then you have to put setOnClickListener to Button .Else if you want to open Activity with Cardview you have to put setOnClickListener to cardview.

将此方法用于 OnClick 方法:

@Override
    public void onClick(View view) {
        if (view==LoginBtn){
 startActivity(new Intent(this,Login1.class));
        }else if(view==Capture){
startActivity(new Intent(this,Capture.class));

      }
     //Do rest by yourself.

    }

这篇关于Cardview点击未打开活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆