gpt4 book ai didi

java - fragment 中的 OnClickListener 不再起作用

转载 作者:行者123 更新时间:2023-12-01 19:30:11 26 4
gpt4 key购买 nike

我想实现一个小聊天应用程序,因此我需要一个发送按钮来清除输入字段并发送消息(现在只需向回收器 View 添加一个元素)。我已经实现了 OnClickListener 接口(interface),并将我的 fragment 作为监听器添加到按钮,但不知何故,永远不会调用 onClick。

class MainFragment : Fragment(), View.OnClickListener  {

private val messageList = ArrayList<MessageData>()
val interactor = MainInteractor()

private lateinit var viewModel: MainViewModel

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
initRecycler()
InputButton.setOnClickListener(this)
return inflater.inflate(R.layout.main_fragment, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
}

private fun initRecycler(){
messageList.add(MessageData("Das ist die erste Nachricht", 0))
messageList.add(MessageData("Das ist die zweite Nachricht", 1))
messageList.add(MessageData("Das ist die dritte Nachricht,\nwelche über 2 Zeilen geht", 0))

val recyclerView: RecyclerView = recycler
val manager: RecyclerView.LayoutManager = LinearLayoutManager(requireActivity())
val adapter: RecyclerView.Adapter<*> = MessageAdapter(messageList)

recyclerView.layoutManager = manager
recyclerView.adapter = adapter
}

private fun sendMessage(){
messageList.add(MessageData(input_field.text.toString(), 0))
input_field.setText("")
}

override fun onClick(v: View?) {
Toast.makeText(context,"Button Pressed",Toast.LENGTH_SHORT).show()
sendMessage()
}
}

这是我的 fragment 中的代码,我的布局如下所示

<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray"
tools:context=".ui.main.MainFragment">

<TextView
android:id="@+id/ueberschrift"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Chat"
android:textColor="@color/yellow"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:background="@color/yellow"

app:layout_constraintTop_toBottomOf="@+id/ueberschrift"
tools:layout_editor_absoluteX="1dp" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scrollbars="vertical"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:layout_constraintTop_toBottomOf="@+id/divider"
app:layout_constraintBottom_toTopOf="@+id/textInputLayout">

</androidx.recyclerview.widget.RecyclerView>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginBottom="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/InputButton"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="1dp"
android:textColorHint="#FFFFFF">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/input_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:textColor="#FFFFFF"
android:hint="Your Message"

/>
</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/InputButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="10dp"
android:text="Send"
android:textColor="#2E2E2E"
android:background = "@color/yellow"
app:layout_constraintStart_toEndOf="@id/textInputLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/textInputLayout"
app:layout_constraintBottom_toBottomOf="@id/textInputLayout"
/>

</androidx.constraintlayout.widget.ConstraintLayout>

这应该不会那么困难,因为这是一个基本的 Android 机制,但我不知道为什么这不起作用。我错过了什么?

最佳答案

尝试以这种方式设置点击监听器:

class MainFragment : Fragment(R.layout.main_fragment) {

//...

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
InputButton.setOnClickListener {
InputButton.setOnClickListener(this)
sendMessage()
}
}
}

关于java - fragment 中的 OnClickListener 不再起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59265690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com