Android之利用TextWatcher制作自定义编辑文本框
2016-08-03 11:02
337 查看
自定义编辑文本框怎么造呢?
利用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;">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 = getCompoundDrawabl
a820
es()[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>效果图:
利用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 = getCompoundDrawabl
a820
es()[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>效果图:
相关文章推荐
- Android 利用Sharp样式设置文本框EditText圆角形状
- Android UI设计系列之自定义TextView属性实现带下划线的文本框(4)
- Android学习3之EditText可编辑的文本框组件
- Android入门逆引手册 - 08 可编辑文本框(EditText)的使用
- Android利用Sharp样式设置文本框EditText圆角形状
- Android利用TextView制作跑马灯该效果
- Android 利用Sharp样式设置文本框EditText圆角形状
- Android自动编辑文本框(AutoCompleteTextView)使用方法详解
- Android学习之自定义TextWatcher来监听文本最大输入字数
- Android 各控件的使用 - 可编辑文本框(EditText)
- Android定制控件:带图标的TextView和可编辑文本框(附项目源码)
- Android之监听文本框输入的文字个数并实时显示还可以输入的个数-->TextWatcher
- 自定义Android组件之带图像的TextView(转)
- Android用代码实现EditText不可编辑
- 自定义android控件EditText 自定义边框 背景
- Android TextView标点符号换行问题(顺便学习Android自定义View)
- android 自定义ListView 实现 弹出自定义对话框(带EditText)实现 配置文件实现
- 利用.Text的公告功能来实现一些自定义功能
- android-利用Animation制作的Menu
- Android自定义TextView中的超链接点击事件处理