[英]Android EditText Space Validation
I have an Edittext in my android application.我的 android 应用程序中有一个 Edittext。 I don't want to allow user to enter first space character..but after entering other charecter user can enter space also ..I used
我不想让用户输入第一个空格字符..但是输入其他字符后用户也可以输入空格..我用过
<EditText
android:id="@+id/editText1_in_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:digits="_,qwertzuiopasdfghjklyxcvbnm,QWERTYUIOPASDFGHJKLZXCVBNM,0123456789">
but in this case user can not enter space.但在这种情况下,用户不能输入空格。
I have also used Text Watcher but I need not to allow user at the time of entering text as android:digits works.我也使用过Text Watcher,但我不需要在输入文本时允许用户使用 android:digits 工作。
final EditText editText = (EditText)findViewById(R.id.editText1_in_row);
InputFilter filter = new InputFilter() {
boolean canEnterSpace = false;
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if(editText.getText().toString().equals(""))
{
canEnterSpace = false;
}
StringBuilder builder = new StringBuilder();
for (int i = start; i < end; i++) {
char currentChar = source.charAt(i);
if (Character.isLetterOrDigit(currentChar) || currentChar == '_') {
builder.append(currentChar);
canEnterSpace = true;
}
if(Character.isWhitespace(currentChar) && canEnterSpace) {
builder.append(currentChar);
}
}
return builder.toString();
}
};
editText.setFilters(new InputFilter[]{filter});
and remove this property from your EditText并从您的 EditText 中删除此属性
android:digits="_,qwertzuiopasdfghjklyxcvbnm,QWERTYUIOPASDFGHJKLZXCVBNM,0123456789"
This code works exactly according to your needs.此代码完全根据您的需要工作。
Using InputFilter easy to handle enter first white space character ignore使用InputFilter轻松处理输入第一个空格字符忽略
First setFilters() method on editText editText 上的第一个 setFilters() 方法
editText.setFilters(new InputFilter[]{ignoreFirstWhiteSpace()});
Make InputFilter制作输入过滤器
// ignore enter First space on edittext
public InputFilter ignoreFirstWhiteSpace() {
return new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (Character.isWhitespace(source.charAt(i))) {
if (dstart == 0)
return "";
}
}
return null;
}
};
}
No need to write android:digits property on XML无需在 XML 上编写 android:digits 属性
remove this line删除这一行
android:digits="_,qwertzuiopasdfghjklyxcvbnm,QWERTYUIOPASDFGHJKLZXCVBNM,0123456789"
This works for me这对我有用
android:inputType="textPersonName"
android:digits= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_-!@#$%^*()"
why cant you use editText.getText().trim();为什么不能使用 editText.getText().trim(); function while using the EditText data
使用 EditText 数据时的功能
If you want to filter input characters in your EditText, you need to use InputFilter.如果要过滤 EditText 中的输入字符,则需要使用 InputFilter。 Here is example.
这是示例。 //Allow only letters or digits
//只允许字母或数字
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
EditText text = (EditText)findViewById(R.id.edittext1);
text.setFilters(new InputFilter[]{filter});
Use this.用这个。 If the character at starting Position is a space, set textView Text To blank
如果起始位置的字符是空格,则将 textView Text 设置为空白
editText1_in_row.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length()>0 && s.subSequence(0, 1).toString().equalsIgnoreCase(" ")) {
editText1_in_row.setText(""); }
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
A slight variation on https://stackoverflow.com/users/2868352/abhishek-v answer. https://stackoverflow.com/users/2868352/abhishek-v答案略有不同。
public class NoInitialSpaceFilter implements InputFilter {
@Override
public CharSequence filter(final CharSequence source, final int start, final int end, final Spanned dest, final int dstart, final int dend) {
if (dstart == 0) {
for (int i = start; i < end; i++) {
if (Character.isSpaceChar(source.charAt(i))) {
return "";
}
}
}
return null;
}
}
Usage:用法:
editText.setFilters(new InputFilter[]{new NoInitialSpaceFilter});
Simply restrict the user to type space as others said on start only:简单地限制用户输入空格,就像其他人在开始时所说的那样:
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String text = createPL.getText().toString();
//restrict space for first char
if (text.startsWith(" ")) {
edittext.setText(text.trim());
}
}
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.,_-!@#$()+=><:;?"
This is filter I have used for Name validation in EditText.这是我在 EditText 中用于名称验证的过滤器。 First letter CAPS, not space and special character.
首字母大写,不是空格和特殊字符。 After completing the word not allow more than a single space.
完成单词后不允许有多个空格。
public void setNameFilter() {
InputFilter filter = new InputFilter() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (dend == 0) {
if (Character.isSpaceChar(source.charAt(i)) ||
!Character.isAlphabetic(source.charAt(i))) {
return Constants.Delimiter.BLANK;
} else {
return String.valueOf(source.charAt(i)).toUpperCase();
}
} else if (Character.isSpaceChar(source.charAt(i)) &&
String.valueOf(dest).endsWith(Constants.Delimiter.ONE_SPACE)) {
return Constants.Delimiter.BLANK;
} else if ((!Character.isSpaceChar(source.charAt(i)) &&
!Character.isAlphabetic(source.charAt(i)))) {
return Constants.Delimiter.BLANK;
}
}
return null;
}
};
editText.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(Constants.Length.NAME_LENGTH)});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.