有两种方法可以处理这种情况: 1.取消注册重新注册侦听器(便捷方式)让我们在这里举个例子:要将 tv_UOM_value_0 (第二个编辑文本)的值更改为 tv_UOM_0 (第一个编辑文本)的侦听器事件期间.所以,在致电之前 tv_UOM_value_0.setText("$ {tv_UOM_value.text.toString().toInt()+ tv_UOM_0.text.toString().toInt()}"") 您应该使用 tv_UOM_value_0.removeTextChangedListener(tv_UOM_value_0的textWatcherObject); 在if/else条件之前删除监听器,然后在if/else条件之后重新注册监听器.第二个编辑文本侦听器的代码也是如此. 2.检查EditText是否具有焦点(直接方式)在两个侦听器中,您应该检查给定的编辑文本是否具有焦点,如果没有焦点,则仅返回.签出以下代码: //tv_UOM_0的侦听器方法@SuppressLint("SetTextI18n")重写fun afterTextChanged(p0:是否可编辑?){if(!tv_UOM_0.hasFocus()){返回}if(p0?.isNotEmpty()== true){tv_UOM_value_0.setText("$ {tv_UOM_value.text.toString().toInt()+ tv_UOM_0.text.toString().toInt()}"))} 别的 {tv_UOM_value_0.setText(")}} i have a two editext and one textview...textview value is coming from api and based upon the value i have to estimate rest values...textview value which is coming from api is tv_UOM_valuefirst edittext id is tv_UOM_0second edittext id is tv_UOM_value_0two cases i have to handle:--1)when i type value in tv_UOM_0 it should add tv_UOM_0 + tv_UOM_value and display in tv_UOM_value_02)when i type value in tv_UOM_value_0 it should subtract tv_UOM_value_0 - tv_UOM_value and display in tv_UOM_0following is my code :- tv_UOM_0.addTextChangedListener(object:TextWatcher{ @SuppressLint("SetTextI18n") override fun afterTextChanged(p0: Editable?) { if(p0?.isNotEmpty() == true){ tv_UOM_value_0.setText( "${tv_UOM_value.text.toString() .toInt() + tv_UOM_0.text.toString().toInt()}" ) }else{ tv_UOM_value_0.setText("") } } override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { } override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { } }) tv_UOM_value_0.addTextChangedListener(object:TextWatcher{ @SuppressLint("SetTextI18n") override fun afterTextChanged(p0: Editable?) { if (p0?.isNotEmpty() == true) { tv_UOM_0.setText( "${tv_UOM_value_0.text.toString() .toInt() - tv_UOM_value.text.toString().toInt()}" ) } else{ tv_UOM_0.setText("") } } override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { } override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { } })above code output:-device gets hang when i type value in first editextneed help thanks in advance... 解决方案 Your issue is that your code is creating infinite loop where both EditTexts' listeners call each other when value is changed.There are two ways to handle this scenario:1. Unregistering & re-registering listener (Convenient way)Let's take an example here:During listener event of tv_UOM_0 (first edit text) when you want to change value to tv_UOM_value_0 (second edit text).So, before callingtv_UOM_value_0.setText("${tv_UOM_value.text.toString().toInt() + tv_UOM_0.text.toString().toInt()}")You should remove listener to second edit text using tv_UOM_value_0.removeTextChangedListener(textWatcherObject for tv_UOM_value_0); before if/else condition and then after if/else condition re-register listener again.Same goes for code of second edit text listener.2. Check whether EditText has focus (Straightforward way)During both the listeners you should check whether that given edit text has focus or not and simply return if there's no focus.Checkout the code below:// Listener method of tv_UOM_0@SuppressLint("SetTextI18n")override fun afterTextChanged(p0: Editable?) { if(!tv_UOM_0.hasFocus()) { return } if(p0?.isNotEmpty() == true){ tv_UOM_value_0.setText("${tv_UOM_value.text.toString().toInt() + tv_UOM_0.text.toString().toInt()}") } else { tv_UOM_value_0.setText("") }} 这篇关于使用addTextChangedListener计算edittext值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 00:37