检测EditText的内容是被用户还是以编程方式更改了? [英] Detect if content of EditText was changed by user or programmatically?

查看:80
本文介绍了检测EditText的内容是被用户还是以编程方式更改了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来检测EditText是否已通过用户键入内容或应用程序以编程方式更改了文本的方式进行了更改.有任何标准的方法吗?我想我总是可以做些骇人听闻的事情,例如在setText()之前取消设置TextWatcher,然后在之后再次设置它,但是必须有一种更好的方法...对吧?

I would need a way to detect if the EditText has been changed by the user typing something or by the app changing the text programmatically. Any standard way of doing this? I guess I could always do something hackish like unsetting the TextWatcher before setText() and setting it back again afterwards, but there's got to be a better way of doing this... right?

我尝试检查EditText是否集中在TextWatcher中,但这无济于事,因为在滚动时无论如何EditEdits都会半随机"地集中...

I tried checking if the EditText is focused in the TextWatcher, but that was of little help since the EditTexts gets focused "semi-randomly" anyway when scrolling...

 

背景

我在每个listite中都有一个带有EditTexts的ListView.我已经解决了存储EditText的值以便在用户滚动时重新使用的基本问题.

I have a ListView with EditTexts in every listitem. I've sorted out the basic problem of storing the values for the EditTexts for reuse when the user scrolls.

我还有一个TextWatcher,可以对所有EditTexts中的值求和,并在用户编辑任何EditTexts的内容时显示总和.

I also have a TextWatcher that sums up the values in all EditTexts and displays the sum when the user edits the content of any of the EditTexts.

问题是,当我滚动列表并且自定义适配器重新输入bindView()上EditTexts中的存储值时,这还会触发TextWatchers afterTextChanged()方法,导致滚动滞后,因为求和-向上功能被触发.

The problem is that when I'm scrolling the list and my custom adapter is reentering the stored values in the EditTexts on bindView(), that also triggers the TextWatchers afterTextChanged() method, causing the scrolling to lag because the summing-up-function is triggered.

推荐答案

这很早以前就已经解决了,但是对于任何在这里找到答案的人来说,这就是我所做的:

This sorted itself out a long time ago, but for anyone who finds their way here looking for an answer, here's what I did:

我最终要在将要以编程方式更改EditText的Tag之前将其设置为某个任意值,然后更改该值,然后将Tag重置为null.然后,在我的TextWatcher.afterTextChanged()方法中,检查Tag是否为null,以确定是否是用户或程序更改了该值.就像魅力一样!

I ended up setting the Tag of the EditText to some arbitrary value right before I'm about to change it programmatically, and changing the value, and then resetting the Tag to null. Then in my TextWatcher.afterTextChanged() method I check if the Tag is null or not to determine if it was the user or the program that changed the value. Works like a charm!

类似这样的东西:

edit.setTag( "arbitrary value" );
edit.setText( "My Text Value" );
edit.setTag(null);

然后

public void afterTextChanged(Editable s) {
    if( view.getTag() == null )             
        // Value changed by user
    else
        // Value changed by program
}

这篇关于检测EditText的内容是被用户还是以编程方式更改了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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