我想在右侧使用Icon进入。
我使用Xamarin Forms + 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.Android CUSTOM RENDERER中进入
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;
}
}