Android 条目中的显示/隐藏图标未显示
我正在尝试为我的条目创建显示/隐藏密码 在Android项目上添加此代码之后。显示和隐藏效果正在工作。但是显示和隐藏效果的图标在条目内不可见Android 条目中的显示/隐藏图标未显示,android,xamarin.forms,Android,Xamarin.forms,我正在尝试为我的条目创建显示/隐藏密码 在Android项目上添加此代码之后。显示和隐藏效果正在工作。但是显示和隐藏效果的图标在条目内不可见 using Android.Graphics.Drawables; using Android.Text.Method; using Android.Views; using Android.Widget; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; [assembly: Reso
using Android.Graphics.Drawables;
using Android.Text.Method;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ResolutionGroupName("Xamarin")]
[assembly: ExportEffect(typeof(ShowHidePassEx.Droid.Effects.ShowHidePassEffect), "ShowHidePassEffect")]
namespace ShowHidePassEx.Droid.Effects
{
public class ShowHidePassEffect : PlatformEffect
{
protected override void OnAttached()
{
ConfigureControl();
}
protected override void OnDetached()
{
}
private void ConfigureControl()
{
EditText editText = ((EditText)Control);
editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(0,0, Resource.Drawable.avd_show_password,0);
editText.SetOnTouchListener(new OnDrawableTouchListener());
}
}
public class OnDrawableTouchListener : Java.Lang.Object, Android.Views.View.IOnTouchListener
{
public bool OnTouch(Android.Views.View v, MotionEvent e)
{
if (v is EditText && e.Action == MotionEventActions.Up)
{
EditText editText = (EditText)v;
if (e.RawX >= (editText.Right - editText.GetCompoundDrawables()[2].Bounds.Width()))
{
if (editText.TransformationMethod == null)
{
editText.TransformationMethod = PasswordTransformationMethod.Instance;
editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, Resource.Drawable.avd_show_password, 0);
}
else
{
editText.TransformationMethod = null;
editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, Resource.Drawable.avd_hide_password, 0);
}
return true;
}
}
return false;
}
}
}
我做得对吗?没有在条目中显示图标的可能原因是什么?解决方法是什么?好吧,我想我找到了答案 问题是,
editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, Resource.Drawable.avd_hide_password, 0);
我把它改成这个
editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(null, null, GetDrawable(proyekto4juan.Droid.Resource.Mipmap.HidePass), null);
对于GetDrawable
函数
public static BitmapDrawable GetDrawable(int resID)
{
var context = global::Android.App.Application.Context;
var drawable = ContextCompat.GetDrawable(context, resID);
var bitmap = ((BitmapDrawable)drawable).Bitmap;
return new BitmapDrawable(Resources.System, Bitmap.CreateScaledBitmap(bitmap, 60, 60, true));
}
GetDrawable
功能用于调整条目内图标的大小。接受您的答案,以便其他人可以尝试使用它