How to capitalize every letter in an Android EditText?

后端 未结 10 1751
离开以前
离开以前 2021-01-07 17:41

I have an array of editTexts which I make like this:

        inputs[i] = new EditText(this);
        inputs[i].setWidth(376);
        inputs[i].setInputType(         


        
10条回答
  •  天命终不由人
    2021-01-07 18:18

    You need to tell it it's from the "class" text as well:

    inputs[i] = new EditText(this);
    inputs[i].setWidth(376);
    inputs[i].setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
    tFields.addView(inputs[i]);
    

    The input type is a bitmask. You can combine the flags by putting the | (pipe) character in the middle, which stands for the OR logic function, even though when used in a bitmask like this it means "this flag AND that other flag".

    (This answer is the same as Robin's but without "magic numbers", one of the worst things you can put in your code. The Android API has constants, use them instead of copying the values and risking to eventually break the code.)

提交回复
热议问题