問題描述
我開發(fā)了一個(gè)簡單的應(yīng)用程序,例如減法、加法.在這個(gè)應(yīng)用程序中,我使用了三個(gè) EditText,一個(gè)用于回答,另外兩個(gè)用于提問.我想計(jì)算關(guān)于文本更改事件的問題的答案.但是,當(dāng)我在這兩個(gè)上應(yīng)用文本更改事件時(shí),事件會(huì)發(fā)生但無法正常工作.因?yàn)楫?dāng)我在問題的第一個(gè) EditText 中輸入文本時(shí),事件發(fā)生但它拋出了這個(gè)異常:
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:
07-03 16:39:48.844: E/EduApp Log :=>(12537): Error In Text change Event java.lang.NumberFormatException: unable to parse '' as integer
我該怎么辦?我將 TextWatcher
用于文本更改事件.
What do I do?
I use the TextWatcher
for text change event.
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) {}
推薦答案
我認(rèn)為您收到了導(dǎo)致此問題的空字符串 " "
.確保從 EditText
中獲得非空字符串.
I think you are receiving empty String " "
which is causing this problem. Make sure you get a non-empty String from your EditText
.
考慮到您的 EditText
沒有輸入任何值,并且您試圖獲取它的值并將其轉(zhuǎn)換為 int,您將遇到這種問題.
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.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
這篇關(guān)于如何在 EditText 上應(yīng)用 Textchange 事件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!