Android 如何以编程方式隐藏google软键盘上显示的数字
我需要一个帮助,我想从谷歌软键程序隐藏数字,只想在CapsLock上的字母,你们可以看到这个链接,它在键盘上方显示数字,这是我不想要的 我试过:Android 如何以编程方式隐藏google软键盘上显示的数字,android,android-softkeyboard,Android,Android Softkeyboard,我需要一个帮助,我想从谷歌软键程序隐藏数字,只想在CapsLock上的字母,你们可以看到这个链接,它在键盘上方显示数字,这是我不想要的 我试过: edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()}); // for making capslock on <EditText android:id="@+id/firstName" android:lay
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()}); // for making capslock on
<EditText
android:id="@+id/firstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapCharacters"
android:maxLength="10"/>
edittext.setFilters(new InputFilter[]{new InputFilter.AllCaps()});//让卡普斯洛克穿上
但数字仍然显示,用户也可以键入数字,我不需要,我需要用户只能键入字母A到Z
我发现了一个解决方案,用户无法键入数字,但它不显示大写字母(软键盘上的大写字母)
这是一个解决方案,它现在对我很有效:谢谢大家的支持
edittext.setFilters(new InputFilter[] {
new InputFilter() {
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend) {
if(src.equals("")){ // for backspace
return src;
}
if(src.toString().matches("[A-Z ]+")){
return src;
}
return "";
}
}
});
<EditText
android:id="@+id/firstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapCharacters"
android:maxLength="10"/>
edittext.setFilters(新的InputFilter[]{
新的InputFilter(){
公共CharSequence筛选器(CharSequence src,int start,
int end、跨距dst、int dstart、int dend){
if(src.equals(“”){//表示退格
返回src;
}
if(src.toString()匹配(“[A-Z]+”){
返回src;
}
返回“”;
}
}
});
验证输入字符串是否仅由字母组成的验证程序示例
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class AlphabetValidator {
private Pattern pattern;
private Matcher matcher;
public static final String ALPHABET_PATTERN = "^[a-zA-Z]+[\\p{L} .'-]*$";
public AlphabetValidator() {
pattern = pattern.compile(ALPHABET_PATTERN);
}
/**
* Validate hex with regular expression
*
* @param hex hex for validation
* @return true valid hex, false invalid hex
*/
public boolean validate(final String hex) {
matcher = pattern.matcher(hex);
return matcher.matches();
}
}
在java文件中包括
private AlphabetValidator alphabetValidator;
//after submission check
if (!alphabetValidator.validate(fieldname.getText().toString())) {
fieldname.setError("Error message");
fieldname.requestFocus();
}
第一种方式:-在编辑文本上设置
android:inputType=“textcap句”
但这只有在设备键盘自动大写设置启用时才起作用
第二种方式:-可以通过编程方式使用
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
更新答案:
尝试在EditText标记中添加此行
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
希望这对您有用。您可以在
onTextChangedListener
中更改文本后将数字替换为空。我认为用户可以编写任何内容,但您可以使用正则表达式检查它,如“^[a-Z]+”
更重要的是,用户可以安装自定义键盘,因此您无法控制键盘上显示的内容,但正如@DheerubhaiBansal所说的,你可以检查和处理用户写的内容。我已经使用了这两种方法,但仍然显示数字,但用户也可以键入数字,这只是在用户键入数字时显示错误消息,我不想要这个。我想要么是谷歌键盘上的数字被隐藏了,要么他不能在上面输入数字,这可能会有帮助