Android带清除功能的输入框控件EditTextWithDel

所属分类: 软件编程 / Android 阅读数: 77
收藏 0 赞 0 分享

记录下一个很实用的小控件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的布局,一个我们自定义的输入框,一个按钮

<?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();
  }
 });
 }
}

源码下载:http://xiazai.jb51.net/201609/yuanma/EditTextWithDel(jb51.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多