How to capitalize the first letter of text in a TextView in an Android Application

前端 未结 11 1759
孤街浪徒
孤街浪徒 2020-12-23 15:35

I\'m not referring to textInput, either. I mean that once you have static text in a TextView (populated from a Database call to user inputted data (that may not be Capitaliz

11条回答
  •  粉色の甜心
    2020-12-23 16:09

    Please create a custom TextView and use it :

    public class CustomTextView extends TextView {
    
        public CapitalizedTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public void setText(CharSequence text, BufferType type) {
            if (text.length() > 0) {
                text = String.valueOf(text.charAt(0)).toUpperCase() + text.subSequence(1, text.length());
            }
            super.setText(text, type);
        }
    }
    

提交回复
热议问题