在我的Android应用程序中,我有不同的EditText
,用户可以在其中输入信息。但是我需要强制用户用大写字母书写。你知道这样做的功能吗?
Android实际上有一个built-in InputFilter just for this!
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
小心,setFilters
将重置通过XML设置的所有其他属性(即maxLines
,inputType
,imeOptinos
......)。要防止这种情况,请将Filter(s)添加到现有的Filter。
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);
这样做:
// ****** 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"
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());
}
});
要获得所有资金,请在XML中使用以下内容:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:inputType="textCapCharacters"
/>
尝试使用以下任何一个代码可能会解决您的问题。
编程方式:
editText.filters = editText.filters + InputFilter.AllCaps()
XML:
android:inputType="textCapCharacters" with Edittext
在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"
/>
这将在启用大写时更新键盘。
要在点击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"
/>
根据接受的答案,这个答案也是如此,但在Kotlin。只是为了简化copypasting:·)
private fun EditText.autocapitalize() {
val allCapsFilter = InputFilter.AllCaps()
setFilters(getFilters() + allCapsFilter)
}
我正在使用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() });
,输入为All Caps
Xamarin相当于ErlVolton的答案:
editText.SetFilters(editText.GetFilters().Append(new InputFilterAllCaps()).ToArray());
简单的kotlin实现
fun EditText.onlyUppercase() {
inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
filters = arrayOf(InputFilter.AllCaps())
}
PS for me filters
最初总是空着
如果你想
强制用户写入
在EditText中默认使用大写字母,您只需要添加android:inputType="textCapCharacters"
。 (用户仍然可以手动更改为小写。)
拟议解决方案的Java 1-liner可以是:
editText.setFilters(Lists.asList(new InputFilter.AllCaps(), editText.getFilters())
.toArray(new InputFilter[editText.getFilters().length + 1]));
注意它需要com.google.common.collect.Lists
。
对我来说,它通过添加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"
/>
只需将下面的代码添加到xml文件的EditText中。
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
如果你想允许大写文本和数字,那么使用下面的代码。
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
将input type
设置为TYPE_CLASS_TEXT| TYPE_TEXT_FLAG_CAP_CHARACTERS
。 keyboard
应该尊重这一点。
你可以用两种方式。
第一种方式:
在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);
}
}
});
您可以将android:textAllCaps="true"
属性添加到EditText中的xml文件中。这将强制软输入键盘以全大写模式显示。您输入的值将以大写字母显示。但是,这不能确保用户只能输入UpperCase
字母。如果他们想要,他们仍然可以回到小写字母。如果你想确保Edittext
的输出是全部大写,那么你必须使用toUpperCase()
类的String
方法手动转换输入字符串。
而不是担心处理键盘,为什么不接受任何输入,小写或大写并将字符串转换为大写?
以下代码应该有所帮助:
EditText edit = (EditText)findViewById(R.id.myEditText);
String input;
....
input = edit.getText();
input = input.toUpperCase(); //converts the string to uppercase
这是用户友好的,因为用户不必知道您需要大写字符串。希望这可以帮助。
您应该将android:inputType="textCapCharacters"
与Edittext放在xml文件中。
使用输入过滤器
editText = (EditText) findViewById(R.id.enteredText);
editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
更好......在Kotlin的一个班轮......
// gets your previous attributes in XML, plus adds AllCaps filter
<your_edit_text>.setFilters(<your_edit_text>.getFilters() + InputFilter.AllCaps())
完成!