步骤如下
1:实现自定义控件的布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/et" android:background="@drawable/bg_edittext" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="left|center_vertical" android:drawableLeft="@drawable/search1" android:paddingRight="30dp" android:singleLine="true" /> <ImageButton android:id="@+id/ib" android:visibility="gone" android:layout_alignParentRight="true" android:layout_width="20dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_height="match_parent" android:scaleType="centerInside" android:background="#00000000" android:src="@drawable/delete" /> </RelativeLayout>
内容很简单,一个EditText 和 一个 ImageButton,其中android:drawableLeft="@drawable/search1"是输入框左侧图标
另外:android:background="@drawable/bg_edittext"这里引用自定义的背景,在我的另一篇文章里面有介绍:http://leoaioria.iteye.com/blog/2207587,如不用该背景,直接去掉这行或者修改即可。
2:定义好布局之后就是使用该布局并写清空内容和隐藏、显示删除按钮的事件了,代码如下
package com.hq.util; import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.ImageButton; import android.widget.LinearLayout; import com.hq.xbk2.R; public class EditClear extends LinearLayout{ ImageButton ib; EditText et; public EditClear(Context context) { super(context); } public EditClear(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.editclear, this, true); init(); } private void init() { ib = (ImageButton) findViewById(R.id.ib); et = (EditText) findViewById(R.id.et); et.addTextChangedListener(tw);// 为输入框绑定一个监听文字变化的监听器 // 添加按钮点击事件 ib.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { hideBtn();// 隐藏按钮 et.setText("");// 设置输入框内容为空 } }); } // 当输入框状态改变时,会调用相应的方法 TextWatcher tw = 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) { if (s.length() == 0) { hideBtn();// 隐藏按钮 } else { showBtn();// 显示按钮 } } }; public void hideBtn() { // 设置按钮不可见 if (ib.isShown()){ ib.setVisibility(View.GONE); } } public void showBtn() { // 设置按钮可见 if (!ib.isShown()){ ib.setVisibility(View.VISIBLE); } } }
注意该类:用到了TextWatcher ,有兴趣的同学可以查阅下这方面的资料
3:以上步骤完成后,自定义控件就实现了,接下来就是在其它布局文件中使用了,使用方法很简单
<com.hq.util.EditClear android:layout_width="match_parent" android:layout_height="35dp"/>
写上完整的类名就可以了
相关推荐
自定义一个带icon图标带删除按钮的EditText,简洁代码。
这个demo是关于android中自定义控件,在EditText两边放置加减按钮。 需求:带加减按钮的EditText控件。 如有BUG或者不 完善,欢迎小伙伴们提出~
自带清除按钮的EditText控件,非常好用,以后再也不用一个一个删除输入错误的内容了
带清除图标的 EditText ( 自定义EditText
有时候在注册时或遇到一些输入内容较长的EditText,输错内容删除慢是个问题,所以就需要来个clear按钮,我找到的这个是自定义的ClearEditText,内含EditText晃动动画代码,灰常实用。
主要介绍了Android 自定义EditText输入框带清空按钮的相关资料,需要的朋友可以参考下
该资源为自定义控件系列文章第三弹 继承控件 带清除按钮EditText的源码
自己封装的一个EditText两边放加减按钮Button来控制输入框的数值
Android EditText 带清空按钮 可直接使用
安卓自定义ListView中含有EditText控件的解决方法
栗子——自定义EditText实现右下角计数控件
Android Edittext文本输入框输入文本时,弹出一个删除图标,清空内容,代码只用到一个自定义的类,直接引用到xml文件中即可
Android 自定义控件 EditText输入框两边加减按钮Button
大家在做商城购物类的APP时,肯定遇到过需要自定义带加减的EditText 那么我上传的这个自定义的EditText: 1.可以根据个人的实际UI需求,修改成自己喜欢的颜色和形状。 2.里面的加和减的业务逻辑判断已完善,大部分...
Android自定义EditText,实现带清除功能的输入框
EditText里面镶嵌两个按钮,设计一个自定义组合控件。它的功能,通过两个按钮分别增加、减小EditText里面的数字
两个自定义控件的实现,想学习自定义控件的可以参考下
小白分享 Android自定义EditText
Android中EditText控件的基础使用,包含xml的基本配置以及在后台的操作函数。
简单实现带有一键删除按钮的自定义控件,并通过模拟计时器的操作判断用户是否输入完成了