0

我想在运行时更改编辑文本的文本,比如我在edittext中输入字符,如果edittext lenth大于3,那么它应该保持括号。

如果有人这样做,请告诉我。

提前致谢

特拉普蒂

4

1 回答 1

0

您可以为在 EditText 字段中输入或删除字符时调用的 EditText注册一个TextWatcher :

EditText editText = (EditText) findViewById(R.id.edit_text_field_id);
editText.addTextChangedListener(new TextWatcher() {

        @Override 
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
          // This method is called to notify you that, within s, the count characters 
          // beginning at start are about to be replaced by new text with length after.  
        }

        @Override 
        public void onTextChanged(CharSequence s, int start, int before, int count) {
          // This method is called to notify you that, within s, the count characters 
          // beginning at start have just replaced old text that had length before.
        }

        @Override 
        public void afterTextChanged(Editable s) {
          // This method is called to notify you that, somewhere within s, the text has 
          // been changed.
        }
      });

有关详细信息,请参阅TextWatcher的文档。

于 2012-08-18T10:30:59.003 回答