从 onTextChangeListener() 更改 EditText 文本 [英] Change EditText text from onTextChangeListener()

查看:16
本文介绍了从 onTextChangeListener() 更改 EditText 文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Android 应用程序.在我的应用程序中,我必须使用基于文本的图像.所以我为 EditText 编写 OnChangeListener().以下是我的示例代码.

I am working on an Android application.In my app I have to use images based on the text.So I write OnChangeListener() for EditText.The following is my sample code.

edt.addTextChangedListener(this);  
@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
    CharSequence cs=convert(edt.getText.toString());
            edt.setText(cs);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stub


}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub


}

但我收到上述代码的异常.我知道异常的原因是从 afterTextChanged() 方法调用 setText().但是我必须根据相同的 EditText 文本更改来更改 EditText 文本值.帮助我的朋友

But I am getting Exception for the above code.I know the reason for the exception is calling setText() from afterTextChanged() method. But I have to change the EditText text value based on the same EditText text change.Help me friends

推荐答案

只需在设置文本之前移除您的监听器,并在完成后重新注册,如下所述:

Just simply remove your listener before you set the text, and register it again after you are done, like described here:

添加 onTextChanged 实现后清除 EditText 文本

这篇关于从 onTextChangeListener() 更改 EditText 文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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