所以我想知道为什么我的SetError方法没有在editText上显示我的图标?
我想要做的就是显示" Tick"用于正确验证的图标和一个" Cross"验证失败时的图标。
这是我的活动
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.LoginView);
// Create your application here
emailEditText = (EditText)FindViewById<EditText>(Resource.Id.etUserName);
passEditText = (EditText)FindViewById<EditText>(Resource.Id.etPass);
Drawable errorIcon = Resources.GetDrawable(Resource.Drawable.Icon);
Drawable correctIcon = Resources.GetDrawable(Resource.Drawable.Icon_sy);
//Android.Graphics.Drawables.Drawable.CreateFromPath("@icon");
emailEditText.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) =>
{
if (IsValidEmail(e.Text.ToString()))
{
//change icon to green
emailEditText.SetError("right", errorIcon);
//emailEditText.SetError(
}
else
{
//emailEditText.set
emailEditText.SetError("wrong", correctIcon);
//change icon to red
}
};
}
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#000000">
<FrameLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minWidth="25px"
android:minHeight="25px" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp">
<EditText
android:id="@+id/etUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittext_top_bg"
android:padding="10dp"
android:hint="Email"
android:textColorHint="#ADA6A6"
style="@style/DefaultTextBox"
android:drawableLeft="@drawable/email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/etPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittext_bottom_bg"
android:layout_marginTop="-2dp"
android:padding="10dp"
android:hint="Password"
android:textColorHint="#ADA6A6"
android:password="true"
style="@style/DefaultTextBox"
android:drawableLeft="@drawable/password"
android:inputType="textPassword" />
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1">
<CheckBox
android:text="Remember Me?"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/checkBox1" />
</LinearLayout>
<Button
android:id="@+id/btnSingIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="4dp"
android:text="Sign In"
style="@style/DefaultButtonText"
android:background="@drawable/button_default_bg"
local:MvxBind="Click LoginCommand" />
</LinearLayout>
</RelativeLayout>
你们有什么建议为什么我无法显示我的图标,或者是否有更简单的方法来验证将显示的editText以及消息和图标?
答案 0 :(得分:3)
如果您只想在验证上显示图标,那么您可以这样做:
if (IsValidEmail(e.Text.ToString()))
{
//change icon to green
emailEditText.SetCompoundDrawablesWithIntrinsicBounds(0, 0, Resource.Drawable.correctIcon, 0);
}
else
{
//change icon to red
emailEditText.SetCompoundDrawablesWithIntrinsicBounds(0, 0, Resource.Drawable.errorIcon, 0);
}
好吧,您应该为此创建一个方法,以便可以在任何地方使用它,或者您也可以为此行为覆盖基本SetError方法。