EditText 中仅允许 1 个字符,并且在用户输入时始终覆盖

发布于 2024-10-31 01:50:24 字数 1788 浏览 1 评论 0原文

我需要制作一个仅接受一个字符且仅接受一个字符(字母/字母)的 EditText 。如果用户输入其他字符,则应替换现有字符(例如用 1 个允许的符号覆盖文本输入的方法)。

  1. 我知道如何设置属性中文本的最大长度。但如果我设置它 为 1,则在用户删除之前不能插入其他字符 现有的一个。但我想用新输入的字符替换现有的字符 自动删除一张,无需手动删除。如何做到这一点?

  2. 我知道如何设置属性以仅允许 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 字符大小。它允许输入除字母(字符/字母)之外的任何内容,而我只想要字符。

使用 sgarmanCharacter.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).

  1. 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?

  2. 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 in EditText?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坏尐絯℡ 2024-11-07 01:50:24

将以下行添加到您的布局 XML 文件 android:maxLength="1"

以下是将其添加到 editText 元素的示例:

<EditText
   android:id="@+id/editText1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:inputType="text"
   android:maxLength="1" >

我遇到了同样的问题,并发现了这个更直接的解决方案。请参阅此博客了解更多细节。

编辑:请注意,我的答案是定义最大字符数,但是,它不会覆盖用户输入。

Add the following line to your layout XML file android:maxLength="1"

Following is an example of adding it to an editText element:

<EditText
   android:id="@+id/editText1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:inputType="text"
   android:maxLength="1" >

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.

爱冒险 2024-11-07 01:50:24

尝试:

s.delete(0, 1);
Selection.setSelection(s, s.length());

Try:

s.delete(0, 1);
Selection.setSelection(s, s.length());
眼眸里的快感 2024-11-07 01:50:24

我想做同样的事情,我在 afterTextChanged(Editable s) 中使用了你的代码,并在 editText 上设置了一个过滤器,如下所示。

editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(2)});

它按照我们的意愿工作了。

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.

editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(2)});

It worked as we wanted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文