Android 编辑文本
我是 Android 初学者。 我有一个字母的 EditText。它只接受字符。当用户在键盘上输入不同的字符时,我需要用新字符替换输入的字符。我怎样才能做到这一点?这可能吗?
// Set edit text width only to one character
InputFilter[] FilterArray = new InputFilter[2];
FilterArray[0] = new InputFilter.LengthFilter(1);
FilterArray[1] = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetter(source.charAt(i))
&& !Character.isSpaceChar(source.charAt(i))) {
return "";
}
}
return null;
}
};
editLetter.setFilters(FilterArray);
I am a beginner in Android.
I have an EditText for one letter. It accepts only characters. I need to replace entered character with the new one when the user enters different character on the keyboard. How can I achive that? Is this possible?
// Set edit text width only to one character
InputFilter[] FilterArray = new InputFilter[2];
FilterArray[0] = new InputFilter.LengthFilter(1);
FilterArray[1] = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetter(source.charAt(i))
&& !Character.isSpaceChar(source.charAt(i))) {
return "";
}
}
return null;
}
};
editLetter.setFilters(FilterArray);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑使用正则表达式和replaceAll,如下所示:
这个答案可能对您有帮助,也可能根本没有帮助。
Consider using regular expressions and replaceAll as in:
This answer may or may not help you at all.
TextWatcher
应该派上用场。The
TextWatcher
should come in handy for this.