在我的应用程序中,我想使用EditText
,我希望开始字符只是英文字母。
我的意思是,第一个字符只有英文字母(a到z)。
我在下面写下代码:
registerUsernameEdtTxt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().length() < 2) {
registerUsernameEdtTxt.setFilters(new InputFilter[]{new InputFilter() {
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend) {
if (src.toString().matches("[a-zA-Z ]+")) {
registerUsernameInptLay.setErrorEnabled(false);
return src;
}
registerUsernameInptLay.setError(context.getResources().getString(R.string.insertJustEnglish));
return "";
}
}});
}
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
}
});
但是没有工作我!我怎么能这样呢?
请帮帮我
答案 0 :(得分:0)
试试这个:
EditText et = findViewById(R.id.text_field);
// This part is to keep the existing filters of the EditText.
InputFilter[] filters = et.getFilters();
InputFilter[] newFilters = Arrays.copyOf(filters, filters.length + 1);
InputFilter firstFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source != null && source.length() > 0 && dstart == 0){
if (!source.toString().matches("^[A-Za-z].*"))
return "";
}
return null;
}
};
// Add the filter to the array of filters
newFilters[newFilters.length - 1] = firstFilter;
et.setFilters(newFilters);
可以像这样简化(如果不需要以前的InputFilter)
EditText et = findViewById(R.id.text_field);
InputFilter firstFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source != null && source.length() > 0 && dstart == 0){
if (!source.toString().matches("^[A-Za-z].*"))
return "";
}
return null;
}
};
et.setFilters(new InputFilter[]{firstFilter});
修改强>
如果要保留字符串的其余部分(例如,如果用户粘贴文本):
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source != null && source.length() > 0 && dstart == 0) {
String s = source.toString();
if (!s.matches("^[A-Za-z].*")) {
Toast.makeText(getApplicationContext(), "This is a Toast", Toast.LENGTH_SHORT).show();
return s.substring(1, s.length());
}
}
return null;
}
编辑2
以上版本在删除时或者在开头粘贴超过禁用字符的文本时(例如'88sdfs')不起作用,因为只删除了第一个文本并保留了其余部分。 这个新版本应涵盖所有这些案例。
我建议为InputFilter创建一个单独的类。
InputFilter firstFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (dstart != 0) { // The modified part is not beginning of the text
return null; // Nothing need to be changed
}
if (source.length() > 0) { // text is added
return onTextAdded(source.toString());
} else { // text is removed
return onTextRemoved(dest, dend);
}
}
private CharSequence onTextRemoved(Spanned dest, int dend) {
// check what the string will look like after the text being removed
String substring = dest.toString().substring(dend, dest.length());
// if there is still a string and it's not valid
if (substring.length() > 0 && !isValid(substring)) {
displayError();
// return the deleted part for the string to not change
return dest.subSequence(0, dend);
}
return null;
}
private String onTextAdded(String s) {
if (isValid(s)) {
return null;
} else {
String substring;
// We want to keep a part of the added string (it can be a paste).
// so we remove all the first characters as long as the string doesn't match
// the requirements
for (int i = 1; i < s.length(); i++) {
substring = s.substring(i, s.length());
if (isValid(substring))
break;
}
displayError();
return substring;
}
}
private boolean isValid(String s) {
return s.matches("^[A-Za-z].*");
}
private void displayError() {
Toast.makeText(getApplicationContext(), "This is a Toast", Toast.LENGTH_SHORT).show();
}
};
et.setFilters(new InputFilter[]{firstFilter});
答案 1 :(得分:0)
将此课程添加到您的项目中:
public class EnglishInputFilter implements InputFilter {
@Override
public CharSequence filter(CharSequence charSequence, int start, int end, Spanned spanned, int dstart, int dend) {
StringBuilder newChars = new StringBuilder(charSequence.toString().substring(start, end));
for (int i = 0; i < newChars.length(); ) {
if (!Character.isLetter(newChars.charAt(i)))
newChars.deleteCharAt(i);
else
i++;
}
return newChars.toString();
}
}
然后使用EditText执行此操作:
myEditText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS)
myEditText.setFilters(new InputFilter[]{new EnglishInputFilter()});
如果您粘贴&#34; 1A2B&#34;它也会从粘贴的字符串中删除任何字母字符。它会真正粘贴&#34; AB&#34;
您可以添加更多过滤器,例如限制总长度。
答案 2 :(得分:0)
尝试:
registerUsernameEdtTxt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2){
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2){
char c = charSequence.toString().charAt(0);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
{
}
else
{
//fail
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
答案 3 :(得分:0)
如果您希望编辑文本接受使用任何英文字母开始第一个字符然后您可以使用正则表达式,如;
String regexp = "^([a-zA-Z]+).*$";
Pattern pattern = Pattern.compile(regexp);
boolean ismatches = pattern.matcher("your input that start with the alphabet").matches();
if(ismatches)
do your stuff
else
show error