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

Android带清除功能的输入框控件EditTextWithDel,第1张

概述记录下一个很实用的小控件EditTextWithDel,就是在Android系统的输入框右边加入一个小图标,点击小图标可以清除输入框里面的内容,由于Android原生EditText不具备此功能,所以要想实现这一功能我们需要重写EditText。

记录下一个很实用的小控件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 count,int after) {  }  @OverrIDe  public voID afterTextChanged(Editable s) {  setDrawable();  } }); setDrawable(); } // 设置删除图片 private voID setDrawable() { if (length() < 1) {  setCompoundDrawablesWithIntrinsicBounds(null,null,null); } else {  setCompoundDrawablesWithIntrinsicBounds(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带清除功能的输入框控件EditTextWithDel全部内容,希望文章能够帮你解决Android带清除功能的输入框控件EditTextWithDel所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1148290.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存