Android Capitalize characters not working in phone

2019-03-04 06:25发布

问题:

in my app, I want to allow the user to enter only upper case letters. So I used capitalize property.

This is one part of code

<EditText
    android:id="@+id/et5"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_x="127dp"
    android:layout_y="219dp"
    android:maxLength="25"
    android:ems="10"
    android:singleLine="true"
    android:capitalize="characters"
    android:text="" />

In some cases I also tried this

android:inputType="textCapCharacters"

It works fine in emulator, But it is not working in phone. In phone, it shows lower case letters. What is the mistake here and how to overcome?

回答1:

Try working with this in java class like this :

textView.setText(text.toUpperCase());

you can also use Textwatcher method to apply capitalize on text changed

To make it work from xml you should try this:

android:inputType="textCapCharacters" 


回答2:

Try this:

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

and set EditText xml property to:

android:inputType="text|textCapCharacters"

Hope it helps :)



回答3:

edittext.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {            

}
    @Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {             
}
@Override
public void afterTextChanged(Editable arg0) {
      String s=arg0.toString();
  if(!s.equals(s.toUpperCase()))
  {
     s=s.toUpperCase();
     edittext.setText(s);
  }
}
});     


回答4:

Try this

<EditText
android:inputType="textCapCharacters" />


<EditText
android:inputType="textCapWords" />

or you can do programmatically in java file

 String str = et.getText.toString().toUpperCase();