Android EditText/TextView how to make each word start with uppercase and all remaining characters of words to be lowercase

ぃ、小莉子 提交于 2019-12-10 12:41:57

问题


I have already used following options to make each starting letter of a word Uppercase

 <EditText
    android:inputType="text|textCapWords"/>

While typing the user has option on the keyboard to change the case of letter i.e. the user with this option can easily type lowercase letters.

Further,I want text on my EditText to be on this format

Each Starting Letter Of A Word Must Be In Uppercase And All Other Letter Of The Word Be In Lowercase.

Meaning,when the user inputs

each StArting LeTTer of a word musT be in uppercase and all other leTTer of the word be in lowercase

, it will be automatically converted to above format.

I have tried using TextWatcher and string.split(\\s+) to get all the words and then make each and every word to follow the above format. But I always end up getting error. So if there is any solution,it would be great.I want this to work in the manner InputFilter.AllCaps.

This is my code so far

private void changeToUpperCase(String inputString) {
    if (inputString != null && inputString.trim().length() > 0) {
        // businessName.addTextChangedListener(null);
        String[] splitString = inputString.split("\\s+");
        int length = splitString.length;
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            String convertedString = splitString[i];
            stringBuffer.append(Character.toUpperCase(convertedString
                    .charAt(0)));
            stringBuffer.append(convertedString.substring(1).toLowerCase());
            stringBuffer.append(" ");
        }
        Log.i("changed String", stringBuffer.toString());
        // businessName.setText(stringBuffer.toString());
        stringBuffer.delete(0, stringBuffer.length());
        stringBuffer = null;
        // businessName.addTextChangedListener(this);
    }
}

This function I am calling from TextWatcher, afterTextChanged(Editable s)


回答1:


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

The options for android:capitalize are following :

android:capitalize="none" : which won't automatically capitalize anything.

android:capitalize="sentences" : which will capitalize the first word of each sentence.

android:capitalize="words" : which will capitalize the first letter of every word.

android:capitalize="characters" : which will capitalize every character.

Update:

As android:capitalize is deprecated now need to use:

android:inputType="textCapWords"



回答2:


change your input type programmatically.

If you are in View layout than use this code

EditText text = new EditText(context);
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS); // which will capitalize the first letter of every word.
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS); //which will capitalize every character.
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); //which will capitalize the first word of each sentence.
addView(text);

and if you are in Activity

EditText text = new EditText(this);
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS); // which will capitalize the first letter of every word.
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS); //which will capitalize every character.
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); //which will capitalize the first word of each sentence.
setContentView(text);



回答3:


Try this,

txtView.setText(WordUtils.capitalize("text view")

WordUtils.java

public class WordUtils {

    public static String capitalize(String str) {
        return capitalize(str, (char[]) null);
    }

    public static String capitalize(String str, char... delimiters) {
        int delimLen = delimiters == null ? -1 : delimiters.length;
        if (!TextUtils.isEmpty(str) && delimLen != 0) {
            char[] buffer = str.toCharArray();
            boolean capitalizeNext = true;

            for (int i = 0; i < buffer.length; ++i) {
                char ch = buffer[i];
                if (isDelimiter(ch, delimiters)) {
                    capitalizeNext = true;
                } else if (capitalizeNext) {
                    buffer[i] = Character.toTitleCase(ch);
                    capitalizeNext = false;
                }
            }

            return new String(buffer);
        } else {
            return str;
        }
    }

    private static boolean isDelimiter(char ch, char[] delimiters) {
        if (delimiters == null) {
            return Character.isWhitespace(ch);
        } else {
            char[] arr$ = delimiters;
            int len$ = delimiters.length;

            for (int i$ = 0; i$ < len$; ++i$) {
                char delimiter = arr$[i$];
                if (ch == delimiter) {
                    return true;
                }
            }

            return false;
        }
    }
}​



回答4:


android:capitalize is deprecated. Use inputType instead.




回答5:


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"


来源:https://stackoverflow.com/questions/21155111/android-edittext-textview-how-to-make-each-word-start-with-uppercase-and-all-rem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!