In Android EditText, how to force writing uppercas

2019-01-08 07:04发布

问题:

In my Android application I have different EditText where the user can enter information. But I need to force user to write in uppercase letters. Do you know a function to do that?

回答1:

Android actually has a built-in InputFilter just for this!

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

Be careful, setFilters will reset all other attributes which were set via XML (i.e. maxLines, inputType,imeOptinos...). To prevent this, add you Filter(s) to the already existing ones.

InputFilter[] editFilters = <EditText>.getFilters();
InputFilter[] newFilters = new InputFilter[editFilters.length + 1];
System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length);
newFilters[editFilters.length] = <YOUR_FILTER>;  
<EditText>.setFilters(newFilters);


回答2:

If you want to force user to write in uppercase letters by default in your EditText, you just need to add android:inputType="textCapCharacters". (User can still manually change to lowercase.)



回答3:

Set the input type to TYPE_CLASS_TEXT| TYPE_TEXT_FLAG_CAP_CHARACTERS. The keyboard should honor that.



回答4:

You can used two way.

First Way:

Set android:inputType="textCapSentences" on your EditText.

Second Way:

When user enter the number you have to used text watcher and change small to capital letter.

edittext.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {            

    }
        @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {             
    }
    @Override
    public void afterTextChanged(Editable et) {
          String s=et.toString();
      if(!s.equals(s.toUpperCase()))
      {
         s=s.toUpperCase();
         edittext.setText(s);
      }
    }
});  


回答5:

You can add the android:textAllCaps="true" property to your xml file in the EditText. This will enforce the softinput keyboard to appear in all caps mode. The value you enter will appear in Uppercase. However, this won't ensure that the user can only enter in UpperCase letters. If they want, they can still fall back to the lower case letters. If you want to ensure that the output of the Edittext is in All caps, then you have to manually convert the input String using toUpperCase() method of String class.



回答6:

Rather than worry about dealing with the keyboard, why not just accept any input, lowercase or uppercase and convert the string to uppercase?

The following code should help:

EditText edit = (EditText)findViewById(R.id.myEditText);
String input;
....
input = edit.getText();
input = input.toUpperCase(); //converts the string to uppercase

This is user-friendly since it is unnecessary for the user to know that you need the string in uppercase. Hope this helps.



回答7:

You should put android:inputType="textCapCharacters" with Edittext in xml file.



回答8:

Use input filter

editText = (EditText) findViewById(R.id.enteredText);
editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});


回答9:

Just do this:

// ****** Every first letter capital in word *********
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textCapWords"
    />

//***** if all letters are capital ************

    android:inputType="textCapCharacters"


回答10:

edittext.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {            

    }
        @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {             
    }
    @Override
    public void afterTextChanged(Editable et) {
          String s=et.toString();
      if(!s.equals(s.toUpperCase()))
      {
         s=s.toUpperCase();
         edittext.setText(s);
      }
      editText.setSelection(editText.getText().length());
    }
});  


回答11:

Even better... one liner in Kotlin...

// gets your previous attributes in XML, plus adds AllCaps filter    
<your_edit_text>.setFilters(<your_edit_text>.getFilters() + InputFilter.AllCaps())

Done!



回答12:

In kotlin, in .kt file make changes:

edit_text.filters = edit_text.filters + InputFilter.AllCaps()

Use synthetic property for direct access of widget with id. And in XML, for your edit text add a couple of more flag as:

<EditText
    android:id="@+id/edit_text_qr_code"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...other attributes...
    android:textAllCaps="true"
    android:inputType="textCapCharacters"
    />

This will update the keyboard as upper case enabled.



回答13:

To get capitalized keyboard when click edittext use this code in your xml,

<EditText
    android:id="@+id/et"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:hint="Input your country"
    android:padding="10dp"
    android:inputType="textCapCharacters"
    />


回答14:

Based on the accepted answer, this answer does the same, but in Kotlin. Just to ease copypasting :·)

private fun EditText.autocapitalize() {
    val allCapsFilter = InputFilter.AllCaps()
    setFilters(getFilters() + allCapsFilter)
}


回答15:

I'm using Visual Studio 2015/Xamarin to build my app for both Android 5.1 and Android 6.0 (same apk installed on both).

When I specified android:inputType="textCapCharacters" in my axml, the AllCaps keyboard appeared as expected on Android 6.0, but not Android 5.1. I added android:textAllCaps="true" to my axml and still no AllCaps keyboard on Android 5.1. I set a filter using EditText.SetFilters(new IInputFilter[] { new InputFilterAllCaps() }); and while the soft keyboard shows lower case characters on Android 5.1, the input field is now AllCaps.

EDIT: The behavioral differences that I observed and assumed to be OS-related were actually because I had different versions of Google Keyboard on the test devices. Once I updated the devices to the latest Google Keyboard (released July 2016 as of this writing), the 'All Caps' behavior was consistent across OSes. Now, all devices show lower-case characters on the keyboard, but the input is All Caps because of SetFilters(new IInputFilter[] { new InputFilterAllCaps() });



回答16:

To get all capital, use the following in your XML:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAllCaps="true"
    android:inputType="textCapCharacters"
/>


回答17:

Xamarin equivalent of ErlVolton's answer:

editText.SetFilters(editText.GetFilters().Append(new InputFilterAllCaps()).ToArray());


回答18:

Simple kotlin realization

fun EditText.onlyUppercase() {
    inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
    filters = arrayOf(InputFilter.AllCaps())
}

PS for me filters is always empty initially



回答19:

As @Ilia Kurtov said in https://stackoverflow.com/a/25571410/2914140, in Kotlin you can write:

editText.filters += InputFilter.AllCaps()

Note, that += adds new filters, not removing previous.



回答20:

Simply, Add below code to your EditText of your xml file.

android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

And if you want to allow both uppercase text and digits then use below code.

android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"