【Android】按钮设置字母不全部大写,button set text to lower case; android button settext lowercase programmably

一般情况下设置button的setText会使被设置的英文字符串全都变为大写,应该在button的xml属性中添加如下属性,才能得到想要的自定义的大小写效果:

android:textAllCaps="false"

或者在程序中设置:

mButton.setTransformationMethod(null);

我在apache.lang工具包中看到capitalize的源码,改良了一下,写了下面这个仅首字母大写的函数:


    /**
     * capitalize only the first char, the other chars in the string will be converted to lower case
     * 

*

     * TextU.capitalizeOnlyFirstChar(null)  = null
     * TextU.capitalizeOnlyFirstChar("")    = ""
     * TextU.capitalizeOnlyFirstChar("cat") = "Cat"
     * TextU.capitalizeOnlyFirstChar("cAtCh ME") = "Catch me"
     * 
*

*

* Created by KyleCe on 2015/12/23. * * @author KyleCe * KyleCe@github */ public static String capitalizeOnlyFirstChar(String str) { int strLen; if (str == null || (strLen = str.length()) == 0) { return str; } return new StringBuilder(strLen) .append(Character.toTitleCase(str.charAt(0))) .append(str.substring(1).toLowerCase()) .toString(); }

have fun

转载署源-By-KyleCe:http://blog.csdn.net/KyleCeshen/article/details/50428143

你可能感兴趣的:(Android问题记录)