Ste*_*fan 165 android android-edittext
我需要在一系列EditTexts上进行表单输入验证.我使用OnFocusChangeListeners在用户输入每个之后触发验证,但这不符合上一个EditText的需要.
如果在键入最终的EditText时单击"完成"按钮,则InputMethod将断开连接,但技术上焦点永远不会丢失在EditText上(因此永远不会发生验证).
什么是最好的解决方案?
当InputMethod从每个EditText解除绑定而不是焦点更改时,我应该监视吗?如果是这样,怎么样?
Nik*_*hil 151
你为什么不用TextWatcher?
由于您有许多EditText盒子需要验证,我认为以下内容适合您:
android.text.TextWatcher界面txt1.addTextChangedListener(this);
txt2.addTextChangedListener(this);
txt3.addTextChangedListener(this);
Run Code Online (Sandbox Code Playgroud)
afterTextChanged(Editable s)方法@Override
public void afterTextChanged(Editable s) {
// validation code goes here
}
Run Code Online (Sandbox Code Playgroud)
这Editable s并没有真正帮助找到哪个EditText框的文本被更改.但是你可以直接检查EditText框的内容
String txt1String = txt1.getText().toString();
// Validate txt1String
Run Code Online (Sandbox Code Playgroud)
用同样的方法.我希望我很清楚,如果我是,它会有所帮助!:)
编辑:有关更清洁的方法,请参阅下面的克里斯托弗佩里的答案.
Chr*_*rry 119
TextWatcher对我的口味有点冗长,所以我做了一些容易吞下的东西:
public abstract class TextValidator implements TextWatcher {
private final TextView textView;
public TextValidator(TextView textView) {
this.textView = textView;
}
public abstract void validate(TextView textView, String text);
@Override
final public void afterTextChanged(Editable s) {
String text = textView.getText().toString();
validate(textView, text);
}
@Override
final public void beforeTextChanged(CharSequence s, int start, int count, int after) { /* Don't care */ }
@Override
final public void onTextChanged(CharSequence s, int start, int before, int count) { /* Don't care */ }
}
Run Code Online (Sandbox Code Playgroud)
只需像这样使用它:
editText.addTextChangedListener(new TextValidator(editText) {
@Override public void validate(TextView textView, String text) {
/* Validation code here */
}
});
Run Code Online (Sandbox Code Playgroud)
Rag*_*har 24
为了减少验证逻辑的冗长,我为Android编写了一个库.它使用注释和内置规则来处理大多数日常验证.有限制,例如@TextRule,@NumberRule,@Required,@Regex,@Email,@IpAddress,@Password,等,
您可以将这些注释添加到UI窗口小部件引用中并执行验证.它还允许您异步执行验证,这对于检查远程服务器的唯一用户名等情况非常理想.
项目主页上有一个关于如何使用注释的示例.您还可以阅读相关的博客文章,其中我编写了有关如何编写自定义验证规则的示例代码.
这是一个描述库使用情况的简单示例.
@Required(order = 1)
@Email(order = 2)
private EditText emailEditText;
@Password(order = 3)
@TextRule(order = 4, minLength = 6, message = "Enter at least 6 characters.")
private EditText passwordEditText;
@ConfirmPassword(order = 5)
private EditText confirmPasswordEditText;
@Checked(order = 6, message = "You must agree to the terms.")
private CheckBox iAgreeCheckBox;
Run Code Online (Sandbox Code Playgroud)
该库是可扩展的,您可以通过扩展Rule类来编写自己的规则.
Dan*_*son 11
这是很好的解决方案,从这里
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++) {
String checkMe = String.valueOf(source.charAt(i));
Pattern pattern = Pattern.compile("[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789_]*");
Matcher matcher = pattern.matcher(checkMe);
boolean valid = matcher.matches();
if(!valid){
Log.d("", "invalid");
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[]{filter});
Run Code Online (Sandbox Code Playgroud)
谷歌最近推出了设计支持库,有一个名为TextInputLayout的组件,它支持通过setErrorEnabled(boolean)和显示错误setError(CharSequence).
如何使用它?
第1步:使用TextInputLayout包装EditText:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/layoutUserName">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hint"
android:id="@+id/editText1" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
第2步:验证输入
// validating input on a button click
public void btnValidateInputClick(View view) {
final TextInputLayout layoutUserName = (TextInputLayout) findViewById(R.id.layoutUserName);
String strUsername = layoutLastName.getEditText().getText().toString();
if(!TextUtils.isEmpty(strLastName)) {
Snackbar.make(view, strUsername, Snackbar.LENGTH_SHORT).show();
layoutUserName.setErrorEnabled(false);
} else {
layoutUserName.setError("Input required");
layoutUserName.setErrorEnabled(true);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在我的Github存储库上创建了一个示例,如果您愿意,请查看示例!
我发现InputFilter更适合验证android上的文本输入.
这是一个简单的例子: 如何使用InputFilter来限制Android中EditText中的字符?
您可以添加Toast以向用户反馈您的限制.还要检查android:inputType标签.
我需要进行字段内验证而不是字段间验证,以测试我的值在一种情况下是无符号浮点值,在另一种情况下是有符号浮点值.这似乎对我有用:
<EditText
android:id="@+id/x"
android:background="@android:drawable/editbox_background"
android:gravity="right"
android:inputType="numberSigned|numberDecimal"
/>
Run Code Online (Sandbox Code Playgroud)
注意,"numberSigned | numberDecimal"中不能有任何空格.例如:"numberSigned | numberDecimal"将不起作用.我不知道为什么.
public void onClickNext(View v) {
FormEditText[] allFields = { etFirstname, etLastname, etAddress, etZipcode, etCity };
boolean allValid = true;
for (FormEditText field: allFields) {
allValid = field.testValidity() && allValid;
}
if (allValid) {
// YAY
} else {
// EditText are going to appear with an exclamation mark and an explicative message.
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
295919 次 |
| 最近记录: |