EditText 中仅允许 1 个字符,并且在用户输入时始终覆盖
我需要制作一个仅接受一个字符且仅接受一个字符(字母/字母)的 EditText
。如果用户输入其他字符,则应替换现有字符(例如用 1 个允许的符号覆盖文本输入的方法)。
我知道如何设置属性中文本的最大长度。但如果我设置它 为 1,则在用户删除之前不能插入其他字符 现有的一个。但我想用新输入的字符替换现有的字符 自动删除一张,无需手动删除。如何做到这一点?
我知道如何设置属性以仅允许
EditText
中的数字,但我 不知道如何只允许字符。所以第二个问题是如何仅允许EditText
中的字符?
目前,我正在使用最大文本大小=2的 EditText
和以下代码:
final EditText editLetter = (EditText)findViewById(R.id.editHouseLetter);
editLetter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (s!=null && s.length()>1){
editLetter.setText(s.subSequence(1, s.length()));
editLetter.setSelection(1);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
要点是,当用户输入第二个字符时,应删除第一个字符。将文本大小设置为 2 允许用户在输入一个字符后输入另一个字符。
我不太明白它是如何工作的,但它确实有效:)。另外,我必须将 EditText
中的光标指向最后一个位置,因为它总是指向开头,这使得根本无法输入任何内容。也没明白这是为什么。
这个解决方案的要点是它有一个 2 字符大小的 EditText
,但我希望它是 1 字符大小。它允许输入除字母(字符/字母)之外的任何内容,而我只想要字符。
使用 sgarman 和 Character.isLetter()
函数提供的建议,afterTextChanged
方法现在看起来是这样的:
public void afterTextChanged(Editable s) {
int iLen=s.length();
if (iLen>0 && !Character.isLetter((s.charAt(iLen-1)))){
s.delete(iLen-1, iLen);
return;
}
if (iLen>1){
s.delete(0, 1);
}
}
我发现使用Selection.setSelection。现在它有一个过滤器,仅允许输入字母。这几乎就是我想要的答案了。剩下的唯一一件事是如何对 1 符号大小的
EditText
执行相同的操作?
I need to make an EditText
which would accept only one character and only character (letter/alpha). And if user enters other char, it should replace existing one (like overwrite method of text input with 1 allowed symbol).
I know how to set the max length of text in properties. But if I set it
to 1, then no other char could be inserted before user deletes an
existing one. But I want to replace existing char with that new entered
one automatically without manual deleting. How to do that?I know how to set property to allow only digits in an
EditText
, but I
can't figure out how to allow only chars. So the second question is how to allow only characters inEditText
?
Currently I'm using an EditText
with max text size=2 with the following code:
final EditText editLetter = (EditText)findViewById(R.id.editHouseLetter);
editLetter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (s!=null && s.length()>1){
editLetter.setText(s.subSequence(1, s.length()));
editLetter.setSelection(1);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
The point is, when a user enters a second char, the first one should be deleted. Making text size to 2 allows user to enter another char after one has been already entered.
I don't really understand how it works but it does :). And additionally I had to point cursor in EditText
to the last position because it always goes to beginning which makes unable to enter anything at all. Didn't get why is that either.
Main point of this solution is that it has a 2-chars sized EditText
, but I want it 1-char sized. And it allows to enter anything besides the letters(chars/alpha) and I want nothing but chars.
Using the advice provided by sgarman and Character.isLetter()
function, afterTextChanged
method looks this way now:
public void afterTextChanged(Editable s) {
int iLen=s.length();
if (iLen>0 && !Character.isLetter((s.charAt(iLen-1)))){
s.delete(iLen-1, iLen);
return;
}
if (iLen>1){
s.delete(0, 1);
}
}
I've discovered that using Selection.setSelection
is not required in this case. Now it has a filter allowing input of letters only. It's almost the answer that I want. The only one thing left is how to do the same with 1-symbol sized EditText
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将以下行添加到您的布局 XML 文件
android:maxLength="1"
以下是将其添加到
editText
元素的示例:我遇到了同样的问题,并发现了这个更直接的解决方案。请参阅此博客了解更多细节。
编辑:请注意,我的答案是定义最大字符数,但是,它不会覆盖用户输入。
Add the following line to your layout XML file
android:maxLength="1"
Following is an example of adding it to an
editText
element:I faced the same problem and found this more straightforward solution. Please refer to this blog for more details.
Edit: Please note that my answer is for defining the max number of characters, however, it does not overwrite upon user input.
尝试:
Try:
我想做同样的事情,我在 afterTextChanged(Editable s) 中使用了你的代码,并在 editText 上设置了一个过滤器,如下所示。
它按照我们的意愿工作了。
I wanted to do the same thing and I used your code in afterTextChanged(Editable s) and also set one filter on the editText like this.
It worked as we wanted.