如何在 EditText 上应用 Textchange 事件 [英] How to Apply the Textchange event on EditText

查看:18
本文介绍了如何在 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 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆