如何在android中运行时在EditText中更改文本

时间:2012-05-08 06:05:36

标签: android android-edittext

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

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

提前致谢

Trapti

1 个答案:

答案 0 :(得分: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的文档。

相关问题