Android Capitalize characters not working in phone

久未见 提交于 2019-12-02 17:54:30

问题


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 this:

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

and set EditText xml property to:

android:inputType="text|textCapCharacters"

Hope it helps :)




回答2:


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" 



回答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();


来源:https://stackoverflow.com/questions/15492284/android-capitalize-characters-not-working-in-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!