如何大写Android EditText中的每个字母?

2022-01-14 00:00:00 android java android-edittext

我有一个这样的editTexts数组:

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

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

我希望每个字符都大写.现在,第一个单词的第一个字母是小写的,然后后面的所有字符都是大写的.我可以在用户输入完成后将其转换为大写,但这并不是我真正想要的.有没有办法让这些字段按照我想要的方式运行?

I want every character to be capitalized. Right now, the first letter of the first word is lowercase, then all the characters afterwords are upper case. I can take what the user inputs after they are done and convert that to uppercase, but that's not really what I'm going for. Is there a way to get these fields to behave the way I want them to?

推荐答案

你也需要告诉它来自class"文本:

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]);

输入类型是一个位掩码.您可以通过放置 | 来组合标志.(管道)字符在中间,它代表 OR 逻辑函数,即使在像这样的位掩码中使用它意味着这个标志和那个另一个标志".

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".

(此答案与 Robin 的答案相同,但没有幻数",这是您可以在代码中放入的最糟糕的事情之一.Android API 具有常量,请使用它们而不是复制值并冒最终破坏代码的风险.)

(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.)

相关文章