Xamarin Android-带有图标的条目-SetCompoundDrawableSwithinInstincBounds-触摸/点击事件?
我想用右边的图标进入 我使用Xamarin表单+PCL 我已经在IOS和Android中实现了主要部分 我在条目中添加了图标,这看起来棒极了 但是! 我需要捕捉当用户点击这个图标 在Ios中,我做的很简单…但是Android。。我不能做这件事((( Droid自定义渲染以供输入Xamarin Android-带有图标的条目-SetCompoundDrawableSwithinInstincBounds-触摸/点击事件?,xamarin,xamarin.android,Xamarin,Xamarin.android,我想用右边的图标进入 我使用Xamarin表单+PCL 我已经在IOS和Android中实现了主要部分 我在条目中添加了图标,这看起来棒极了 但是! 我需要捕捉当用户点击这个图标 在Ios中,我做的很简单…但是Android。。我不能做这件事((( Droid自定义渲染以供输入 FormsEditText editText = Control; if(!string.IsNullOrEmpty(element.Image)) {
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;
}
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;
}
}
因此,我需要捕获此图标上的点击/触摸事件
多谢各位
捕获此图标上的单击/触摸事件
下面是获取它的方法(例如,带有左图标的条目):
在Xamarin.Droid中自定义渲染以供输入
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;
}
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;
}
}
protected override void OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
//Resource.Drawable.ic_action_info是您的图像剩余
Control.SetCompoundDrawablesRelativeWithIntrinsicBounds(Resource.Drawable.ic_action_info,0,0,0);
SetOnTouchListener(新的OnDrawableTouchListener());
}
}
OnDrawableTouchListener的公共类:Java.Lang.Object、Android.Views.View.IOnTouchListener
{
公共bool OnTouch(Android.Views.View v,MotionEvent e)
{
if(v是EditText&&e.Action==MotionEventActions.Up)
{
EditText EditText=(EditText)v;
editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(Resource.Drawable.ic_action_info,0,0,0);
如果(editText.GetCompoundDrawables()[0]!=null)
{
//如果我点击的区域是带有图标的区域
如果(e.RawX
捕获此图标上的单击/触摸事件
下面是获取它的方法(例如,带有左图标的条目):
在Xamarin.Droid中自定义渲染以供输入
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;
}
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;
}
}
protected override void OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
//Resource.Drawable.ic_action_info是您的图像剩余
Control.SetCompoundDrawablesRelativeWithIntrinsicBounds(Resource.Drawable.ic_action_info,0,0,0);
SetOnTouchListener(新的OnDrawableTouchListener());
}
}
OnDrawableTouchListener的公共类:Java.Lang.Object、Android.Views.View.IOnTouchListener
{
公共bool OnTouch(Android.Views.View v,MotionEvent e)
{
if(v是EditText&&e.Action==MotionEventActions.Up)
{
EditText EditText=(EditText)v;
editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(Resource.Drawable.ic_action_info,0,0,0);
如果(editText.GetCompoundDrawables()[0]!=null)
{
//如果我点击的区域是带有图标的区域
如果(e.RawX我相信下面的代码中有一个问题,点击位置与图标位置进行了比较。这对你有用吗
如果(e.RawX我相信下面的代码中有一个问题,点击位置与图标位置进行了比较。这对你有用吗
如果(e.RawX)下面的解决方案不起作用,请告诉我,以便我可以添加答案!如果下面的解决方案不起作用,请告诉我,以便我可以添加答案!