本文共 2105 字,大约阅读时间需要 7 分钟。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Android.App;using Android.Content;using Android.OS;using Android.Runtime;using Android.Views;using Android.Widget;using Android.Graphics.Drawables;using Android.Util;using Java.Lang;using Android.Text;using Android.Graphics;namespace EditTextListener{ public class CustomerEditText:EditText { private Drawable imgClear; private Context Context; public CustomerEditText(Context context, IAttributeSet attrs) : base(context, attrs) { this.Context = context; init(); } private void init() { imgClear = Context.Resources.GetDrawable(Resource.Drawable.del); AfterTextChanged += (s, e) => { setDrawable(); }; } //回执删除图片 private void setDrawable() { if (Length() < 1) SetCompoundDrawablesWithIntrinsicBounds(null, null, null, null); else SetCompoundDrawablesWithIntrinsicBounds(null,null,imgClear,null); } //当触摸范围在右侧时,触发删除方法,隐藏叉叉 public override bool OnTouchEvent(MotionEvent e) { if (imgClear != null && e.Action == MotionEventActions.Up) { int eventX = (int)e.RawX; int eventY = (int)e.RawY; Rect rect = new Rect(); GetGlobalVisibleRect(rect); rect.Left = rect.Right - 100; if (rect.Contains(eventX, eventY)) { Text=string.Empty; } } return base.OnTouchEvent(e); } }}布局文件Main.axml
自定义的背景样式:bg_frame_search.xml
颜色值我就不贴了,自己写几个颜色就OK了。
转载地址:http://fbqya.baihongyu.com/