【问题标题】:Android: How to catch every touch-input of the Soft Keyboard?Android:如何捕捉软键盘的每一个触摸输入?
【发布时间】:2013-03-19 14:33:46
【问题描述】:
nameInput.setOnEditorActionListener(new OnEditorActionListener() {        
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.????_?????_??????) {
                Integer inputLength2 = nameInput.getText().length();
                String realTimeText = inputLength2.toString();
                textView1.setText("Number of Characters: " + realTimeText);
            }
        return false;
        }
    });

nameInput 是 EditText 类型的对象。我想在 TextView 上实时显示从 EditText 创建的字符串的字符数。原理很简单,在我看来它会完美运行(我需要做的就是“拦截”软键盘的字符,就像我在那里所做的那样),但问题是:

http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html

没有可用的常量,这可能意味着我需要做一些晦涩的技巧来解决问题。你知道我该怎么做吗?

【问题讨论】:

    标签: android android-edittext


    【解决方案1】:

    使用TextWatcherhttp://developer.android.com/reference/android/text/TextWatcher.html

    您将收到有关输入的任何更改的通知。示例:

    private TextWatcher textWatcher = new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Integer inputLength2 = s.length();
            String realTimeText = inputLength2.toString();
            textView1.setText("Number of Characters: " + realTimeText);
        }
    
        public void afterTextChanged(Editable s) {}
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    };
    
    editText.addTextChangedListener(textWatcher);
    

    【讨论】:

    • 非常感谢!谷歌的人很聪明;他们考虑了一切:)。甚至“TextWatcher”这个名字也完美地描述了我所需要的。
    猜你喜欢
    • 2011-11-30
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 2016-04-15
    • 2011-03-03
    • 1970-01-01
    • 2020-09-18
    • 2012-09-30
    相关资源
    最近更新 更多