In Android EditText, how to force writing uppercase?

前端 未结 23 1622
后悔当初
后悔当初 2020-11-27 12:28

In my Android application I have different EditText where the user can enter information. But I need to force user to write in uppercase letters. Do you know a

相关标签:
23条回答
  • 2020-11-27 12:54

    Just do this:

    // ****** Every first letter capital in word *********
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textCapWords"
        />
    
    //***** if all letters are capital ************
    
        android:inputType="textCapCharacters"
    
    0 讨论(0)
  • 2020-11-27 12:55

    You should put android:inputType="textCapCharacters" with Edittext in xml file.

    0 讨论(0)
  • 2020-11-27 12:55

    Use input filter

    editText = (EditText) findViewById(R.id.enteredText);
    editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
    
    0 讨论(0)
  • 2020-11-27 12:55
    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 et) {
              String s=et.toString();
          if(!s.equals(s.toUpperCase()))
          {
             s=s.toUpperCase();
             edittext.setText(s);
          }
          editText.setSelection(editText.getText().length());
        }
    });  
    
    0 讨论(0)
  • 2020-11-27 12:56

    try this code it will make your input into upper case

    edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
    
    0 讨论(0)
  • 2020-11-27 12:57

    Simply, Add below code to your EditText of your xml file.

    android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    

    And if you want to allow both uppercase text and digits then use below code.

    android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
    
    0 讨论(0)
提交回复
热议问题