为什么我的 onClickListener 在我的片段中不起作用?

Why does my onClickListener not work in my fragment?

我有一堆这样的 onclicklisteners,但这个只是出于某种原因不想工作,我确定我错过了一些明显的东西,哈哈。尝试做的所有按钮都是注销。

我现在知道不是 startActivity 不起作用,而是整个侦听器都不起作用,因为我添加了 Toast 制作文本警报,当我点击注销按钮时它不会触发。

我也尝试过使用“binding!!.btnLogout.setOnClickListener”进行“binding = FragmentProfileBinding.inflate(layoutInflater)”,但是当我在点击注销。

但是是的,我们将不胜感激任何帮助。

这是我的科特林片段代码:

package com.example.nonutsinmybasket.fragments

import android.content.Intent
import android.content.SharedPreferences
import android.view.LayoutInflater
import android.view.ViewGroup
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.example.nonutsinmybasket.R
import com.example.nonutsinmybasket.activity.Login
import com.example.nonutsinmybasket.databinding.FragmentProfileBinding
import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.fragment_profile.view.*

class Profile(var userId: String?, var sharedPrefs: SharedPreferences) : Fragment() {
    var binding: FragmentProfileBinding? = null
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreate(savedInstanceState)
        val view  = inflater.inflate(R.layout.fragment_profile, container, false)
        setupLogoutButton(view, sharedPrefs)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // find views by id
    }

    private fun setupLogoutButton(view: View, sharedPrefs: SharedPreferences) {
        view.btnLogout.setOnClickListener {
            Toast.makeText(activity,
                "Tap detected",
                Toast.LENGTH_LONG).show()
            sharedPrefs.edit().remove("Email").apply()
            sharedPrefs.edit().remove("UserId").apply()
            FirebaseAuth.getInstance().signOut()
            startActivity(Intent(context, Login::class.java))
            activity?.finish()
        }
    }
}

这是我的关联 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"
    android:background="@color/bg"
    android:focusableInTouchMode="true"
    tools:context=".fragments.Profile">

    <LinearLayout
        android:id="@+id/profileFooter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:layout_marginEnd="@dimen/_10sdp"
        android:layout_marginBottom="@dimen/_10sdp"
        android:background="@drawable/bg_rounded_ractangle_10"
        android:backgroundTint="@color/colorPrimary"
        android:orientation="vertical"
        android:padding="@dimen/_10sdp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:clickable="true">
        <TextView
            android:id="@+id/btnLogout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:fontFamily="@font/p_bold"
            android:gravity="center"
            android:text="Logout"
            android:textColor="@color/white"
            android:textSize="@dimen/_12sdp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/aboveFooter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:layout_marginEnd="@dimen/_10sdp"
        android:layout_marginBottom="@dimen/_10sdp"
        android:background="@drawable/bg_rounded_ractangle_10"
        android:backgroundTint="@color/colorPrimary"
        android:orientation="vertical"
        android:padding="@dimen/_10sdp"
        app:layout_constraintBottom_toTopOf="@id/profileFooter">

        <TextView
            android:id="@+id/updateInfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:fontFamily="@font/p_bold"
            android:gravity="center"
            android:text="Update Information"
            android:textColor="@color/white"
            android:textSize="@dimen/_12sdp" />
    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/_5sdp"
                android:layout_marginTop="@dimen/_5sdp"
                android:layout_marginRight="@dimen/_5sdp"
                android:layout_marginBottom="@dimen/_5sdp"
                android:background="@drawable/bg_rounded_ractangle_10"
                android:orientation="vertical"
                android:padding="@dimen/_10sdp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <EditText
                        android:id="@+id/test"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#00ffffff"
                        android:fontFamily="@font/p_reguler"
                        android:hint=""
                        android:inputType="text"
                        android:textColor="@color/text1"
                        android:textColorHint="@color/text4"
                        android:textSize="@dimen/_10sdp"
                        android:visibility="gone" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/_10sdp"
                        android:layout_weight="1"
                        android:background="@drawable/bg_rounded_ractangle_5_stroke"
                        android:orientation="vertical"
                        android:padding="@dimen/_10sdp">

                        <EditText
                            android:id="@+id/etRegisterEmail"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="#00ffffff"
                            android:fontFamily="@font/p_reguler"
                            android:hint="Email"
                            android:inputType="text"
                            android:maxLines="1"
                            android:textColor="@color/text1"
                            android:textColorHint="@color/text4"
                            android:textSize="@dimen/_11sdp" />

                    </LinearLayout>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_10sdp"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/bg_rounded_ractangle_5_stroke"
                        android:orientation="horizontal"
                        android:padding="@dimen/_10sdp">

                        <EditText
                            android:id="@+id/etConfirmPassword"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:background="#00ffffff"
                            android:fontFamily="@font/p_reguler"
                            android:hint="Password"
                            android:inputType="textPassword"
                            android:maxLines="1"
                            android:textColor="@color/text1"
                            android:textColorHint="@color/text4"
                            android:textSize="@dimen/_11sdp" />

                        <ImageView
                            android:id="@+id/btnShowPass"
                            android:layout_width="@dimen/_25sdp"
                            android:layout_height="match_parent"
                            android:layout_gravity="center"
                            android:adjustViewBounds="true"
                            android:alpha=".5"
                            android:padding="@dimen/_2sdp"
                            android:src="@drawable/ic_eye" />

                    </LinearLayout>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_10sdp"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/bg_rounded_ractangle_5_stroke"
                        android:orientation="horizontal"
                        android:padding="@dimen/_10sdp">

                        <EditText
                            android:id="@+id/confirmpassword"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:background="#00ffffff"
                            android:fontFamily="@font/p_reguler"
                            android:hint="Confirm Password"
                            android:inputType="textPassword"
                            android:maxLines="1"
                            android:textColor="@color/text1"
                            android:textColorHint="@color/text4"
                            android:textSize="@dimen/_11sdp" />

                        <ImageView
                            android:id="@+id/btnShowConPass"
                            android:layout_width="@dimen/_25sdp"
                            android:layout_height="match_parent"
                            android:layout_gravity="center"
                            android:adjustViewBounds="true"
                            android:alpha=".5"
                            android:padding="@dimen/_2sdp"
                            android:src="@drawable/ic_eye" />

                    </LinearLayout>

                </LinearLayout>


            </LinearLayout>

        </LinearLayout>
    </ScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>

不确定这是否有帮助,但在编写 view.btnLogout 时我不得不导入“导入 kotlinx.android.synthetic.main.fragment_profile.view.*”,否则文本为红色。

onCreateView 应该只用于 view inflation。任何对 Fragment 的视图进行操作的逻辑都应该写在 onViewCreated 中,来自 onCreateView docs

It is recommended to only inflate the layout in this method and move logic that operates on the returned View to onViewCreated(View, Bundle).

因此,在您的情况下,首先要做的是将 setupLogoutButton(view, sharedPrefs) 移至 onViewCreated,其次,我不完全确定您是否正确使用了合成材料,因为 view.btnLogout 没有多大意义,通常在合成时你指定视图 ID,我建议你 migrate to ViewBinding,或者在这种情况下只需在 Fragmentsview 上使用 findViewById作为

val button = view.findViewById<TextView>(R.id.btnLogout)
button.setOnClickListener {
    // your code here
}

经过认证的 bruh 时刻 - 我的滚动视图覆盖了我的按钮,我知道它会是这样的