Android 带清除功能的输入框控件EditTextWithDel
记录下一个非常有用的小控件EditTextWithDel。就是在Android系统的输入框右边增加一个小图标。点击小图标能够清除输入框里面的内容,由于Android原生EditText不具备此功能,所以要想实现这一功能我们须要重写EditText。
效果图例如以下:
基本的思路就是为右边的图片设置监听。点击右边的图片清除输入框的内容并隐藏删除图标,由于我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件,用输入框的的onTouchEvent()方法来模拟.
package com.xiaolijuan.edittextwithdeldome;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.EditText;
/**
* @author: adan
* @description: 自己定义带有删除功能的EditText
* @projectName: EditTextWithDelDome
* @date: 2016-02-28
* @time: 23:34
*/
public class EditTextWithDel extends EditText {
private final static String TAG = "EditTextWithDel";
private Drawable imgInable;
private Drawable imgAble;
private Context mContext;
public EditTextWithDel(Context context) {
super(context);
mContext = context;
init();
}
public EditTextWithDel(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
init();
}
public EditTextWithDel(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}
private void init() {
imgAble = mContext.getResources().getDrawable(
R.mipmap.icon_delete_gray);
addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
setDrawable();
}
});
setDrawable();
}
// 设置删除图片
private void setDrawable() {
if (length() < 1) {
setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
} else {
setCompoundDrawablesWithIntrinsicBounds(null, null, imgAble, null);
}
}
// 处理删除事件
@Override
public boolean onTouchEvent(MotionEvent event) {
if (imgAble != null && event.getAction() == MotionEvent.ACTION_UP) {
int eventX = (int) event.getRawX();
int eventY = (int) event.getRawY();
Log.e(TAG, "eventX = " + eventX + "; eventY = " + eventY);
Rect rect = new Rect();
getGlobalVisibleRect(rect);
rect.left = rect.right - 50;
if (rect.contains(eventX, eventY))
setText("");
}
return super.onTouchEvent(event);
}
@Override
protected void finalize() throws Throwable {
super.finalize();
}
}
setDrawable()方法,setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)来在上、下、左、右设置图标,假设不想在某个地方显示,则设置为null。
接下来我们来使用它设置Activity的布局。一个我们自己定义的输入框,一个button
<?
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="25dp"
android:background="#ffffff">
<ImageView
android:id="@+id/img"
android:layout_width="25dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:src="@mipmap/ic_launcher" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#56AB55" />
<com.xiaolijuan.edittextwithdeldome.EditTextWithDel
android:id="@+id/et_phoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="2dp"
android:layout_toRightOf="@+id/img"
android:background="#ffffff"
android:hint="请输入手机号码"
android:maxLength="11"
android:phoneNumber="true"
android:singleLine="true" />
</RelativeLayout>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:background="#56AB55"
android:text="确定" />
</LinearLayout>
然后就是界面代码的编写。主要測试下输入框
package com.xiaolijuan.edittextwithdeldome;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText userName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText) findViewById(R.id.et_phoneNumber);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(userName.getText().toString())){
Toast.makeText(getApplicationContext(),"手机号码为空",Toast.LENGTH_LONG).show();
return;
}
Toast.makeText(getApplicationContext(),userName.getText().toString(),Toast.LENGTH_LONG).show();
}
});
}
}
Android 带清除功能的输入框控件EditTextWithDel的更多相关文章
- (转载) Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框
Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框 标签: Android清除功能EditText仿IOS的输入框 2013-09-04 17:33 70865人阅读 ...
- Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框
转载请注明出处http://blog.csdn.net/xiaanming/article/details/11066685 今天给大家带来一个很实用的小控件ClearEditText,就是在Andr ...
- Android 带清除功能的输入框控件EditText
1.效果图 2.源码下载 http://download.csdn.net/detail/yanzi2015/8864603 3.相关博客 http://www.cnblogs.com/to ...
- 实现带查询功能的Combox控件
前言 ComBox 还可以实现查询功能,通过设置 ComBox 控件的 AutoCompleteSource 属性和 AutoCompleteMode 属性,可以实现从 Combox 控件中查询已存在 ...
- 实现带查询功能的ComboBox控件
实现效果: 知识运用: ComboBox控件的AutoCompleteMode属性 public AutoCompleteMode AutoCompleteMode{get;set;} //属性值为枚 ...
- 使用katalon自带Spy功能获取/验证控件Selector、XPath
背景 最近刚接手一个katalon编写的UI自动化项目,页面最近刚改版,已有用例很多查找元素失败.了解到katalon元素定位支持xpath,所以直接使用chrome开发者工具打开目标页面+获取xpa ...
- 微信小程序基础之input输入框控件
今天主要详写一下微信小程序中的Input输入框控件,输入框在程序中是最常见的,登录,注册,获取搜索框中的内容等等都需要,同时,还需要设置不同样式的输入框,今天的代码中都要相应的使用. input输入框 ...
- (转载)Android UI设计之AlertDialog弹窗控件
Android UI设计之AlertDialog弹窗控件 作者:qq_27630169 字体:[增加 减小] 类型:转载 时间:2016-08-18我要评论 这篇文章主要为大家详细介绍了Android ...
- Android support library支持包常用控件介绍(二)
谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现 Material Design设计效果,官方给出了Android support design library ...
随机推荐
- Spring CORS
转载:Spring MVC 4.2 增加 CORS 支持 http://spring.io/blog/2015/06/08/cors-support-in-spring-framework http: ...
- node中间层
node中间层 一.总结 1.node中间层作用:前端也是mvc,NodeJS之后,前端可以更加专注于视图层,而让更多的数据逻辑放在Node层处理 2.node中间层作用:当发现所有请求量太多应付不过 ...
- jQuery快速入门知识重点
1.jquery中attr与prop的区别 attr:是通过setAttribute 和 getAttribute来设置的使用的是DOM属性节点 prop:是通过document.getEle ...
- c#下halcon配置
1.在halcon中写入算子,实现函数过程 比如: read_image(Image,'D:/MyFile/halcon/数字识别/1.jpg') decompose3(Image, ImageR, ...
- 无状态会话bean(1)---定义
无状态会话bean用于完毕在单个方法的生命周期内的操作.无状态bean能够实现很多业务操作,可是每一个方法都不能假定不论什么其它的方法会在它之前调用.后半句的意思是如今的你可能不是刚才的你.明天的你可 ...
- Linux CentOS PhpMyAdmin安装--转载
原文地址:https://www.centos.bz/2011/04/linux-centos-phpmyadmin-install/ 安装好PHP,Apache和MySQL程序后,为了管理MySQL ...
- OFFICE2013 打开两个word文档卡死的解决办法
这几天Office打开一个word好好的,两个就直接卡死了,百度了一下: 原文地址: http://hi.baidu.com/pjhero/item/ca326e3dcfebebb9623aff6e ...
- NLP系列(1)_从破译外星人文字浅谈自然语言处理的基础
作者:龙心尘 &&寒小阳 时间:2016年1月. 出处: http://blog.csdn.net/longxinchen_ml/article/details/50543337, h ...
- IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404
IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404 .embody{ padding:10px 10px 10px; margin:0 -20px; borde ...
- php实现记忆化递归--以斐波那契数列为例(还是以边学边做为主,注重练习)
php实现记忆化递归--以斐波那契数列为例(还是以边学边做为主,注重练习) 一.总结 1.递归不优化的话,30层开外就有点吃力了 2.php因为定义变量的时候不用定义变量类型,所以数组里面的类型也是p ...