使用addTextChangedListener计算edittext值 [英] compute the edittext value using addTextChangedListener

查看:117
本文介绍了使用addTextChangedListener计算edittext值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个editext和一个textview ... textview值来自api,并基于该值我必须估计其余值...

来自API的

textview值是 tv_UOM_value 第一个edittext ID为 tv_UOM_0 第二个edittext id是 tv_UOM_value_0

我必须处理两种情况:-

1)当我在 tv_UOM_0 中键入值时,应添加 tv_UOM_0 + tv_UOM_value 并显示在 tv_UOM_value_0

2)当我在 tv_UOM_value_0 中键入值时,应减去 tv_UOM_value_0 - tv_UOM_value 并显示在 tv_UOM_0

以下是我的代码:-

  tv_UOM_0.addTextChangedListener(object:TextWatcher {@SuppressLint("SetTextI18n")重写fun afterTextChanged(p0:是否可编辑?){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(")}}覆盖fun beforeTextChanged(p0:CharSequence ?, p1:Int,p2:Int,p3:Int){}重写fun onTextChanged(p0:CharSequence ?, p1:Int,p2:Int,p3:Int){}})tv_UOM_value_0.addTextChangedListener(object:TextWatcher {@SuppressLint("SetTextI18n")重写fun afterTextChanged(p0:是否可编辑?){如果(p0?.isNotEmpty()== true){tv_UOM_0.setText(" $ {tv_UOM_value_0.text.toString().toInt()-tv_UOM_value.text.toString().toInt()}")}别的{tv_UOM_0.setText(")}}覆盖fun beforeTextChanged(p0:CharSequence ?, p1:Int,p2:Int,p3:Int){}重写fun onTextChanged(p0:CharSequence ?, p1:Int,p2:Int,p3:Int){}}) 

以上代码输出:-当我在第一次editext中键入value时,设备挂起

需要帮助,在此先感谢...

解决方案

您的问题是您的代码正在创建无限循环,当值更改时,两个 EditText 的侦听器会相互调用.

有两种方法可以处理这种情况:

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_value first edittext id is tv_UOM_0 second edittext id is tv_UOM_value_0

two 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_0

2)when i type value in tv_UOM_value_0 it should subtract tv_UOM_value_0 - tv_UOM_value and display in tv_UOM_0

following 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 editext

need 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 calling

tv_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值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆