如何强制EditText以大写字母开始文本?

问题描述 投票:70回答:12

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

android
12个回答
144
投票

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

有一个特定的inputType自动大写第一个字母:

android:inputType="textCapSentences"

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


1
投票

将其粘贴到您的edittext(xml)中:

  android:capitalize="sentences"

1
投票
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});

0
投票

万一你遇到设置android:inputType = textCapSentences然后以单行EditText结尾的烦人的情况,这里是解决方案:

    android:inputType="textCapSentences|textMultiLine"

21
投票

android:capitalize的选项是

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

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

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

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

显然它已被改为inputType而不是capitalize


13
投票

使用

android:inputType="textPersonName|textCapWords"

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

与邮政地址类似:

android:inputType="textPostalAddress|textCapSentences"

8
投票

在你的XML中添加这个

 android:inputType="textCapWords"

android:inputType="textCapSentences"将为判刑工作。但是,我需要将全名字段中的每个单词都大写。


6
投票

试试这个,

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

android:inputType="textCapSentences"只能工作如果您的设备键盘启用了自动大写设置。


3
投票

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


2
投票

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


2
投票

你使用了“强制”这个词。所以试试吧。只需将您的edittext作为参数传递即可。

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);
}

2
投票

如果密码从大写开始,它将是:

android:inputType="textPassword|textCapSentences"

© www.soinside.com 2019 - 2024. All rights reserved.