问题描述
我在Android项目中使用带有建议的可搜索项目.它本质上是一个EditText
I'm using a searchable item with suggestion in my Android project. It is essentially an EditText
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search.hint"
android:includeInGlobalSearch="true"
android:searchSettingsDescription="@string/search.hint"
android:searchSuggestAuthority="com.xxx.android.provider.SearchSuggestionsProvider"
android:searchSuggestSelection=" ?"
android:inputType="text"
android:imeOptions="actionSearch">
</searchable>
当我开始键入时,它显示为首字母大写.为什么?我希望它以小写开头.有可能吗?
When I start typing it shows as first letter uppercase. Why? I would like it to start lowercase. Is it possible?
推荐答案
我很惊讶还没有一个好的答案.也许是我找不到另一个问题.
I'm surprised there isn't a good answer for this yet. Or maybe it's on another question that I couldn't find.
这是我的解决方法.
editText.setFilters(new InputFilter[] {
new InputFilter.AllCaps() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
return String.valueOf(source).toLowerCase();
}
}
});
无论如何, editText
中的所有文本均为小写.
All text in editText
will be lowercase, no matter what.
您可以根据需要修改字符串.
例如:您希望所有文本都小写并且不允许使用空格(假设这是电子邮件输入字段)
You can modify the string however you like.
For example: you want all text to be lowercase AND no spaces allowed (let's say it's an email input field)
您可以这样替换 return ...
:
return String.valueOf(source).toLowerCase().replace(" ", "");
以相同的方式允许或拒绝单个字符.
本示例将所有 e
或 E
替换为 3
.
The same way you can allow or reject individual characters.
This example replaces all e
or E
with 3
.
return String.valueOf(source).replace("e", "3").replace("E", "3");
以此类推.
我希望这对某人有帮助.
I hope this helps someone.
这篇关于如何将可搜索项的EditText中的文本小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!