安卓的java:更新相同的EditText在TextChanged事件事件、java、EditText、TextChanged

2023-09-13 01:56:50 作者:孤单的像条狗

在我的Andr​​oid应用我需要实现一个TextWatcher接口来实现 onTextChanged 。我的问题是,我想更新相同的EditText一些额外的字符串。当我尝试做这个程序将终止。

In my android Application I need to implement a TextWatcher interface to implement onTextChanged. The problem I have is, I want to update the same EditText With some extra string. When I try to do this the program terminates.

 final EditText ET = (EditText) findViewById(R.id.editText1);
 ET.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            try
            {
                 ET.setText("***"+ s.toString());
                 ET.setSelection(s.length());
            }
            catch(Exception e)
            {
                Log.v("State", e.getMessage());
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {

        }

        @Override
        public void afterTextChanged(Editable s)
        {               
        }
    });

我的程序终止,甚至我尝试捕捉异常就像我的code还在它终止。 有没有人有任何想法,为什么发生这种情况,我如何能做到这一点?谢谢你。

My program terminates and even I try to catch the exception like in my code still it terminates. Does anyone have any idea why this happens and how I can achieve this? Thanks.

推荐答案

的含量的TextView 是不可编辑的关于 onTextChanged 事件。

The content of the TextView is uneditable on the onTextChanged event.

相反,你需要处理的 afterTextChanged 事件能够更改文本。

Instead, you need to handle the afterTextChanged event to be able to make changes to the text.

有关更详尽的解释请参见:Android TextWatcher.afterTextChanged VS TextWatcher.onTextChanged

For more thorough explanation see: Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged

注意:错误 onTextChanged

Obvioulsy,你是通过连续的 afterTextChanged 事件改变的文本的造成一个死循环。

Obvioulsy, you are causing an endless loop by continuously changing the text on afterTextChanged event.

从裁判:

公共抽象无效afterTextChanged(编辑S)   这个方法被调用来通知您,在S中的某处,文本已   改变。它是合法的,使从这个进一步的更改送   回调,但要注意不要让自己陷入死循环,   因为任何改变,你做将导致此方法被再次调用   递归。 ... 的

public abstract void afterTextChanged (Editable s) This method is called to notify you that, somewhere within s, the text has been changed. It is legitimate to make further changes to s from this callback, but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively. ...

建议1 :如果可以,检查取值是已的你想要什么时,该事件被触发

Suggestion 1: if you can, check if the s is already what you want when the event is triggered.

@Override
public void afterTextChanged(Editable s)
{    
    if( !s.equalsIngoreCase("smth defined previously"))
         s = "smth defined previously";              
}

建议2 :如果您需要做更复杂的东西(格式化, 验证),你也许可以使用同步办法像this 帖子。

Suggestion 2: if you need to do more complex stuff (formatting, validation) you can maybe use a synchronized method like in this post.

注2 :格式化输入的部分隐着的 N 的明星,直到最后的 4 的字符( * 的*四)

Note 2 : Formatting the input as partially hidden with n stars till the last 4 chars ( **four)

您可以使用像这样的的的建议1 的:

You can use something like this in suggestion 1:

    @Override
    public void afterTextChanged(Editable s)
    {    
       String sText = ET.getText().toString()

        if( !isFormatted(sText))
             s = format(sText);              
    }
    bool isFormatted(String s)
    {
     //check if s is already formatted
    }

    string format(String s)
    {
      //format s & return
    }