First letter capitalization for EditText

后端 未结 17 2005
傲寒
傲寒 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 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);
    

提交回复
热议问题