当前位置: 移动技术网 > IT编程>移动开发>Android > Android带清除功能的输入框控件EditTextWithDel

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

旧个人所得税税率表,很狠撸,德奥达里奥

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

源码下载:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网