开发者

Text-transform:uppercase equivalent in Android?

开发者_如何学运维

Does this exist? I need to make a TextView which is always uppercase.


Try this:

   <TextView 
            android:textAllCaps="true"
    />


I don't see anything like this in the TextView attributes. However, you could just make the text uppercase before setting it:

textView.setText(text.toUpperCase());

If the TextView is an EditText and you want whatever the user types to be uppercase, you could implement a TextWatcher and use the EditText addTextChangedListener to add it, and on the onTextChange method take the user input and replace it with the same text in uppercase.

editText.addTextChangedListener(upperCaseTextWatcher);

final TextWatcher upperCaseTextWatcher = new TextWatcher() {

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        editText.setText(editText.getText().toString().toUpperCase());
        editText.setSelection(editText.getText().toString().length());
    }

    public void afterTextChanged(Editable editable) {
    }
};


For your EditText you can use InputFilter.AllCaps as filter

editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});

See: http://d.android.com/reference/android/text/InputFilter.AllCaps.html

Also you can specify your EditText via android:inputType:

<EditText
    ...
    android:inputType="textCapCharacters" />


use

android:textAllCaps="true" 

this will make your all Textview capital.


You could do it, only adding TYPE_TEXT_FLAG_CAP_CHARACTERS to InputType::

editText.setInputType(android.text.InputType.TYPE_CLASS_TEXT
                   + android.text.InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);

I hope be helped!


I found this at android developers guide:

<TextView 
...
android:capitalize="characters"
...
/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜