简体   繁体   中英

How use OnClickListener in Fragments

This is my code. I'm still learning android studio. I want to show a toast when the button is clicked but when I run the application and click the button it does nothing. (this is a Fragment). Does anyone know how to fix this? I tried putting "onClick" method in the xml( android:onClick="onClick" ) then the app crashed as soon as I clicked the button.

fragment_fragment_course.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".fragmentCourse">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Course"
        android:textSize="25sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnCourse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/textView2"
        app:layout_constraintStart_toStartOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

fragmentCourse.java

package com.example.sma;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;


public class fragmentCourse extends Fragment implements View.OnClickListener {

    Button btn2;

    public fragmentCourse() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View CView = inflater.inflate(R.layout.fragment_fragment_course, container, false);

        btn2 = (Button)CView.findViewById(R.id.btnCourse);
        final TextView txt = (TextView)CView.findViewById(R.id.textView2);

        btn2.setOnClickListener(this);

        return CView;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btnCourse:
                Toast.makeText(getActivity(), "done", Toast.LENGTH_SHORT).show();
                break;

        }
    }
}

Override onViewCreated method and move below code to there. You can't access the views in onCreateView of fragment.

btn2 = (Button)CView.findViewById(R.id.btnCourse);
final TextView txt = (TextView)CView.findViewById(R.id.textView2);
btn2.setOnClickListener(this)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM