EditText左侧设置⽂字,右侧设置图⽚
⾃定义EditText实现左侧可以设置⽂字,右侧设置的图⽚可处理两个功能;1:清除当前输⼊框内容;2.隐藏或显⽰密码。可选择设置editText下⽅的下划线,默认有下划线
先看⼀下实现的效果
左侧⽂字的颜⾊、⼤⼩、左侧⽂字跟光标的间距都可以更改
1.在values包下创建l⽂件
<declare-styleable name="AutoEditTextView">
<!--//左侧设置的⽂字,如果设置了就显⽰,不设置就不显⽰-->
<attr name="leftText" format="string" />
<!--左侧⽂字的颜⾊-->
<attr name="leftTextColor" format="color" />
<!--左侧⽂字的⼤⼩-->
<attr name="leftTextSize" format="float" />
<!--左侧⽂字距离光标的距离-->
<attr name="leftTextPadding" format="integer" />
<!--右侧图标点击需要处理的类型 0 删除当前内容 1隐藏和显⽰密码-->
<attr name="rightButtonClickType" format="integer" />
<!--是否要显⽰底部的线-->
<attr name="isShowDownLine" format="boolean" />
<!--设置底部线默认的颜⾊-->
<attr name="downLineDefaultColor" format="color"/>
<!--设置EditText获取焦点之后底部线的颜⾊-->
<attr name="downLineFocusColor" format="color"/>
</declare-styleable>
2.编写AutoEditTextView继承android.support.v7.widget.AppCompatEditText
package cn.wangxiao.home.education.view;
t.Context;
t.res.TypedArray;
aphics.Canvas;
aphics.Paint;
aphics.Rect;
aphics.drawable.Drawable;
import android.t.ContextCompat;
TextUtils;
ethod.HideReturnsTransformationMethod;
ethod.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.MotionEvent;
import cn.wangxiao.home.education.R;
/**
* Created by ytt on 2018/7/19.
* 定义⼀个左侧可以填写⽂字,右侧可以放置图⽚,点击右侧图⽚根据type实现功能
* 还有下划线,下划线颜⾊根据获取焦点和未获取焦点有两种状态
* 右侧图⽚功能
* 1.删除当前输⼊⽂字,只有当前edittext有内容时才会显⽰该图⽚
* 2.密码的显⽰和隐藏
*/
public class AutoEditTextView extends android.support.v7.widget.AppCompatEditText {
public class AutoEditTextView extends android.support.v7.widget.AppCompatEditText {
//TODO 左侧的⽂字内容,没有则不设置
private String leftText;
//TODO 左侧⽂字的颜⾊
private int leftTextColor;
//TODO 左侧⽂字的⼤⼩
private float leftTextSize;
//TODO 左侧⽂字距离光标的距离
private int leftTextPadding;
//TODO 右侧图标点击的功能,0删除当前内容,1隐藏和显⽰密码
private int clickType;
//TODO 是否要显⽰底部的线
private boolean isShowDownLine;
//TODO 底部线显⽰的默认颜⾊
private int downLineDefaultColor;
//TODO 底部线现实的选中颜⾊
private int downLineFocusColor;
/
/绘制左侧⽂字的画笔
private Paint mTextPaint;
//绘制下⽅线的笔
private Paint mLinePaint;
private boolean isShowPass;
public AutoEditTextView(Context context) {
this(context, null);
}
public AutoEditTextView(Context context, AttributeSet attrs) {
this(context, attrs,R.attr.editTextStyle);
}
public AutoEditTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initData(context, attrs, defStyleAttr);
}
private void initData(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.AutoEditTextView, defStyleAttr, 0);
leftText = String(R.styleable.AutoEditTextView_leftText);
leftTextColor = Color(R.styleable.AutoEditTextView_leftTextColor, 3));
leftTextSize = Float(R.styleable.AutoEditTextView_leftTextSize, sp2px(16f));
clickType = Int(R.styleable.AutoEditTextView_rightButtonClickType, 0);
isShowDownLine = Boolean(R.styleable.AutoEditTextView_isShowDownLine, true);
leftTextPadding = Int(R.styleable.AutoEditTextView_leftTextPadding, dip2px(8));
downLineDefaultColor = Color(R.styleable.AutoEditTextView_downLineDefaultColor, ayLine)); downLineFocusColor = Color(R.styleable.AutoEditTextView_downLineFocusColor, lor.selectLine)); le();
setBackground(null);
initPaint();
}
private void initPaint() {
mTextPaint = new Paint();
mTextPaint.setColor(leftTextColor);
mTextPaint.setTextSize(leftTextSize);
mTextPaint.setAntiAlias(true);
mLinePaint = new Paint();
mLinePaint.setColor(downLineDefaultColor);
mLinePaint.setAntiAlias(true);
mLinePaint.setStrokeWidth(dip2px(1));
//TODO 如果需要设置左侧的问题,需要重新计算padding
if (!TextUtils.isEmpty(leftText)) {
int paddingLeft = (int) asureText(leftText) + getPaddingLeft() + leftTextPadding;
int paddingLeft = (int) asureText(leftText) + getPaddingLeft() + leftTextPadding;
//设置距离光标距离左侧的距离
setPadding(paddingLeft, getPaddingTop(), getPaddingRight(), getPaddingBottom());
}
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if (isShowDownLine) {
mLinePaint.setColor(focused ? downLineFocusColor : downLineDefaultColor);
invalidate();
}
}
/
**
* getScrollX()这个⽅法是当editText只显⽰⼀⾏时,如果起始点设置成0会被挤出屏幕,
* 看了⼀下TextView和View的源码才分析出来的
* */
@Override
protected void onDraw(Canvas canvas) {
//TODO 绘制左侧的⽂字,并居中显⽰
if (!TextUtils.isEmpty(leftText)) {
canvas.drawText(leftText, getScrollX(), (getMeasuredHeight() - TextSize()) / 2 + TextSize(), mTextPaint); }
//绘制底部的线
if (isShowDownLine) {
canvas.drawLine(getScrollX(), Height() - dip2px(1), getScrollX() + Width(), Height() - dip2px(1), mLinePaint);
}
}
/* @说明:isInnerWidth, isInnerHeight为ture,触摸点在删除图标之内,则视为点击了删除图标
* X() 获取相对应⾃⾝左上⾓的X坐标
* Y() 获取相对应⾃⾝左上⾓的Y坐标
* getWidth() 获取控件的宽度
* getHeight() 获取控件的⾼度
* getTotalPaddingRight() 获取删除图标左边缘到控件右边缘的距离
* getPaddingRight() 获取删除图标右边缘到控件右边缘的距离
* isInnerWidth:
* getWidth() - getTotalPaddingRight() 计算删除图标左边缘到控件左边缘的距离
* getWidth() - getPaddingRight() 计算删除图标右边缘到控件左边缘的距离
* isInnerHeight:
* distance 删除图标顶部边缘到控件顶部边缘的距离
textstyle* distance + height 删除图标底部边缘到控件顶部边缘的距离
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (Action() == MotionEvent.ACTION_UP) {
if (getCompoundDrawables()[2] != null) {
int x = (int) X();
int y = (int) Y();
Rect rect = getCompoundDrawables()[2].getBounds();
int height = rect.height();
int distance = (getHeight() - height) / 2;
boolean isInnerWidth = x > (getWidth() - getTotalPaddingRight()) && x < (getWidth() - getPaddingRight());
boolean isInnerHeight = y > distance && y < (distance + height);
if (isInnerWidth && isInnerHeight) {
dealRightButtonClickType();
}
}
}
TouchEvent(event);
}
//TODO 处理右侧图标点击事件
private void dealRightButtonClickType() {
if (clickType == 0) {//删除当前内容
setText("");
} else if (clickType == 1) {//隐藏和显⽰密码
isShowPass = !isShowPass;
setTransformationMethod(isShowPass ? Instance() : Instance()); setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1],
isShowPass ? getDrawable(R.mipmap.input_hidden_text) : getDrawable(R.mipmap.input_show_text), getCompoundDrawables()[3]); }
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
if (!TextUtils.isEmpty(text)) {
String().length() - 1);
}
}
public Drawable getDrawable(int id) {
Drawable drawable = getContext().getResources().getDrawable(id);
drawable.setBounds(0, 0, MinimumWidth(), MinimumHeight());
return drawable;
}
//dip--->px 1dp = 1px 1dp = 2px
public int dip2px(double dip) {
//dp和px的转换关系⽐例值
float density = getContext().getResources().getDisplayMetrics().density;
return (int) (dip * density + 0.5);
}
public int sp2px(float spValue) {
final float fontScale = getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
public int getColor(int id) {
Color(getContext(), id);
}
}
隐藏和显⽰密码的图标需要⾃⼰更改成⾃⼰的图⽚名称哦~不需要该功能也可以去掉
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论