【问题标题】:Counting Chars in EditText Changed Listener在 EditText 更改侦听器中计数字符
【发布时间】:2011-05-17 16:01:25
【问题描述】:

在我的项目中,我有一个EditText。我想计算EditText 中的字符,并在TextView 中显示该数字。我已经编写了以下代码,它工作正常。但是,我的问题是当我单击 Backspace 时它会增加,但我需要减少数字。如何考虑 Backspace

tv = (TextView)findViewById(R.id.charCounts);
textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {
        i++;
        tv.setText(String.valueOf(i) + " / " + String.valueOf(charCounts));
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
}); 

【问题讨论】:

  • 请原谅无关紧要,但我很好奇您是如何在问题格式中放置“退格”的?我已经问过类似的问题,该技术会非常有用。
  • 将您的单词(在本例中为 Backspace)放在 标记之间,例如:Backspace
  • 每当你看到有趣的格式(比如“退格”),你总是可以点击“编辑”链接看看作者是怎么做的。然后只需单击“取消”即可放弃您的编辑。

标签: android listener android-edittext onchange


【解决方案1】:

在 EditText 中获取 char 的长度并显示它怎么样?

类似的东西

tv.setText(s.length() + " / " + String.valueOf(charCounts));

【讨论】:

【解决方案2】:

使用

s.length()

曾经在其中一个答案中提出以下建议,但效率非常低

textMessage.getText().toString().length()

【讨论】:

    【解决方案3】:

    您的代码几乎没有变化:

    TextView tv = (TextView)findViewById(R.id.charCounts);
    textMessage = (EditText)findViewById(R.id.textMessage);
    textMessage.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s) {
            tv.setText(String.valueOf(s.toString().length()));
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    }); 
    

    【讨论】:

    • 这不会像写的那样工作 - length() 返回一个 int 而 setText 需要一个 String
    • @MartinLockett 使用 Integer.toString(int_type) 将字符串转换为 int。
    【解决方案4】:

    这是一个更笼统的答案,为未来的观众提供了更多解释。

    添加文本更改监听器

    如果您想在文本更改后查找文本长度或执行其他操作,您可以在编辑文本中添加一个文本更改侦听器。

    EditText editText = (EditText) findViewById(R.id.testEditText);
    editText.addTextChangedListener(new TextWatcher() {
    
        @Override
        public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
    
        }
    
        @Override
        public void onTextChanged(CharSequence charSequence, int start, int before, int count)  {
    
        }
    
        @Override
        public void afterTextChanged(Editable editable) {
    
        }
    });
    

    监听器需要一个TextWatcher,这需要覆盖三个方法:beforeTextChangedonTextChangedafterTextChanged

    计数字符

    您可以使用

    获取 onTextChangedbeforeTextChanged 中的字符数
    charSequence.length()
    

    或在afterTextChanged 中使用

    editable.length()
    

    方法的含义

    参数有点混乱,所以这里有一点额外的解释。

    beforeTextChanged

    beforeTextChanged(CharSequence charSequence, int start, int count, int after)

    • charSequence:这是待更改之前的文本内容。您不应该尝试更改它。
    • start:这是插入新文本的索引。如果选择了一个范围,则它是该范围的开始索引。
    • count:这是要替换的选定文本的长度。如果未选择任何内容,则 count 将是 0
    • after:这是要插入的文本的长度。

    onTextChanged

    onTextChanged(CharSequence charSequence, int start, int before, int count)

    • charSequence:这是修改后的文字内容。您不应尝试在此处修改此值。根据需要修改afterTextChanged中的editable
    • start:这是插入新文本的开始处的索引。
    • before:这是旧值。它是被替换的先前选择的文本的长度。这与beforeTextChanged 中的count 的值相同。
    • count:这是插入的文本长度。这与beforeTextChanged 中的after 的值相同。

    afterTextChanged

    afterTextChanged(Editable editable)

    onTextChanged 一样,在更改完成后调用它。但是,现在文本可能会被修改。

    • editable:这是EditText 的可编辑文本。但是,如果您更改它,则必须小心不要陷入无限循环。有关详细信息,请参阅documentation

    来自this answer的补充图片

    【讨论】:

      【解决方案5】:

      TextWatcher maritalStatusTextWatcher = new TextWatcher() { @覆盖 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

          }
      
          @Override
          public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
              try {
                  if (charSequence.length()==0){
                      topMaritalStatus.setVisibility(View.GONE);
                  }else{
                      topMaritalStatus.setVisibility(View.VISIBLE);
                  }
              }catch (Exception e){
                  e.printStackTrace();
              }
          }
      
          @Override
          public void afterTextChanged(Editable editable) {
      
          }
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多