0

如何获得我在编辑视图中写入的字符数?我想检查这个数字,因为当我写超过 3 时,我想使用 alertdialog。我怎样才能以简单的方式做到这一点?

4

2 回答 2

1

为此,您需要使用 TextWatcher。

    yourEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
        int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

使用任何方法来跟踪数量并在适用时显示警报

于 2012-04-24T08:11:38.953 回答
1

用于TextWatcher将 EditText 中的字符计数为:

private CharSequence temp;
    mEditText = (EditText)findViewById(R.id.ET);
        mEditText.addTextChangedListener(mTextWatcher);

        TextWatcher mTextWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int arg1, int arg2,
                        int arg3) {
                    temp = s;
                }

                @Override
                public void onTextChanged(CharSequence s, int arg1, int arg2,
                        int arg3) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                   // check here length of text in EditText after change
           if (temp.length() > 3) {
                // your alertdialog
        }
                }
            };
于 2012-04-24T08:16:12.520 回答