记录下一个非常有用的小控件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();
}
});
}
}

Dome下载

Android 带清除功能的输入框控件EditTextWithDel的更多相关文章

  1. (转载) Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框

    Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框 标签: Android清除功能EditText仿IOS的输入框 2013-09-04 17:33 70865人阅读  ...

  2. Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框

    转载请注明出处http://blog.csdn.net/xiaanming/article/details/11066685 今天给大家带来一个很实用的小控件ClearEditText,就是在Andr ...

  3. Android 带清除功能的输入框控件EditText

    1.效果图      2.源码下载 http://download.csdn.net/detail/yanzi2015/8864603 3.相关博客 http://www.cnblogs.com/to ...

  4. 实现带查询功能的Combox控件

    前言 ComBox 还可以实现查询功能,通过设置 ComBox 控件的 AutoCompleteSource 属性和 AutoCompleteMode 属性,可以实现从 Combox 控件中查询已存在 ...

  5. 实现带查询功能的ComboBox控件

    实现效果: 知识运用: ComboBox控件的AutoCompleteMode属性 public AutoCompleteMode AutoCompleteMode{get;set;} //属性值为枚 ...

  6. 使用katalon自带Spy功能获取/验证控件Selector、XPath

    背景 最近刚接手一个katalon编写的UI自动化项目,页面最近刚改版,已有用例很多查找元素失败.了解到katalon元素定位支持xpath,所以直接使用chrome开发者工具打开目标页面+获取xpa ...

  7. 微信小程序基础之input输入框控件

    今天主要详写一下微信小程序中的Input输入框控件,输入框在程序中是最常见的,登录,注册,获取搜索框中的内容等等都需要,同时,还需要设置不同样式的输入框,今天的代码中都要相应的使用. input输入框 ...

  8. (转载)Android UI设计之AlertDialog弹窗控件

    Android UI设计之AlertDialog弹窗控件 作者:qq_27630169 字体:[增加 减小] 类型:转载 时间:2016-08-18我要评论 这篇文章主要为大家详细介绍了Android ...

  9. Android support library支持包常用控件介绍(二)

    谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现 Material Design设计效果,官方给出了Android support design library ...

随机推荐

  1. Linux下改动Oracle数据库字符集命令

    常见情形:从server备份Oracle数据库后再到本地机器上还原Oracle数据库的时候常常会碰见数据库字符编码不一致的情况,能够用下面命令来改动本地的Oracle数据库字符编码,然后顺利还原Ora ...

  2. stackFromBottom-listview 内容从底部开始填充

    今天遇到了一个问题,就是listview虽然占满了整个屏幕,但是,当它的内容只有几条的时候,它会从底部开始显示,上面留有空白.后来进入xml发现,listview有个属性stackFromBottom ...

  3. 【博客之星】CSDN2013博客之星--分析和预测

    本文纯属个人见解,多有得罪啊! 具体结果,还是看最后CSDN给的结果吧! 昵称 名字 题材 质量 数量 知名度 预测 阳光岛主 杨刚 Python,Clojure,SAE 很高 346+ 很大 一定( ...

  4. PatentTips - Method for guest operating system integrity validation

    BACKGROUND The embodiments relate to guest operating system integrity validation, and more particula ...

  5. 微信支付v2开发(5) 订单查询

    本文介绍微信支付中订单查询功能的实现. 作者:方倍工作室 地址:http://www.cnblogs.com/txw1958/p/wxpay-order-query.html 一.订单查询 因为某一方 ...

  6. 快速创建SSH信任实现无密码登录

    1. 生成本机的公私钥匙对[oracle@Oracle11_2 scripts]$ -t rsa Generating public/private rsa key pair. Enter file ...

  7. python3 pygame 坦克自动移动

    让坦克自动跑起来 这里需要一个坦克的图. 放到与脚本同一目录. 好,我们就让这个坦克自动跑. 下面上代码: # !/usr/bin/env python # -*- coding:utf-8 -*- ...

  8. report_timing

    report_timing   -max_path   2 会报告两条路径,但不一定是最差的路径 report_timing  -nworst  2  -max_path   2 会报告两条最差的路径

  9. 读文件头数据判断 PE 文件格式和类型

    namespace X.Reflection { using System; using System.IO; public static partial class ReflectionX { pu ...

  10. 对Linux下常用头文件总结

    asm.current.h    定义全局项current ,其指向结构体struct  task_struct linux/sched.h    定义结构体task_struct ,只要包含此头文件 ...