Android之利用TextWatcher制作自定义编辑文本框
来源:互联网 发布:智能网络液晶平板电视 编辑:程序博客网 时间:2024/04/29 09:27
自定义编辑文本框怎么造呢?
利用Textwatcher观察者来自定义。
首先,我们看下代码:
<span style="font-size:18px;">package com.example.boom.messageproject.ui;import android.content.Context;import android.graphics.drawable.Drawable;import android.text.Editable;import android.text.TextWatcher;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.EditText;import com.example.boom.messageproject.R;/** * Created by Boom on 2016/8/2. */public class CustomEditText extends EditText { Context mcontext; Drawable img; public CustomEditText(Context context) { this(context, null); } public CustomEditText(Context context, AttributeSet attrs) { super(context, attrs); mcontext = context; init(); } private void init() { this.img = mcontext.getResources().getDrawable(R.mipmap.icon_clear); this.setSingleLine(true); this.addTextChangedListener(new CustomTextWatcher()); this.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { setDrawable(hasFocus); } }); } class CustomTextWatcher implements TextWatcher { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { setDrawable(true); } } private void setDrawable(boolean hasFouce) { if (length() >= 1 && hasFouce) { setCompoundDrawablesWithIntrinsicBounds(null, null, img, null); } else { setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); ; } } @Override public boolean onTouchEvent(MotionEvent event) { final int DRAWABLE_RIGHT = 2; Drawable rightIcon = getCompoundDrawables()[DRAWABLE_RIGHT]; if (rightIcon != null && event.getAction() == MotionEvent.ACTION_UP) { int leftEdgeOfRightDrawable = getRight() - getPaddingRight() - rightIcon.getBounds().width(); if (event.getRawX() >= leftEdgeOfRightDrawable) { setText(""); } } return super.onTouchEvent(event); } @Override protected void finalize() throws Throwable { img = null; super.finalize(); }}</span>功能性代码:
1.设置自定义观察者并添加监察者
<span style="font-size:18px;"> class CustomTextWatcher implements TextWatcher { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { setDrawable(true); } }</span>
<span style="font-size:18px;"> this.addTextChangedListener(new CustomTextWatcher());</span>2.设置图片位置的显示与不显示,同时包括切换焦点
<span style="font-size:18px;">private void setDrawable(boolean hasFouce) { if (length() >= 1 && hasFouce) { setCompoundDrawablesWithIntrinsicBounds(null, null, img, null); } else { setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); ; } }</span>
<span style="font-size:18px;"> this.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { setDrawable(hasFocus); } });</span>
3.处理点击删除图片 删除编辑框的内容
<span style="font-size:18px;"> @Override public boolean onTouchEvent(MotionEvent event) { final int DRAWABLE_RIGHT = 2; Drawable rightIcon = getCompoundDrawables()[DRAWABLE_RIGHT]; if (rightIcon != null && event.getAction() == MotionEvent.ACTION_UP) { int leftEdgeOfRightDrawable = getRight() - getPaddingRight() - rightIcon.getBounds().width(); if (event.getRawX() >= leftEdgeOfRightDrawable) { setText(""); } } return super.onTouchEvent(event); }</span>效果图:
0 0
- Android之利用TextWatcher制作自定义编辑文本框
- Android EditText 之 TextWatcher
- android之TextWatcher的监听
- Android学习之自定义TextWatcher来监听文本最大输入字数
- Android学习3之EditText可编辑的文本框组件
- Android 控件之三:EditView 可编辑文本框
- Android中编辑文本时使用TextWatcher和InputFilter.Length
- TextWatcher编辑框监听器
- TextWatcher编辑框监听器
- TextWatcher编辑框监听器
- TextWatcher编辑框监听器
- android TextWatcher
- Android TextWatcher
- Android中级第二讲--制作搜索页面,使用TextWatcher
- Xamarin Android自定义文本框
- Android自定义GridView之实现一个图片加多个文本框
- 222_编辑框textWatcher
- 双击单元格变成可编辑的文本框 - 制作过程
- 栈的PUSH POP序列
- Linux命令(2):目录
- 成员变量、实例变量、类变量、成员方法、实例方法、类方法的区别
- python问题Non-ASCII character '\xe5' in file Apriori.py on line 1, but no encoding declared 解决办法
- mssql数据库碰到的问题解决
- Android之利用TextWatcher制作自定义编辑文本框
- 机器学习7 拉格朗日KKT条件
- Tutorial on how to install apache spark on Windows
- Android中的进程有哪些,说出他们的优先级
- Eclipse 插件
- Linux冷门却很重要的命令(七)---lsof
- IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
- PHP程序员进阶学习书籍参考指南
- Windows Server 2008 R2 导入大批量的用户账户