获取 EditText 的字符数 [英] Getting character count of EditText

查看:13
本文介绍了获取 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();

这篇关于获取 EditText 的字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆