How to Apply the Textchange event on EditText(如何在 EditText 上应用 Textchange 事件)
问题描述
我开发了一个简单的应用程序,例如减法、加法.在这个应用程序中,我使用了三个 EditText,一个用于回答,另外两个用于提问.我想计算关于文本更改事件的问题的答案.但是,当我在这两个上应用文本更改事件时,事件会发生但无法正常工作.因为当我在问题的第一个 EditText 中输入文本时,事件发生但它抛出了这个异常:
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) {}
推荐答案
我认为您收到了导致此问题的空字符串 " "
.确保从 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
没有输入任何值,并且您试图获取它的值并将其转换为 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
这篇关于如何在 EditText 上应用 Textchange 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 EditText 上应用 Textchange 事件


- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01