【问题标题】:How to force EditText to start text with capital letter?如何强制 EditText 以大写字母开头?
【发布时间】:2020-03-03 06:39:14
【问题描述】:

我有一个EditText,我需要其中的文本(当用户输入时)以大写字母开头。

【问题讨论】:

  • +1 可搜索性:P

标签: android


【解决方案1】:

如果同时添加android:capitalize="sentences"android:inputType="text",请小心,因为后者似乎优先于第一个,并且输入不会大写。

有一个特定的inputType 用于自动将首字母大写:

android:inputType="textCapSentences"

http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType

【讨论】:

  • 不错的提示!我两者都有,它在 Nexus One 和 Galaxy Nexus 上按预期工作。但在 HTC Sense 设备上,这些词没有大写。 android:inputType 是诀窍。
  • 警告词:因为在我的例子中该字段将是一个人的名字,所以我设置了 android:inputType="textPersonName"。这没有按预期工作:我不得不使用 android:inputType="textCapWords" 来使名称的每个单词都大写。
  • android:capitalize="sentences" 不应使用此标签,因为它已被弃用
  • 如果 android:inputType="textMultiLine" 怎么办?
  • 然后使用android:inputType="textMultiLine|textCapSentences"
【解决方案2】:

android:capitalize 的选项是

android:inputType="none", which won't automatically capitalize anything.

android:inputType="sentences", Which will capitalize the first word of each sentence.

android:inputType="words", Which Will Capitalize The First Letter Of Every Word.

android:inputType="characters", WHICH WILL CAPITALIZE EVERY CHARACTER.

显然已经改成inputType而不是capitalize

【讨论】:

  • 此方法已弃用。
  • @h8pathak inputType 应用于支持此方法。
【解决方案3】:

使用

android:inputType="textPersonName|textCapWords"

因为只使用"textPersonName" 是不够的,所以名字的第一个字母会大写。

与邮政地址类似:

android:inputType="textPostalAddress|textCapSentences"

【讨论】:

  • 不适用于“da Silva”或“von Anhalt”等名称。
【解决方案4】:

将此添加到您的XML

 android:inputType="textCapWords"

android:inputType="textCapSentences" 适用于句子。但是,我需要将全名字段中的每个单词都大写。

【讨论】:

    【解决方案5】:

    试试这个方法,

    testEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
    

    android:inputType="textCapSentences" 仅在您的设备键盘自动大写设置启用时才有效。

    【讨论】:

    • 接受的答案很棒,但我正在寻找编码答案,这很有效。交易
    【解决方案6】:

    在布局xml中,添加android:inputType=textCapSentences

    【讨论】:

      【解决方案7】:

      您使用了“强制执行”这个词。所以试试这个。只需将您的编辑文本作为参数传递即可。

      public static void setCapitalizeTextWatcher(final EditText editText) {
          final TextWatcher textWatcher = new TextWatcher() {
      
              int mStart = 0;
      
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      
              }
      
              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
                  mStart = start + count;
              }
      
              @Override
              public void afterTextChanged(Editable s) {
                   String input = s.toString();
                  String capitalizedText;
                  if (input.length() < 1)
                      capitalizedText = input;
                  else
                      capitalizedText = input.substring(0, 1).toUpperCase() + input.substring(1);
                  if (!capitalizedText.equals(editText.getText().toString())) {
                      editText.addTextChangedListener(new TextWatcher() {
                          @Override
                          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      
                          }
      
                          @Override
                          public void onTextChanged(CharSequence s, int start, int before, int count) {
      
                          }
      
                          @Override
                          public void afterTextChanged(Editable s) {
                              editText.setSelection(mStart);
                              editText.removeTextChangedListener(this);
                          }
                      });
                      editText.setText(capitalizedText);
                  }
              }
          };
      
          editText.addTextChangedListener(textWatcher);
      }
      

      【讨论】:

        【解决方案8】:

        在布局xml中,添加android:capitalize="sentences"

        【讨论】:

          【解决方案9】:

          如果密码以大写开头,则为:

          android:inputType="textPassword|textCapSentences"

          【讨论】:

            【解决方案10】:

            如果您遇到设置 android:inputType=textCapSentences 然后以单行 EditText 结束的烦人情况,解决方案如下:

                android:inputType="textCapSentences|textMultiLine"
            

            【讨论】:

              【解决方案11】:

              将此粘贴​​到您的编辑文本(xml)中:

                android:capitalize="sentences"
              

              【讨论】:

                【解决方案12】:
                edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
                

                【讨论】:

                • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性的 cmets 挤满你的代码,这会降低代码和解释的可读性!
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-04-04
                • 2018-05-29
                • 2021-02-26
                • 1970-01-01
                • 2022-11-23
                • 1970-01-01
                相关资源
                最近更新 更多