How to Apply the Textchange event on EditText

时间:2022-07-07 21:44:54

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:

我开发了一个简单的应用程序,如减法,添加。在这个应用程序中,我使用三个EditTexts,一个用于回答,另外两个用于提问。我想计算文本更改事件的问题答案。但是当我在这两个上应用文本更改事件时,事件发生但不正常工作。因为当我在问题的第一个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 TextWatcher for text change event.

我该怎么办?我使用TextWatcher进行文本更改事件。

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) {}  

4 个解决方案

#1


38  

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 EditText doesn'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://*.com/a/3377648/603744

#2


1  

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);

}

#3


1  

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

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

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) {

    }
});

#4


0  

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.

希望它有所帮助。

#1


38  

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 EditText doesn'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://*.com/a/3377648/603744

#2


1  

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);

}

#3


1  

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

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

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) {

    }
});

#4


0  

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.

希望它有所帮助。