First letter capitalization for EditText

后端 未结 17 1835
傲寒
傲寒 2020-11-28 01:11

I\'m working on a little personal todo list app and so far everything has been working quite well. There is one little quirk I\'d like to figure out. Whenever I go to add a

相关标签:
17条回答
  • 2020-11-28 01:59
    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.

    0 讨论(0)
  • 2020-11-28 01:59

    if you are writing styles in styles.xml then

    remove android:inputType property and add below lines

    <item name="android:capitalize">words</item>
    
    0 讨论(0)
  • 2020-11-28 02:04

    Apply following line in your EditText in XML.

    android:inputType="textCapSentences|textMultiLine"
    

    It will also allow multi-line support.

    0 讨论(0)
  • 2020-11-28 02:04

    To capitalize, you can do the following with edit text:

    To make first letter capital of every word:

    android:inputType="textCapWords"
    

    To make first letter capital of every sentence:

    android:inputType="textCapSentences"
    

    To make every letter capital:

    android:inputType="textCapCharacters"
    

    But this will only make changes to keyboard and user can change the mode to write letter in small case.

    So this approach is not much appreciated if you really want the data in capitalize format, add following class first:

    public class CapitalizeFirstLetter {
        public static String capitaliseName(String name) {
            String collect[] = name.split(" ");
            String returnName = "";
            for (int i = 0; i < collect.length; i++) {
                collect[i] = collect[i].trim().toLowerCase();
                if (collect[i].isEmpty() == false) {
                    returnName = returnName + collect[i].substring(0, 1).toUpperCase() + collect[i].substring(1) + " ";
                }
            }
            return returnName.trim();
        }
        public static String capitaliseOnlyFirstLetter(String data)
        {
            return data.substring(0,1).toUpperCase()+data.substring(1);
        }
    }
    

    And then,

    Now to capitalize every word:

    CapitalizeFirstLetter.capitaliseName(name);
    

    To capitalize only first word:

    CapitalizeFirstLetter.capitaliseOnlyFirstLetter(data);
    
    0 讨论(0)
  • 2020-11-28 02:05

    I encountered the same problem, just sharing what I found out. Might help you and others...

    Try this on your layout.add the line below in your EditText.

    android:inputType="textCapWords|textCapSentences"
    

    works fine on me.. hope it works also on you...

    0 讨论(0)
  • 2020-11-28 02:06

    In your layout XML file :

    • set android:inputType="textCapSentences"

    • On your EditText to have first alphabet of the first word of each sentence as capital

    • Or android:inputType="textCapWords" on your EditText to have first alphabet of each word as capital

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