检测用户何时将数据输入到edittext中,立即显示答案 [英] Detect when user enters data into edittext immediately shows answer

查看:78
本文介绍了检测用户何时将数据输入到edittext中,立即显示答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测是否将一个字符输入到长度为0到1个字符的EditText中,然后执行某些操作?

How is it possible to detect if a character is inputted into an EditText which makes it from 0 to 1 characters long and then perform some action?

推荐答案

由于您有一个非常抽象的问题,让我尝试一个同样通用的答案:

Since you have a rather abstract question, let me attempt an equally generic answer:

在您的onCreate()中,声明并强制转换EditText

In your onCreate(), declare and cast your EditText

EditText editText = (EditText) findViewById(R.id.editText);
editText.addTextChangedListener(filterTextWatcher);

然后,外部 onCreate(),像这样设置filterTextWatcher:

And then, outside the onCreate(), setup the filterTextWatcher like this:

private TextWatcher filterTextWatcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // DO THE CALCULATIONS HERE AND SHOW THE RESULT AS PER YOUR CALCULATIONS

        int radius = 0;
        radius = Integer.valueof(s.toString);
        double area = Math.PI * radius * radius;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

已更新代码,并可能进行计算. (未测试的代码:我只是在不进行测试的情况下输入了它.在需要的地方进行修改)

Updated code with possible calculation. (UNTESTED CODE: I JUST TYPED IT IN WITHOUT TESTING IT. MODIFY WHERE NECESSARY)

详细了解 TextWatcher在这里

以下是一些入门示例:

  1. http://www.android-ever. com/2012/06/android-edittext-textwatcher-example.html
  2. http://www.cybernetikz.com/blog/android-textwatcher-example/
  3. http://www.allappsdevelopers.com/TopicDetail.aspx?TopicID = 22b00052-dad0-4e09-a07e-b74f115ab247
  4. http://myandroidsolutions.blogspot.in/2012/06/android-edittext-change-listener.html
  1. http://www.android-ever.com/2012/06/android-edittext-textwatcher-example.html
  2. http://www.cybernetikz.com/blog/android-textwatcher-example/
  3. http://www.allappsdevelopers.com/TopicDetail.aspx?TopicID=22b00052-dad0-4e09-a07e-b74f115ab247
  4. http://myandroidsolutions.blogspot.in/2012/06/android-edittext-change-listener.html

这篇关于检测用户何时将数据输入到edittext中,立即显示答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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