我在使用 setOnClickListener 单击按钮时遇到问题
问题如下。我有一个由三个片段组成的菜单。您需要从一个片段切换到数据输入布局。使用按钮进行转换。但从逻辑上讲这是行不通的。我不明白为什么。请帮忙。
package com.example.client_ismpr_system
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.client_ismpr_system.adapters.equipment.EquipmentAdapter
import com.example.client_ismpr_system.models.Equipment
class MakingNewEquipmentFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val navButton: Button? = view?.findViewById(R.id.nav_button_to_new_order)
navButton?.setOnClickListener(object : View.OnClickListener {
override fun onClick(view: View) {
Toast.makeText(requireContext(), "Button Clicked", Toast.LENGTH_SHORT).show()
println("Test")
}
})
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_making_new_equipment, container, false)
val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
val equipmentsList = listOf(
Equipment(data), Equipment(data), Equipment(data), Equipment(data),
)
val adapter = EquipmentAdapter(requireContext(), equipmentsList)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(requireContext())
return view
}
}
<?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="@color/main_theme_color"
tools:context=".MakingNewEquipmentFragment">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/recyclerView" />
<Button
android:id="@+id/nav_button_to_new_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="32dp"
android:layout_marginTop="12dp"
android:layout_marginRight="32dp"
android:layout_marginBottom="12dp"
android:backgroundTint="#4CAF50"
android:text="@string/text_making_new_order" />
</LinearLayout>
这里已经创建了Setonclicklistener处理程序,但是不起作用。另外,根据其他参与者的建议,我的观点又回来了。但这都不起作用。按钮根本不起作用。日志不会输出到LogCat。 我希望这个按钮能够正常工作。
如Fragment生命周期文档中所述,
onCreate
在beforeonCreateView
之前运行,因此你的view
在那里始终为空。
如果你想确保你的视图存在,你应该使用
onViewCreated
,它传递给onCreateView
返回的视图。
通过使用带有
R.layout
ID 的 Fragment 构造函数,您实际上根本不需要重写 onCreateView
,因此您的 Fragment 应该如下所示:
class MakingNewEquipmentFragment : Fragment(R.layout.fragment_making_new_equipment) {
override fun onViewCreated(
view: View,
savedInstanceState: Bundle?
) {
val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
val equipmentsList = listOf(
Equipment(data), Equipment(data), Equipment(data), Equipment(data),
)
val adapter = EquipmentAdapter(requireContext(), equipmentsList)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(requireContext())
val navButton = view.findViewById<Button>(R.id.nav_button_to_new_order)
navButton.setOnClickListener {
Toast.makeText(requireContext(), "Button Clicked", Toast.LENGTH_SHORT).show()
println("Test")
}
}
}