【问题标题】:Show/Hide Icon in Entry does not shown条目中的显示/隐藏图标未显示
【发布时间】:2020-10-09 09:35:59
【问题描述】:

我正在尝试为我的条目创建一个显示/隐藏密码。

在 Android 项目上添加此代码后。显示和隐藏效果正在工作。但是显示和隐藏效果的图标在入口内是不可见的。

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;
        }
    }
}

我做得对吗?可能是什么原因导致条目内没有显示图标,解决方法是什么?

【问题讨论】:

    标签: android xamarin.forms


    【解决方案1】:

    好吧,我想我找到了答案。

    问题是这样的,

      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 函数用于调整条目内的图标大小。

    【讨论】:

    • 接受您的回答,以便其他人可以尝试使用它
    猜你喜欢
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多