Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

java - On click Listener issue

I am working on Android app in Android Studio and I created Grid Layout with Card view. as on image below: enter image description here

I was able to make onClick listener to open my BMIActivity when user click on BMI element in the grid.

Here is my Code for MainActivity.java:

    package com.example.metapall;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    GridLayout mainGrid;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainGrid = (GridLayout)findViewById(R.id.mainGrid);
        setSingleEvent(mainGrid);
    }

    private void setSingleEvent(GridLayout mainGrid) {
        for(int i=0;i<mainGrid.getChildCount();i++)
        {
            CardView cardView = (CardView)mainGrid.getChildAt(i);
            final int finalI = i;
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    openBMIActivity();
                }
            });
        }
    }

    private void openBMIActivity() {
        Intent intent = new Intent(this, BMIActivity.class);
        startActivity(intent);
    }
}

And here is my xml code for mainActivity:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:background="@drawable/bg"
    android:weightSum="10"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="0dp">

        <TextView
            android:id="@+id/TextGird"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="METAPAL"
            android:textColor="@android:color/white"
            android:layout_centerInParent="true"
            android:textSize="34sp" />

    </RelativeLayout>

    <GridLayout
        android:id="@+id/mainGrid"
        android:columnCount="2"
        android:rowCount="3"
        android:alignmentMode="alignMargins"
        android:columnOrderPreserved="false"
        android:layout_weight="8"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:padding="14dp"
        >
        <!-- Row 1 -->
        <!-- Column 1 -->

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"

            >

            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@drawable/group"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="BMI"
                    android:textAlignment="center"
                    android:textColor="@android:color/black"
                    android:textSize="18dp"
                    android:textStyle="bold" />
            </LinearLayout>


        </androidx.cardview.widget.CardView>

        <!-- Column 2 -->
        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            >

            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@drawable/notes"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:textSize="18dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"
                    android:text="CHO" />
            </LinearLayout>


        </androidx.cardview.widget.CardView>

        <!-- Row 2 -->
        <!-- Column 1 -->

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            >

            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@drawable/piggy"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:textSize="18dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"
                    android:text="H20" />
            </LinearLayout>


        </androidx.cardview.widget.CardView>

        <!-- Column 2 -->
        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            >

            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@drawable/shopping"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:textSize="18dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"
                    android:text="Protein" />
            </LinearLayout>


        </androidx.cardview.widget.CardView>

        <!-- Row 3 -->
        <!-- Column 1 -->

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            >

            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:src="@drawable/calendar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:textSize="18dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"
                    android:text="Calendar" />
            </LinearLayout>


        </androidx.cardview.widget.CardView>

        <!-- Column 2 -->
        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginLeft="16dp"
            android:l

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since you have added views manually in layout file, you should assign id's to your cells (CardViews) and click listeners directly on them.

<androidx.cardview.widget.CardView
    android:id="@+id/cvBMI"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_rowWeight="1"
    android:layout_columnWeight="1"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="16dp"
    app:cardCornerRadius="8dp"
    app:cardElevation="8dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true">

.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    GridLayout mainGrid;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainGrid = (GridLayout) findViewById(R.id.mainGrid);

        findViewById(R.id.cvBMI).setOnClickListener(this);
        findViewById(R.id.cvCHO).setOnClickListener(this);
        findViewById(R.id.cvH20).setOnClickListener(this);
        findViewById(R.id.cvProtein).setOnClickListener(this);
        findViewById(R.id.cvCalendar).setOnClickListener(this);
        findViewById(R.id.cvSettings).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cvBMI:
                openBMIActivity();
                break;
            case R.id.cvCHO:
                openCHOActivity();
                break;
            case R.id.cvH20:
                openH2OActivity();
                break;
            case R.id.cvProtein:
                openProteinActivity();
                break;
            case R.id.cvCalendar:
                openCalendarActivity();
                break;
            case R.id.cvSettings:
                openSettingsActivity();
                break;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...