android-studio - 为什么我的 onClickListener 在我的片段中不起作用?
问题描述
我有一堆像这样的 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.*”,否则文本为红色。
解决方案
经过认证的 bruh 时刻 - 我的滚动视图覆盖了我的按钮,知道它会是这样的
推荐阅读
- azure - 为什么 Azure VM 没有收到 HTTP GET 响应?
- python - 当一列具有nans时如何将数据框中的两列相乘
- python - 如何从 KivyMDTextField 获取文本?
- r - 如何从另一个表的列中获取最近的匹配字符串以及分数?
- python - 散景图中辅助 y 轴的旋转/镜像 y 轴文本标签
- python - Python pyautogui 和双引号
- c# - HttpRequest 是否在查询字符串中发出空值?
- java - 签署 DOCX 时出现 poi 错误。如何让它工作?
- php - 从后向迭代数组并在 foreach 循环中更改值
- go - &Struct{field: "blabla"} 和 Struct{field: "blabla"} (没有内存地址符号)之间的实际区别是什么?