文本转换:Android 中的大写等价物?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3286343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Text-transform:uppercase equivalent in Android?
提问by Joren
Does this exist? I need to make a TextView which is always uppercase.
这存在吗?我需要制作一个始终为大写的 TextView。
采纳答案by lander16
I don't see anything like this in the TextView attributes. However, you could just make the text uppercase before setting it:
我在 TextView 属性中没有看到类似的内容。但是,您可以在设置之前将文本设为大写:
textView.setText(text.toUpperCase());
If the TextView is an EditText and you want whatever the user types to be uppercase, you could implement a TextWatcher and use the EditText addTextChangedListener to add it, and on the onTextChange method take the user input and replace it with the same text in uppercase.
如果 TextView 是 EditText 并且您希望用户键入的任何内容都为大写,则可以实现 TextWatcher 并使用 EditText addTextChangedListener 添加它,并在 onTextChange 方法中获取用户输入并将其替换为相同的大写文本。
editText.addTextChangedListener(upperCaseTextWatcher);
final TextWatcher upperCaseTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
editText.setText(editText.getText().toString().toUpperCase());
editText.setSelection(editText.getText().toString().length());
}
public void afterTextChanged(Editable editable) {
}
};
回答by TheLD
Try this:
尝试这个:
<TextView
android:textAllCaps="true"
/>
回答by synergy
For your EditText you can use InputFilter.AllCaps as filter
对于您的 EditText,您可以使用 InputFilter.AllCaps 作为过滤器
editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
See: http://d.android.com/reference/android/text/InputFilter.AllCaps.html
请参阅:http: //d.android.com/reference/android/text/InputFilter.AllCaps.html
Also you can specify your EditText via android:inputType
:
您也可以通过android:inputType
以下方式指定您的 EditText :
<EditText
...
android:inputType="textCapCharacters" />
回答by Dilip
use
用
android:textAllCaps="true"
this will make your all Textview
capital.
这将使您的所有Textview
资本。
回答by Alex Pimenta
You could do it, only adding TYPE_TEXT_FLAG_CAP_CHARACTERS to InputType::
你可以做到,只需将 TYPE_TEXT_FLAG_CAP_CHARACTERS 添加到 InputType::
editText.setInputType(android.text.InputType.TYPE_CLASS_TEXT
+ android.text.InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
I hope be helped!
我希望得到帮助!