In Android EditText, how to force writing uppercase?

前端 未结 23 1619
后悔当初
后悔当初 2020-11-27 12:28

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

相关标签:
23条回答
  • 2020-11-27 12:35

    For me it worked by adding android:textAllCaps="true" and android:inputType="textCapCharacters"

    <android.support.design.widget.TextInputEditText
                        android:layout_width="fill_parent"
                        android:layout_height="@dimen/edit_text_height"
                        android:textAllCaps="true"
                        android:inputType="textCapCharacters"
                        />
    
    0 讨论(0)
  • 2020-11-27 12:36

    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"
    />
    
    0 讨论(0)
  • 2020-11-27 12:36

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

    0 讨论(0)
  • 2020-11-27 12:37

    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);
    
    0 讨论(0)
  • 2020-11-27 12:39

    Try using any one of the below code may solve your issue.

    programatically:

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

    XML :

    android:inputType="textCapCharacters" with Edittext
    
    0 讨论(0)
  • 2020-11-27 12:40

    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.

    0 讨论(0)
提交回复
热议问题