在我的Android应用程序中,我有不同的EditText
,用户可以在其中输入信息。但是我需要强制用户用大写字母书写。
你知道这样做的功能吗?
答案 0 :(得分:303)
Android实际上有一个built-in InputFilter just for this!
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
请注意,setFilters
会重置通过XML设置的所有其他属性(即maxLines
,inputType
,imeOptinos
... )。要防止这种情况,请将过滤器添加到现有过滤器中。
InputFilter[] editFilters = <EditText>.getFilters();
InputFilter[] newFilters = new InputFilter[editFilters.length + 1];
System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length);
newFilters[editFilters.length] = <YOUR_FILTER>;
<EditText>.setFilters(newFilters);
答案 1 :(得分:107)
如果您希望强制用户在EditText中以大写字母编写,则只需添加android:inputType="textCapCharacters"
即可。 (用户仍然可以手动更改为小写。)
答案 2 :(得分:20)
将input type
设置为TYPE_CLASS_TEXT| TYPE_TEXT_FLAG_CAP_CHARACTERS
。 keyboard
应该尊重这一点。
答案 3 :(得分:15)
你可以用两种方式。
第一种方式:
在EditText上设置android:inputType="textCapSentences"
。
第二种方式:
当用户输入号码时,您必须使用文本观察者并将小号更改为大写字母。
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);
}
}
});
答案 4 :(得分:12)
您可以将android:textAllCaps="true"
属性添加到EditText中的xml文件中。这将强制软输入键盘以全大写模式显示。您输入的值将以大写字母显示。但是,这不能确保用户只能输入UpperCase
个字母。如果他们想要,他们仍然可以回到小写字母。如果您想确保Edittext
的输出是全部大写,那么您必须使用toUpperCase()
类的String
方法手动转换输入字符串。
答案 5 :(得分:10)
为什么不接受任何输入,小写或大写并将字符串转换为大写?
而不是担心处理键盘?以下代码应该有所帮助:
EditText edit = (EditText)findViewById(R.id.myEditText);
String input;
....
input = edit.getText();
input = input.toUpperCase(); //converts the string to uppercase
这是用户友好的,因为用户无需知道您需要大写的字符串。 希望这会有所帮助。
答案 6 :(得分:10)
您应该将android:inputType="textCapCharacters"
与Edittext放在xml文件中。
答案 7 :(得分:8)
使用输入过滤器
editText = (EditText) findViewById(R.id.enteredText);
editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
答案 8 :(得分:6)
这样做:
// ****** 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"
答案 9 :(得分:5)
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());
}
});
答案 10 :(得分:5)
更好...... Kotlin 中一个班轮 ......
// gets your previous attributes in XML, plus adds AllCaps filter
<your_edit_text>.setFilters(<your_edit_text>.getFilters() + InputFilter.AllCaps())
完成!
答案 11 :(得分:4)
要获得全部资本,请在XML中使用以下内容:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:inputType="textCapCharacters"
/>
答案 12 :(得分:2)
尝试使用以下任一代码都可以解决您的问题。
以编程方式:
editText.filters = editText.filters + InputFilter.AllCaps()
XML:
android:inputType="textCapCharacters" with Edittext
答案 13 :(得分:2)
在 kotlin 的.kt文件中进行更改:
edit_text.filters = edit_text.filters + InputFilter.AllCaps()
使用合成属性直接访问ID为的小部件。 在XML中,为您的编辑文本添加以下两个标志:
<EditText
android:id="@+id/edit_text_qr_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...other attributes...
android:textAllCaps="true"
android:inputType="textCapCharacters"
/>
这会将键盘更新为大写启用。
答案 14 :(得分:1)
尝试此代码,它将使您的输入变为大写
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
答案 15 :(得分:1)
Xamarin相当于ErlVolton的答案:
editText.SetFilters(editText.GetFilters().Append(new InputFilterAllCaps()).ToArray());
答案 16 :(得分:1)
根据接受的答案,这个答案也是如此,但在Kotlin。只是为了简化copypasting:·)
private fun EditText.autocapitalize() {
val allCapsFilter = InputFilter.AllCaps()
setFilters(getFilters() + allCapsFilter)
}
答案 17 :(得分:1)
我使用Visual Studio 2015 / Xamarin为Android 5.1和Android 6.0构建我的应用程序(两者都安装了相同的apk)。
当我在我的axml中指定android:inputType="textCapCharacters"
时,AllCaps键盘在Android 6.0上按预期显示,但不是Android 5.1。我在我的axml上添加了android:textAllCaps="true"
,但在Android 5.1上仍然没有添加AllCaps键盘。我使用EditText.SetFilters(new IInputFilter[] { new InputFilterAllCaps() });
设置过滤器,而软键盘在Android 5.1上显示小写字符,输入字段现在是AllCaps。
编辑:我观察到并假设与操作系统相关的行为差异实际上是因为 我在测试设备上有不同版本的Google键盘。 我将设备更新到最新的Google键盘(截至撰写本文时已于2016年7月发布),“全部大写”操作系统的行为是一致的。现在,所有设备都在键盘上显示小写字符,但由于SetFilters(new IInputFilter[] { new InputFilterAllCaps() });
答案 18 :(得分:1)
点击 edittext 获取大写键盘,在xml中使用此代码,
<EditText
android:id="@+id/et"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="Input your country"
android:padding="10dp"
android:inputType="textCapCharacters"
/>
答案 19 :(得分:0)
简单的Kotlin实现
fun EditText.onlyUppercase() {
inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
filters = arrayOf(InputFilter.AllCaps())
}
filters
对我来说PS最初总是空的
答案 20 :(得分:0)
提出的解决方案的Java 1-liner可能是:
render(){
return (
<canvas ref="canvas" className="DrawReflect" onMouseDown={this.startDrawing}>
</canvas>
);
}
startDrawing(e){
switch(this.context.drawingTool){
case "pen":
// Stuck on getting mouse position. None of google's source is not accurate.
//this.drawPen(e.nativeEvent.offsetX, e.nativeEvent.offsetY);
//this.drawPen(e.pageX - e.target.offsetLeft, e.pageY - e.target.offsetTop);
break;
}
}
drawPen(cursorX, cursorY){
// Just for showing drawing information in a label
this.context.updateDrawInfo({
cursorX: cursorX,
cursorY: cursorY,
drawingNow: true
});
// Draw something
const canvas = this.refs.canvas;
const canvasContext = canvas.getContext("2d");
canvasContext.beginPath();
canvasContext.arc(
cursorX, cursorY /* start position */,
1, /* radius */
0, /* start angle */
2 * Math.PI /* end angle */);
canvasContext.stroke();
}
请注意,它需要editText.setFilters(Lists.asList(new InputFilter.AllCaps(), editText.getFilters())
.toArray(new InputFilter[editText.getFilters().length + 1]));
。
答案 21 :(得分:0)
对我来说,它通过添加 android:textAllCaps =“ true” 和 android:inputType =“ textCapCharacters”
而起作用<android.support.design.widget.TextInputEditText
android:layout_width="fill_parent"
android:layout_height="@dimen/edit_text_height"
android:textAllCaps="true"
android:inputType="textCapCharacters"
/>
答案 22 :(得分:-2)
只需将以下代码添加到xml文件的EditText中即可。
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
如果你想允许大写文本和数字,那么使用下面的代码。
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"