Android 如何在 EditText 上应用 Textchange 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11309710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:51:49  来源:igfitidea点击:

How to Apply the Textchange event on EditText

androidandroid-edittexttextwatcher

提问by Zala Janaksinh

I developed one simple app, like subtraction, addition. In this app I use three EditTexts, one for answer and other two for question. I want to calculate the answer of question on text change event. But when I apply the text change event on both of this the event occur but not properly work. Because when I enter in the text in first EditText of question the event occur but it throws this exception:

我开发了一个简单的应用程序,比如减法、加法。在这个应用程序中,我使用了三个 EditText,一个用于回答,另外两个用于问题。我想计算有关文本更改事件的问题的答案。但是当我在这两个上应用文本更改事件时,事件发生但不能正常工作。因为当我在问题的第一个 EditText 中输入文本时,事件发生但它抛出此异常:

07-03 16:39:48.844: E/EduApp Log :=>(12537): Error In Text change Event java.lang.NumberFormatException: unable to parse '' as integer

What do I do? I use the TextWatcherfor text change event.

我该怎么办?我使用TextWatcherfor 文本更改事件。

txtOne.addTextChangedListener(this);
txtTwo.addTextChangedListener(this);

public void afterTextChanged(Editable s) {}

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

public void onTextChanged(CharSequence s, int start, int before, int count) {}  

回答by Andro Selva

I think you are receiving empty String " "which is causing this problem. Make sure you get a non-empty String from your EditText.

我认为您收到的" "是导致此问题的空字符串。确保你从你的EditText.

Consider your EditTextdoesn't have any value typed in, and you are trying to get its value and convert into int you will run into this kind of problem.

考虑到您EditText没有输入任何值,而您正在尝试获取其值并将其转换为 int,您将遇到此类问题。

edittext.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
            if(!s.equals("") ) { 
                //do your work here 
            }
    }



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

    }

    public void afterTextChanged(Editable s) {

    }
});

Also check this link for more idea,

另请查看此链接以获取更多想法,

https://stackoverflow.com/a/3377648/603744

https://stackoverflow.com/a/3377648/603744

回答by user6757534

i think the best in case the edittext type is number... use the (length function) of parameter instead of (equle() function) ex:

我认为最好在edittext类型是数字的情况下......使用参数的(长度函数)而不是(equle()函数)例如:

edittext.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        if (s.length() > 0)
                { //do your work here }
        }

    }

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

    }

    public void afterTextChanged(Editable s) {

    }
});

回答by Omid

I used this and it's correct:

我用过这个,它是正确的:

public void afterTextChanged(Editable arg0) {
    String inputs = input.getText().toString();
    Integer index=0;
    if(!inputs.equals("")) 
        index=Integer.valueOf(inputs);

}

回答by myo htet aung

I think you need to check your editText value is empty or not first. Something like this:

我认为您需要先检查您的 editText 值是否为空。像这样的东西:

String textValue;
textValue = edittext().getText().toString());
Log.v("","Value is " + textValue);
if(textValue != ""){
   // Call Text Change Listener Here
}else{
   // Throw error message or something
}

Hope it's help.

希望它有帮助。

回答by Hitesh Sahu

Added Kotlinimplementation for future reference

添加了Kotlin实现以供将来参考

 webUrl.editText!!.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                var urlToload: String
                if (s ==null || s.isEmpty()) {
                    urlToload = UniversalWebViewFragment.YOUTUBE_SERACH_URL + "hd trailers"
                } else {
                    urlToload = UniversalWebViewFragment.GOOGLE_SERACH_URL + s.toString()
                }

                loadURL(urlToload)
            }

        })

回答by AndroidHV

I had the same issue and the problem was solved with these 2 actions:
1 - android:inputType="textNoSuggestions"(it did not solve the problem itself)
2 - adding the listener:

我遇到了同样的问题,并且通过以下 2 个操作解决了问题:
1 - android:inputType="textNoSuggestions"(它本身并没有解决问题)
2 - 添加侦听器:

edittext1.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before,
            int count) {

                Log.e("JFT", "EDITTEXT => = "+s);
                if(!s.equals("") ) {
                    //do your work here
                }
            }



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

            }

            public void afterTextChanged(Editable s) {

            }
        });

回答by Aftab Alam

For Kotlin use this below code

对于 Kotlin,请使用以下代码

edit1.addTextChangedListener(object : TextWatcher {
   override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
       if (edit1.text.toString().length == 1) {
             edit2.requestFocus()
       }
   }

   override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int
   ) {// TODO Auto-generated method stub
   }

   override fun afterTextChanged(s: Editable) {}
})