计算 EditText 中的字符已更改侦听器
在我的项目中,我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
答案之一曾经建议过以下内容,但效率很低
Use
The following was once suggested in one of the answers, but its very inefficient
只获取 EditText 中 char 的长度并显示它怎么样?
沿着线的东西
how about just getting the length of char in your EditText and display it?
something along the line of
您的代码几乎没有变化:
little few change in your code :
这是一个稍微更笼统的答案,为未来的观众提供了更多解释。
添加文本更改侦听器
如果您想在文本更改后查找文本长度或执行其他操作,可以向编辑文本添加文本更改侦听器。
监听器需要一个
TextWatcher
,这需要要重写的三个方法:beforeTextChanged
、onTextChanged
和afterTextChanged
。计算字符数
您可以在
onTextChanged
或beforeTextChanged
中获取字符计数,或者在
afterTextChanged
中获取字符计数 方法的含义
参数有点令人困惑,所以这里有一点额外的解释。
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.
The listener needs a
TextWatcher
, which requires three methods to be overridden:beforeTextChanged
,onTextChanged
, andafterTextChanged
.Counting the characters
You can get the character count in
onTextChanged
orbeforeTextChanged
withor in
afterTextChanged
withMeaning 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 thencount
will be0
.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 theeditable
inafterTextChanged
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 ascount
inbeforeTextChanged
.count
: This is the length of text that was inserted. This is the same value asafter
inbeforeTextChanged
.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 theEditText
. 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
TextWatcher maritalStatusTextWatcher = new TextWatcher() {
@覆盖
公共无效 beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
TextWatcher maritalStatusTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {