如何在editText中大写字母?

时间:2018-07-05 12:02:28

标签: android android-edittext textwatcher

我有一个简单的Edittext,当我要在即时通讯设置监听器new textWatcher中更改输入字母时,它的onTextChanged()方法如下:

@Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.d("qwer", "onTextChanged: " + s + " " + start + " " + before + " " + count);

            String originalText = s.toString();
            int originalTextLength = originalText.length();
            int currentSelection = textHeading.getSelectionStart();

            StringBuilder sb = new StringBuilder();
            boolean hasChanged = false;
            for (int i = 0; i < originalTextLength; i++) {
                char currentChar = originalText.charAt(i);
                if (isAllowed(currentChar) && i < 21) {
                    sb.append(currentChar);
                } else {
                    hasChanged = true;
                    textHeading.setError("Please insert current letters");
                }
            }
            if (hasChanged) {
                String newText = sb.toString();
                textHeading.setText(capitalize(newText));
                textHeading.setSelection(currentSelection);
            }
        }

当我将经过验证的数据重新设置为edittext时,无尽的循环开始,因为它再次调用了方法ontextCahnged()。所以我的目标是动态更改输入字母,我必须将其大写。我知道还有更多最简单的方法。但是我需要这样做。

7 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点:

1-使用通用实用程序

库:http://commons.apache.org/proper/commons-lang/

StringUtils.capitalize(..)

2-通过自定义方法

public static String upperCaseFirst(String value) {

        // Convert String to char array.
        char[] array = value.toCharArray();
        // Modify first element in array.
        array[0] = Character.toUpperCase(array[0]);
        // Return string.
        return new String(array);
    }

3-来自Apache Common

库:http://commons.apache.org/proper/commons-lang/

WordUtils.capitalize(java.lang.String)

现在您可以将该字符串分配给输入框。

答案 1 :(得分:0)

您可以将(EditText的)输入类型设置为TYPE_CLASS_TEXT | TYPE_TEXT_FLAG_CAP_CHARACTERS。

OR

在您的EditText上设置android:inputType =“ textCapSentences”。

您可以点击此链接https://developer.android.com/reference/android/text/InputFilter.AllCaps

答案 2 :(得分:0)

您可以使用以下InputFilter.AllCaps

或这个

android:inputType="textCapCharacters"

答案 3 :(得分:0)

为什么不使用标志?我已通过添加boolean setManually标志来修改您的代码。

boolean setManually = false;
@Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.d("qwer", "onTextChanged: " + s + " " + start + " " + before + " " + count);
            if (setManually) {
                 setManually = false;
                  return;
            }

            String originalText = s.toString();
            int originalTextLength = originalText.length();
            int currentSelection = textHeading.getSelectionStart();

            StringBuilder sb = new StringBuilder();
            boolean hasChanged = false;
            for (int i = 0; i < originalTextLength; i++) {
                char currentChar = originalText.charAt(i);
                if (isAllowed(currentChar) && i < 21) {
                    sb.append(currentChar);
                } else {
                    hasChanged = true;
                    textHeading.setError("Please insert current letters");
                }
            }
            if (hasChanged) {
                String newText = sb.toString();
                 setManually = true;
                textHeading.setText(capitalize(newText));
                textHeading.setSelection(currentSelection);
            }
        }

答案 4 :(得分:0)

您遇到的问题是TextWatcher,而不是您编写的逻辑。以下代码块导致问题 endless cycle begins when i'm setting validated data back to the edittext becouse it calls method ontextCahnged() again

if (hasChanged) {
    String newText = sb.toString();
    textHeading.setText(capitalize(newText)); // <<<<<< This line is culprit which is calling Watcher's method again and again.
    textHeading.setSelection(currentSelection);
}

要解决此问题,您需要执行以下步骤

  1. 从EditText删除Watcher
  2. 设置文字
  3. 将观察者添加到EditText。

有关更多信息,请阅读How can I change the EditText text without triggering the Text Watcher?

答案 5 :(得分:0)

您只需在XML中使用

android:inputType="textCapCharacters"

无需为大写字母编写代码。

答案 6 :(得分:0)

尝试这样:

  String originalText = s.toString().toUpperCase();

if (hasChanged) {
            String newText = sb.toString().toUpperCase();
            textHeading.setText(newText);
            textHeading.setSelection(currentSelection);
        }