【问题标题】:Xamarin Android - Entry with icon - SetCompoundDrawablesWithIntrinsicBounds -- TOUCH/Click event?Xamarin Android - 带有图标的条目 - SetCompoundDrawablesWithIntrinsicBounds - TOUCH/Click 事件?
【发布时间】:2019-08-22 02:07:36
【问题描述】:

我想用右侧的图标进入。

我使用 Xamarin 表单 + PCL

我已经在IOS和Android中实现了主要部分。

我在条目中添加了图标,这看起来很棒............

但是! 当用户单击此图标时,我需要捕捉。

在 Ios 中我做的很简单....但是 Android .. 我做不到(((

Xamarin.Droid CUSTOM RENDER 进入

  FormsEditText editText = Control;
            if(!string.IsNullOrEmpty(element.Image))
            {            
                Drawable d = GetDrawable(element.Image);

                switch(element.ImageAlignment)
                {
                    case ImageAlignment.Left:
                         editText.SetCompoundDrawablesWithIntrinsicBounds(d, null, null, null);
                        break;

                    case ImageAlignment.Right:
                        editText.SetCompoundDrawablesWithIntrinsicBounds(null, null, d, null);
                        break;
                }
            }


 private BitmapDrawable GetDrawable(string imageEntryImage)
        {
            int resID = Resources.GetIdentifier(imageEntryImage, "drawable", Context.PackageName);
            Android.Graphics.Drawables.Drawable drawable = ContextCompat.GetDrawable(Context, resID);
            Bitmap bitmap = ((BitmapDrawable)drawable).Bitmap;
            var im =  new BitmapDrawable(Resources, Bitmap.CreateScaledBitmap(bitmap, element.ImageWidth * 4, element.ImageHeight * 4, true));
            return im;
        }

所以我需要在这个图标上捕捉点击/触摸事件。

谢谢。

【问题讨论】:

  • 如果以下解决方案不起作用,请告诉我,以便我添加答案!

标签: xamarin xamarin.android


【解决方案1】:

在此图标上捕捉点击/触摸事件

这里有一个获取方法(例如,带有左图标的条目):

在您的 Xamarin.Droid CUSTOM RENDER 中输入

protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
           //Resource.Drawable.ic_action_info is your image resId
            Control.SetCompoundDrawablesRelativeWithIntrinsicBounds(Resource.Drawable.ic_action_info, 0, 0, 0);
            Control.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;
                editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(Resource.Drawable.ic_action_info, 0, 0, 0);
                if (editText.GetCompoundDrawables()[0] != null)
                {
                    //If the region on which i tapped is the region with the icon
                    if (e.RawX <=editText.GetCompoundDrawables()[0].Bounds.Width())
                    {
                        Toast.MakeText(v.Context,"icon",ToastLength.Short).Show();
                        return true;
                    }
                }
            }

            return false;
        }
    }

【讨论】:

    【解决方案2】:

    我认为下面的代码中存在一个问题,即点击位置与图标位置进行比较。这对你有用吗?

    if (e.RawX

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多