Text-transform:uppercase equivalent in Android?

前端 未结 6 1613
鱼传尺愫
鱼传尺愫 2020-12-05 16:56

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

相关标签:
6条回答
  • 2020-12-05 17:22

    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" />
    
    0 讨论(0)
  • 2020-12-05 17:26

    I found this at android developers guide:

    <TextView 
    ...
    android:capitalize="characters"
    ...
    />
    
    0 讨论(0)
  • 2020-12-05 17:33

    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!

    0 讨论(0)
  • 2020-12-05 17:37

    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) {
        }
    };
    
    0 讨论(0)
  • 2020-12-05 17:40

    Try this:

       <TextView 
                android:textAllCaps="true"
        />
    
    0 讨论(0)
  • 2020-12-05 17:44

    use

    android:textAllCaps="true" 
    

    this will make your all Textview capital.

    0 讨论(0)
提交回复
热议问题