Xamarin.Forms PDA Entry 带删除按钮功能

先看下效果:
在这里插入图片描述
在这里插入图片描述
1.介绍
在PDA开发中,经常会使用到文本框,但是Xamarin.Forms 版本4.2.x的不带清除按钮,本文章主要是自定义Entry按钮
2.开始
2.1 创建Xamarin.Forms项目过程省略
2.2 新建MyEntry.cs类,并集成Entry

namespace App1
{
    public class MyEntry:Entry
    {
    }
}

2.3 在项目Android下新建MyEntryRender.cs类

[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRender))]
namespace App1.Droid
{
    public class MyEntryRender:EntryRenderer
    {
        Drawable drawable;
        public MyEntryRender(Context context) : base(context) {
            drawable = GetDrawable("g_del_off20");
        }
        void ReseDelIcon(string text) {
            if (!string.IsNullOrEmpty(text))
            {
                Control.SetCompoundDrawablesRelativeWithIntrinsicBounds(null, null, drawable, null);
                Control.SetOnTouchListener(new OnDrawableTouchListener());
            }
            else {
                Control.SetCompoundDrawablesRelativeWithIntrinsicBounds(null, null, null, null);
            }
        }
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            if (Control != null) {
                ReseDelIcon(e.NewElement.Text);
            }
        }
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (Control != null) {
                if (e.PropertyName == "Text") {
                    Entry entry = (Entry)sender;
                    ReseDelIcon(entry.Text);
                }
            }
        }

        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,20,20, true));
            return im;
        }

    }
    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;
                Drawable drawable = editText.GetCompoundDrawables()[2];
                if (drawable != null) {
	                int  width=editText.Width;
                    if (e.GetX()>= width - 30 && e.GetX()<= width-10) {
                        editText.Text = "";
                        return true;
                    }
                }

            }
            return false;
        }
        
    }
}

2.4 在MainPage.xaml文件中,引用自定义控件

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:App1"
             mc:Ignorable="d"
             x:Class="App1.MainPage">
    <StackLayout>
        <!-- Place new controls here -->
        <local:MyEntry x:Name="entry0"/>
        <local:MyEntry x:Name="entry1"/>
        <local:MyEntry x:Name="entry2"/>
    </StackLayout>
</ContentPage>

请大家多多点赞,留言,多多赞赏!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上海小飞象

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值