1
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タイプのオブジェクトです。EditTextから作成された文字列の文字数をTextViewにリアルタイムで表示したい。原理は単純で、私の考えでは完全に機能します(私がする必要があるのは、ソフトキーボードの文字を、そこで行ったのとまったく同じ方法で「インターセプト」することだけです)が、問題は次のとおりです。

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

そのために利用できる定数はありません。これはおそらく、問題を解決するためにいくつかのあいまいなトリックを実行する必要があることを意味します。どうすればいいか知っていますか?

4

1 に答える 1

3

使用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);
于 2013-03-19T06:28:28.500 に答える