计算 EditText 中的字符已更改侦听器

发布于 2024-10-05 06:31:47 字数 689 浏览 7 评论 0原文

在我的项目中,我有一个 EditText。我想计算 EditText 中的字符数,并在 TextView 中显示该数字。我编写了以下代码并且运行良好。但是,我的问题是,当我单击 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){}
}); 

In my project I have an EditText. I want to count the characters in the EditText, and show that number it in a TextView. I have written the following code and it works fine. However, my problem is when I click Backspace it counts up, but I need to decrement the number. How can I consider 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){}
}); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

相思故 2024-10-12 06:31:47

使用

s.length()

答案之一曾经建议过以下内容,但效率很低

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

Use

s.length()

The following was once suggested in one of the answers, but its very inefficient

textMessage.getText().toString().length()
甜尕妞 2024-10-12 06:31:47

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

沿着线的东西

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

how about just getting the length of char in your EditText and display it?

something along the line of

tv.setText(s.length() + " / " + String.valueOf(charCounts));
Spring初心 2024-10-12 06:31:47

您的代码几乎没有变化:

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){}
}); 

little few change in your code :

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){}
}); 
阿楠 2024-10-12 06:31:47

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

添加文本更改侦听器

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

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的可编辑文本。但是,如果您更改它,则必须小心不要陷入无限循环。有关更多详细信息,请参阅文档

来自此答案的补充图片

在此处输入图像描述

This is a slightly more general answer with more explanation for future viewers.

Add a text changed listener

If you want to find the text length or do something else after the text has been changed, you can add a text changed listener to your edit text.

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) {

    }
});

The listener needs a TextWatcher, which requires three methods to be overridden: beforeTextChanged, onTextChanged, and afterTextChanged.

Counting the characters

You can get the character count in onTextChanged or beforeTextChanged with

charSequence.length()

or in afterTextChanged with

editable.length()

Meaning of the methods

The parameters are a little confusing so here is a little extra explanation.

beforeTextChanged

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

  • charSequence: This is the text content before the pending change is made. You should not try to change it.
  • start: This is the index of where the new text will be inserted. If a range is selected, then it is the beginning index of the range.
  • count: This is the length of selected text that is going to be replaced. If nothing is selected then count will be 0.
  • after: this is the length of the text to be inserted.

onTextChanged

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

  • charSequence: This is the text content after the change was made. You should not try to modify this value here. Modify the editable in afterTextChanged if you need to.
  • start: This is the index of the start of where the new text was inserted.
  • before: This is the old value. It is the length of previously selected text that was replaced. This is the same value as count in beforeTextChanged.
  • count: This is the length of text that was inserted. This is the same value as after in beforeTextChanged.

afterTextChanged

afterTextChanged(Editable editable)

Like onTextChanged, this is called after the change has already been made. However, now the text may be modified.

  • editable: This is the editable text of the EditText. If you change it, though, you have to be careful not to get into an infinite loop. See the documentation for more details.

Supplemental image from this answer

enter image description here

若有似无的小暗淡 2024-10-12 06:31:47

TextWatcher maritalStatusTextWatcher = new TextWatcher() {
@覆盖
公共无效 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) {

    }
};

TextWatcher maritalStatusTextWatcher = new TextWatcher() {
@Override
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) {

    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文