【问题标题】:Why does my onClickListener not work in my fragment?为什么我的 onClickListener 在我的片段中不起作用?
【发布时间】:2021-08-17 15:21:04
【问题描述】:

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

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

我也尝试了“binding = FragmentProfileBinding.inflate(layoutInflater)”和“binding!!.btnLogout.setOnClickListener”,但是当我点击注销时,onClickListener 仍然无法工作或激活生成文本警报。

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

这是我的 kotlin 片段代码:

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 时我必须导入“import kotlinx.android.synthetic.main.fragment_profile.view.*”,否则文本是红色的。

【问题讨论】:

  • setupLogoutButton(view, sharedPrefs) 移动到onViewCreated()。我认为您可能在片段生命周期中过早使用合成视图引用。不确定,因为不推荐使用合成视图引用,而且我从来没有真正使用过它们。
  • 另外,我从未尝试在 TextView 上使用 ClickListener 而不是 Button。不确定它会按照您希望的方式工作。为什么不用 Button 来当按钮呢?

标签: android-studio kotlin android-fragments onclicklistener


【解决方案1】:

onCreateView 只能用于view 膨胀。任何在 Fragment 视图上操作的逻辑都应该写在 onViewCreated 中,来自 onCreateView docs

建议只在此方法中膨胀布局并移动 对返回的 View 操作的逻辑到 onViewCreated(View, 捆绑)。

因此,在您的情况下,首先要做的是将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
}

【讨论】:

  • 感谢您的回复,我还是应用开发新手,因此感谢您的反馈。我已经实施了您的更改,但结果似乎相同:/
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多