Android自定义控件
来源:互联网 发布:违章查询软件下载 编辑:程序博客网 时间:2024/06/06 09:32
Android自定义控件 - 带清除功能的输入框
在业务需求中我们经常会用到输入框快捷删除输入内容的操作,然后Android的EditText控件是不带清除功能的,所以我们得自己写一个带清除功能的EditText。
先看看效果图:
实现该控件只需要两步:
- 自定义一个EditText(继承自EditText类)
- 布局文件中引用
下面贴代码:
- 自定义的EditText类 - EditTextWithDel
package cn.cqlwkj.lwinspection.view;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;import cn.cqlwkj.lwinspection.R;/** * Created by pengbo on 2017-09-21. * 带删除的输入框 */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.drawable.ic_delete); 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(); }}
注意:ic_delete是你自己使用的删除按钮的图片。
- 2、 在布局文件中使用自定义的控件
<cn.cqlwkj.lwinspection.view.EditTextWithDel android:id="@+id/edit_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_margin="10dp" android:layout_toRightOf="@+id/img" android:hint="xxx" android:inputType="numberDecimal" android:layout_weight="0.7" android:singleLine="true" android:background="@drawable/edit_shape"/>
至此,我们就完成了一个自定义输入框。
以上内容基于其他博文修改而来,原博文地址:http://blog.csdn.net/qq_20785431/article/details/50762834
阅读全文
0 0
- [Android自定义控件] Android自定义控件
- Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- Android 自定义控件 单页翻书控件
- android虚线控件---自定义控件
- android自定义控件实例 --控件
- Android自定义控件--组合控件
- android 控件 自定义组合控件
- 【android自定义控件】ProgressBar自定义
- Android自定义控件 自定义属性
- Android自定义控件 -- 自定义View
- Android自定义控件 -- 自定义ViewGroup
- Android 自定义控件 自定义标题栏
- Android 自定义控件-自定义进度条。
- 极乐技术周报(第二十七期)
- 【手把手教你全文检索】Lucene索引的【增、删、改、查】
- WOJ-74 ABC
- Openstack环境部署
- 二叉树的深度
- Android自定义控件
- hdu 1671 phone list Trie 树
- EasyPR + VS2015 + OpenCV330配置
- 点击条目webView加载网页
- ssh-keygen 的 详解
- 泛型1
- oracle中的游标的原理和使用详解
- springboot+mybatis
- java多态经典例子