本文介绍了如何验证 Xamarin/Android 应用程序中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有谁知道如何在 Xamarin/Android 应用中设置必填字段?
我的 Android 布局中有这个字段和按钮,我想确保在他们进入下一个活动之前输入了信息.
<按钮android:id="@+id/btnSearch"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/btnSearch"android:textSize="30sp"/>
如果在 iOS 上找到了这个,但在 Android 上找不到类似的东西:
在您的 Activity
上实现 View.IOnKeyListener
并检查/验证每个键条目
zipCodeEntry.SetFilters(new IInputFilter[] { this });public bool OnKey(View view, [GeneratedEnum] Keycode keyCode, KeyEvent e){if (view.Id == Resource.Id.zipCodeEntry){Log.Debug("V", keyCode.ToString());//按键验证}返回假;}
使用 InputFilter
(在您的活动中实现 IInputFilter
):
public ICharSequence FilterFormatted(ICharSequence source, int start, int end, Ispanned dest, int dstart, int dend){StringBuilder sb = new StringBuilder();for (int i = start; i
Does any one know how to make a field required in Xamarin/Android app?
I have this field and button in my Android Layout, and I'd like to make sure the info is entered before they go to the next activity.
<TextView
android:text="Zip Code:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/zipCodeLabel"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="@+id/btnSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btnSearch"
android:textSize="30sp" />
If found this for iOS, but can't find something similar for Android:https://developer.xamarin.com/recipes/ios/standard_controls/text_field/validate_input/
解决方案
There are a number of ways to validate input depending upon what your user experience requirements are.
android:digits
to restrict input:
<EditText
android:id="@+id/zipCodeEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="1234567890-"
/>
Validate after entry done and show error message:
button.Click += delegate {
if (!ValidateZipCode(zipCodeEntry.Text))
{
zipCodeEntry.Error = "Enter Valid USA Zip Code";
return;
}
DoSubmit();
protected bool ValidateZipCode(string zipCode)
{
string pattern = @"^\d{5}(\-\d{4})?$";
var regex = new Regex(pattern);
Log.Debug("V", regex.IsMatch(zipCode).ToString());
return regex.IsMatch(zipCode);
}
Implement View.IOnKeyListener
on your Activity
and check/validate every key entry
zipCodeEntry.SetFilters(new IInputFilter[] { this });
public bool OnKey(View view, [GeneratedEnum] Keycode keyCode, KeyEvent e)
{
if (view.Id == Resource.Id.zipCodeEntry)
{
Log.Debug("V", keyCode.ToString()); // Validate key by key
}
return false;
}
Use an InputFilter
(Implement IInputFilter
on your activity):
public ICharSequence FilterFormatted(ICharSequence source, int start, int end, ISpanned dest, int dstart, int dend)
{
StringBuilder sb = new StringBuilder();
for (int i = start; i < end; i++)
{
if ((Character.IsDigit(source.CharAt(i)) || source.CharAt(i) == '-'))
sb.Append(source.CharAt(i));
}
return (sb.Length() == (end - start)) ? null : new Java.Lang.String(sb.ToString());
}
这篇关于如何验证 Xamarin/Android 应用程序中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!