Java 片段中的OnClickListener不再工作
我想实现一个小聊天应用程序,因此我需要一个发送按钮来清除输入字段并发送消息(现在只需向回收器视图添加一个元素)。我已经实现了OnClickListener接口,并将我的片段作为Listener添加到按钮中,但不知何故从未调用onClickJava 片段中的OnClickListener不再工作,java,android,xml,button,kotlin,Java,Android,Xml,Button,Kotlin,我想实现一个小聊天应用程序,因此我需要一个发送按钮来清除输入字段并发送消息(现在只需向回收器视图添加一个元素)。我已经实现了OnClickListener接口,并将我的片段作为Listener添加到按钮中,但不知何故从未调用onClick class MainFragment : Fragment(), View.OnClickListener { private val messageList = ArrayList<MessageData>() val int
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()
}
}
class MainFragment:Fragment(),View.OnClickListener{
private val messageList=ArrayList()
val interactitor=mainInteractitor()
私有lateinit变量viewModel:MainViewModel
覆盖创建视图(
充气器:布局充气器,容器:视图组?,
savedInstanceState:捆绑?
):查看{
初始化回收器()
InputButton.setOnClickListener(此)
返回充气机。充气(右布局图。主_碎片,容器,假)
}
覆盖活动创建的乐趣(savedInstanceState:Bundle?){
super.onActivityCreated(savedInstanceState)
viewModel=ViewModelProviders.of(this.get)(MainViewModel::class.java)
}
私人娱乐回收商(){
添加(MessageData(“Das ist die erste Nachricht”,0))
添加(MessageData(“Das ist die zweite Nachricht”,1))
messageList.add(MessageData(“Das ist die dritte Nachricht,\n Elcheüber 2 Zeilen geht”,0))
val recyclerView:recyclerView=回收商
val manager:RecyclerView.LayoutManager=LinearLayoutManager(require())
val适配器:RecyclerView.adapter=MessageAdapter(messageList)
recyclerView.layoutManager=manager
recyclerView.adapter=适配器
}
私人娱乐发送消息(){
messageList.add(MessageData(input_field.text.toString(),0))
输入_字段。setText(“”)
}
覆盖有趣的onClick(v:视图?){
Toast.makeText(上下文,“按下按钮”,Toast.LENGTH\u SHORT.show())
sendMessage()
}
}
这是我片段中的代码,我的布局如下
<?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>
这应该没有那么困难,因为这是一个基本的安卓机制,但我不知道为什么这不起作用。我缺少什么?您应该将数据更改通知适配器:
private fun sendMessage(){
messageList.add(MessageData(input_field.text.toString(), 0))
recycler.adapter.notifyDataSetChanged()
input_field.setText("")
}
尝试按以下方式设置单击侦听器:
class MainFragment : Fragment(R.layout.main_fragment) {
//...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
InputButton.setOnClickListener {
InputButton.setOnClickListener(this)
sendMessage()
}
}
}
这对我不起作用,但我想我已经找到了导致这种情况的原因。请参见下文。在解决我的setContentView问题后,您的实现工作正常。谢谢。@Lukaschüler您可以通过避免重写
onCreateView()
来保持它的整洁。只需更改从类MainFragment:Fragment()
扩展的行。而是执行classmainfragment:Fragment(R.layout.main\u Fragment)
我可能已经找到了导致此问题的原因。为了解决另一个问题,我调用了``setContentView(R.layout.main_片段)``而不是R.layout.main_活动,这样我就不会因为尝试调用虚拟方法而得到null指针异常。因为onCreateView不会被调用,ButtonListener也不会被设置。