Xamarin Android自定义文本框
2024-09-25 06:36:23
xamarin android 自定义文本框简单的用法
关键点在于,监听EditText的内容变化,不同于java中文本内容变化去调用EditText.addTextChangedListener(mTextWatcher);为EditText设置内容变化监听!
简单来说就是添加一个AfterTextChanged 事件就OK了,这是最简单的一种做法,当然你要想java那样去监听也可以的。
来看一下实现的效果图:
自定义的EditText::CustomerEditText.cs
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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditTextListener.CustomerEditText
android:id="@+id/edit_search"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="10dp"
android:background="@drawable/bg_frame_search"
android:hint="带删除按钮的EditText"
android:maxLength="50"
android:padding="5dp"
android:singleLine="true"
android:textColor="#000000" />
</LinearLayout>
自定义的背景样式:bg_frame_search.xml
<?xml version="1.0" encoding="utf-8" ?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/background_white"/>
<corners android:radius="5dp"/>
<stroke android:width="1px" android:color="@color/frame_search"/>
</shape>
颜色值我就不贴了,自己写几个颜色就OK了。
代码非常简单,当然这是最简单的实现方法,还有很多功能小功能都没实现。所以后面继续努力吧。
最新文章
- 关于 FPGA 内部信号扇入扇出
- P1159岳麓山上打水
- 字符编码浅识:关于Unicode与UTF-8
- angularJS自定义属性作为条件中转
- 确实是非常实用的Ubuntu命令
- linux重新增加硬盘容量
- 框架搭建资源 (一) V(视图)C(控制)模式
- BZOJ 1198: [HNOI2006]军机调度(搜索)
- 微信小程序(组件demo)以及预览方法:(小程序交流群:604788754)
- Kali Linux常用服务配置教程安装及配置DHCP服务
- JAVA&#160;利用MyEclipse结合TestNG测试框架进行单元测试
- react路由的安装及格式和使用方法
- django-debug-toolbar和Django 日志配置
- C++生成静态库
- Shell脚本 | 性能测试之启动流量
- PAT甲题题解-1105. Spiral Matrix (25)-(模拟顺时针矩阵)
- 【工具】用命令行与Python使用YARA规则
- Suricata开源IDS安装与配置
- MyBatis Generator去掉生成的注解
- 开源IDS系列--解决barnyard2 停止运行 libmysqlclient.so.16.0.0
热门文章
- 基于 nodejs 的 webSockt (socket.io)
- Pyhton函数篇(一)之函数中的形参与实参
- 【转】javascript中的LHS与RHS
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
- linux上安装php7 memcache扩展 和 安装服务端memcached
- Akka(41): Http:DBTable-rows streaming - 数据库表行交换
- 移动端H5制作安卓和IOS的坑 持续更新...
- HTTP协议之URL
- 网页设计——2. html入门
- ubuntu16.04 Qt5.8 如何使用opecv3.2