【问题标题】:How to capitalize letters in editText?如何将editText中的字母大写?
【发布时间】:2018-07-05 12:02:28
【问题描述】:

我有简单的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);
            }
        }

当我将经过验证的数据设置回编辑文本时,无限循环开始,因为它再次调用方法ontextCahnged()。所以我的目标是动态更改输入字母,我必须将其大写。我知道有更多最简单的方法可以做到这一点。但我需要这样做。

【问题讨论】:

  • 我的目标是动态更改输入字母??你能详细说明一下吗?
  • 只需在 xml 中的 EditText 中设置 android:inputType="textCapSentences"
  • 当用户在编辑文本中输入字母时,我必须捕获所有输入字母并逐个字母动态验证它们...

标签: android android-edittext textwatcher


【解决方案1】:

你的问题在于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?

【讨论】:

    【解决方案2】:

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

    1- 使用常用的 Utils

    图书馆: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)
    

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

    【讨论】:

      【解决方案3】:

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

      在你的 EditText 上设置 android:inputType="textCapSentences"。

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

      【讨论】:

        【解决方案4】:

        你可以关注InputFilter.AllCaps

        或者这个

        android:inputType="textCapCharacters"
        

        【讨论】:

          【解决方案5】:

          为什么不使用标志?我通过添加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);
                      }
                  }
          

          【讨论】:

            【解决方案6】:

            你可以简单地在 XML 中使用

            android:inputType="textCapCharacters"
            

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

            【讨论】:

              【解决方案7】:

              试试这样:

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

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

              【讨论】:

                猜你喜欢
                • 2011-06-16
                • 2012-11-22
                • 1970-01-01
                • 2020-07-06
                • 2018-06-14
                • 1970-01-01
                • 2013-02-04
                • 2014-09-16
                • 2015-08-07
                相关资源
                最近更新 更多