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

我有一个 EditText,我需要在它的文本(当用户输入)开始与大写字母。

63554 次浏览

In the layout xml, add android:capitalize="sentences"

Be careful if you add both android:capitalize="sentences" and android:inputType="text", as the latter seems to have priority over the first and the input will not be capitalized.

There's a specific inputType for automatically capitalizing the first letter:

android:inputType="textCapSentences"

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

The options for android:capitalize are

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.

Apparently it has been changed to inputType instead of capitalize

In the layout xml, add android:inputType=textCapSentences

Try this way,

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

or android:inputType="textCapSentences" will only work If your device keyboard Auto Capitalize Setting enabled.

Paste this in your edittext (xml):

  android:capitalize="sentences"

Add this in your XML

 android:inputType="textCapWords"

android:inputType="textCapSentences" will work for sentences. However, I needed to capitalize every word in a full name field.

Use

android:inputType="textPersonName|textCapWords"

as using only "textPersonName" is not enough so name's first letters would be capitalized.

Similarly with postal addresses:

android:inputType="textPostalAddress|textCapSentences"

You used the word "Enforce". So try this. Just pass your edittext as argument.

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);
}
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});

In case of password which starts from upper case it will be:

android:inputType="textPassword|textCapSentences"

in case you ran into the annoying case of setting android:inputType=textCapSentences then ending up with one-liner EditText, here's the solution:

    android:inputType="textCapSentences|textMultiLine"

Adding input type in edit text as textCapSentences which will make first letter of the sentence capital

        <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="10dp"
android:gravity="center"
android:inputType="textCapSentences"
android:hint="Name"/>