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 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01