Android addTextChangedListener会立即触发
我对addTextChangedListener有问题。 addTextChangedListener在我启动应用程序时(触摸屏幕后)立即触发 addTextChangedListener位于具有自定义适配器的recyclerview中的ViewHolder类中 我曾尝试将setOnClickListener1()和setOnClickListener2()从onBindViewHolder移动到onCreateViewHolder,但效果最差(在启动应用程序后立即触发) 这是我的适配器代码Android addTextChangedListener会立即触发,android,kotlin,android-recyclerview,listener,textwatcher,Android,Kotlin,Android Recyclerview,Listener,Textwatcher,我对addTextChangedListener有问题。 addTextChangedListener在我启动应用程序时(触摸屏幕后)立即触发 addTextChangedListener位于具有自定义适配器的recyclerview中的ViewHolder类中 我曾尝试将setOnClickListener1()和setOnClickListener2()从onBindViewHolder移动到onCreateViewHolder,但效果最差(在启动应用程序后立即触发) 这是我的适配器代码 i
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
open class CustomAdapter(private val data: DataClass, private val activity: MainActivity) :
RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
init {
setHasStableIds(true)
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ViewHolder {
val rowItem: View =
LayoutInflater.from(parent.context).inflate(com.example.essai2.R.layout.list_item_view, parent, false)
var myview= ViewHolder(rowItem)
//myview.setOnClickListener1()
//myview.setOnClickListener2()
return myview
}
override fun onBindViewHolder(
holder: ViewHolder,
position: Int
) {
holder.editText.setText(data.list1[position])
holder.editText2.setText(data.list2[position])
holder.textview.setText(data.list3[position])
holder.setOnClickListener1()
holder.setOnClickListener2()
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItemCount(): Int {
return data.list1.size
}
inner class ViewHolder(rowItem: View) : RecyclerView.ViewHolder(rowItem) {
val textview : TextView = rowItem.findViewById(com.example.essai2.R.id.textview)
val editText : EditText= rowItem.findViewById(com.example.essai2.R.id.editText)
val editText2 : EditText= rowItem.findViewById(com.example.essai2.R.id.editText2)
fun fonction3(position: Int)
{
if (position >=1 && position <=98) {
data.list3[position] = (0.5 * (data.list2[position - 1].toDouble() + data.list2[position + 1].toDouble())).toString()
//activity.Update()
}
}
fun setOnClickListener1() {
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
data.list1[getAdapterPosition()]= editText.text.toString()
// activity.fonction()
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
})
}
fun setOnClickListener2() {
editText2.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
data.list2[getAdapterPosition()] = editText2.text.toString()
activity.fonction2()
fonction3(getAdapterPosition())
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
})
}
}
}
导入android.text.Editable
导入android.text.TextWatcher
导入android.view.LayoutInflater
导入android.view.view
导入android.view.ViewGroup
导入android.widget.EditText
导入android.widget.TextView
导入androidx.recyclerview.widget.recyclerview
打开类CustomAdapter(专用val数据:DataClass,专用val活动:MainActivity):
RecyclerView.Adapter(){
初始化{
SetHassTableId(真)
}
覆盖视图保持器(
父对象:视图组,
视图类型:Int
):视窗座{
val行项目:视图=
LayoutInflater.from(parent.context).充气(com.example.essai2.R.layout.list\u item\u视图,parent,false)
var myview=ViewHolder(行项目)
//myview.setOnClickListener1()
//myview.setOnClickListener2()
返回myview
}
覆盖BindViewHolder(
持有者:视图持有者,
职位:Int
) {
holder.editText.setText(data.list1[位置])
holder.editText2.settText(data.list2[位置])
holder.textview.setText(data.list3[位置])
holder.setOnClickListener1()
holder.setOnClickListener2()
}
覆盖getItemId(位置:Int):长{
返回位置。toLong()
}
重写getItemCount():Int{
return data.list1.size
}
内部类ViewHolder(rowItem:View):RecyclerView.ViewHolder(rowItem){
val textview:textview=rowItem.findViewById(com.example.essai2.R.id.textview)
val editText:editText=rowItem.findViewById(com.example.essai2.R.id.editText)
val editText2:EditText=rowItem.findViewById(com.example.essai2.R.id.editText2)
趣味游戏3(位置:Int)
{
如果(position>=1&&position您不应该在onBindViewHolder中添加TextWatcher,因为您每次调用onBindViewHolder
时都会向一个小部件添加多个侦听器
在onCreateViewHolder
中设置您的侦听器,以仅添加一个侦听器,然后当您希望将setText设置为EditText时,您可以将Tag设置为类似的视图
editText.setTag("system input");
然后在listener中检查视图的标记以确定它是用户输入还是系统输入
如果tag==“系统输入”
完成您的工作并最终删除标记
view.setTag(null)
我只是根据下面的对话修改了我的答案
首先,这里是您的XML文件(我只是更改了视图的id,其他什么都没有):
这是您的适配器(我检查了所有内容,没有错误):
导入android.content.Context
导入android.text.Editable
导入android.text.TextWatcher
导入android.view.LayoutInflater
导入android.view.view
导入android.view.ViewGroup
导入androidx.recyclerview.widget.recyclerview
导入kotlinx.android.synthetic.main.list\u item\u view.view*
类CustomAdapter(专用val数据:DataClass,专用val上下文:context):
RecyclerView.Adapter(){
初始化{
SetHassTableId(真)
}
override onCreateViewHolder(父级:ViewGroup,viewType:Int):ViewHolder{
val rowItem:View=LayoutInflater.from(上下文)。充气(R.layout.list\u item\u视图,父视图,false)
返回视图保持器(行项目)
}
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
holder.itemView.tv1.setText(data.list3[位置])
holder.itemView.et1.setText(data.list1[位置])
holder.itemView.et2.setText(data.list2[位置])
holder.itemView.et1.addTextChangedListener(对象:TextWatcher{
覆盖后文本更改(p0:可编辑?){
data.list1[position]=holder.itemView.et1.text.toString()
//activity.fonction()
}
重写更改前的乐趣(p0:CharSequence?,p1:Int,p2:Int,p3:Int){
}
重写已更改的文本(s:CharSequence?,start:Int,before:Int,count:Int){
}
})
holder.itemView.et2.addTextChangedListener(对象:TextWatcher{
覆盖后文本更改(p0:可编辑?){
data.list2[position]=holder.itemView.et2.text.toString()
//这不是'fonction2()`
holder.itemView.tv2.text=“???????”
//基金会2()
基金会3(职位)
}
重写更改前的乐趣(p0:CharSequence?,p1:Int,p2:Int,p3:Int){
}
重写已更改的文本(s:CharSequence?,start:Int,before:Int,count:Int){
}
})
}
覆盖getItemId(位置:Int):长{
返回位置。toLong()
}
重写getItemCount():Int{
return data.list1.size
}
内部类ViewHolder(rowItem:View):RecyclerView.ViewHolder(rowItem){
}
趣味游戏3(位置:Int){
如果(1..98中的位置){
data.list3[position]=(0.5*(data.list2[position-1].toDouble()+data.list2[position+1].toDouble()).toString()
//activity.Update()
}
}
}
你能提供演示项目吗?这是一张“应用程序”的图片。非常感谢你的帮助。但我不知道这是为了调试我的问题还是为了解决它(我的英语和编程技能很差)。非常感谢你的帮助。它很有效!只做了一些小改动。(holder.editText不带itemview。)很好!不用担心。我很高兴能帮上忙。但是
editText.setTag("system input");
view.setTag(null)
<?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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="MissingConstraints">
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView2"
android:textSize="16dp" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="TextView1"
android:textSize="16dp" />
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et2"
android:layout_width="196dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusableInTouchMode="true"
android:focusedByDefault="false"
android:hint="hint" />
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusedByDefault="false"
android:hint="hint" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.list_item_view.view.*
class CustomAdapter(private val data : DataClass, private val context : Context) :
RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
init {
setHasStableIds(true)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val rowItem: View = LayoutInflater.from(context).inflate(R.layout.list_item_view, parent, false)
return ViewHolder(rowItem)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.itemView.tv1.setText(data.list3[position])
holder.itemView.et1.setText(data.list1[position])
holder.itemView.et2.setText(data.list2[position])
holder.itemView.et1.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
data.list1[position]= holder.itemView.et1.text.toString()
// activity.fonction()
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
})
holder.itemView.et2.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
data.list2[position] = holder.itemView.et2.text.toString()
//this is instead of `fonction2()`
holder.itemView.tv2.text = "?????"
//fonction2()
fonction3(position)
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
})
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItemCount(): Int {
return data.list1.size
}
inner class ViewHolder(rowItem: View) : RecyclerView.ViewHolder(rowItem) {
}
fun fonction3(position: Int) {
if (position in 1..98) {
data.list3[position] = (0.5 * (data.list2[position - 1].toDouble() + data.list2[position + 1].toDouble())).toString()
//activity.Update()
}
}
}