获取 EditText 的字符数

2022-01-14 00:00:00 android java android-edittext

我正在尝试获取 EditText 的字符数.我研究了 EditText 和 TextView 类的不同属性,但似乎没有返回字符数量的函数.我曾尝试使用 TextWatcher,但这并不理想,因为有时我会从首选项中将保存的消息加载到 EditText 中,而 TextWatcher 不会计算当时未键入的字符.

I'm trying to get a character count of an EditText. I've looked into different properties of the EditText and TextView classes, but there doesn't seem to be a function that returns the amount of characters. I have tried to use the TextWatcher, but that is not ideal since sometimes I load a saved message into the EditText from the preferences, and TextWatcher does not count characters not typed right then.

任何帮助都会很棒!

干杯!

推荐答案

只需将 EditText 中的文本抓取为字符串并检查其长度即可:

Just grab the text in the EditText as a string and check its length:

int length = editText.getText().length();

相关文章