帶刪除按鈕的EditText 帶密碼顯示隱藏
阿新 • • 發佈:2019-01-31
package com.iim.ego.widget;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.iim.ego.R;
import java.lang.reflect.Field;
/**
* Created by cjf on 2018-04-10 11:36
*/
public class EditTextWithDel extends EditText implements View.OnFocusChangeListener {
private Drawable imgAble;
private Context mContext;
//點選區域大小
private int clickArea = 100;
//是否為密碼型別
private boolean isPassword = false;
//當前是否可檢視密碼
private boolean isEyeOne = false;
public EditTextWithDel(Context context) {
super(context);
mContext = context;
init();
// this(context,null);
}
public EditTextWithDel(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
// this(context,attrs,0);
}
public EditTextWithDel(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
init();
}
private void init() {
setHintTextColor(getResources().getColor(R.color.color_bg_cccccc));
setTextColor(getResources().getColor(R.color.color_bg_333333));
setGravity(Gravity.CENTER_VERTICAL);
setCursorColor();
imgAble = mContext.getResources().getDrawable(R.drawable.btn_equip_clear);
addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
setDrawable();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
setOnFocusChangeListener(this);
}
//設定刪除圖片
public void setDrawable() {
if (hasFocus()) {
if (length() < 1) {
setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
} else {
setCompoundDrawablesWithIntrinsicBounds(null, null, imgAble, null);
}
} else {
setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
}
}
// 處理右邊圖示點選事件
@Override
public boolean onTouchEvent(MotionEvent event) {
if (imgAble != null && event.getAction() == MotionEvent.ACTION_DOWN && hasFocus()) {
int eventX = (int) event.getRawX();
int eventY = (int) event.getRawY();
Rect rect = new Rect();
int[] location = new int[2];
getLocationOnScreen(location);
rect.right = location[0] + getWidth();
rect.left = rect.right - clickArea;
rect.top = location[1];
rect.bottom = rect.top + getHeight();
if (rect.contains(eventX, eventY)) {//點選圖示
if (isPassword) {//密碼型別
if (isEyeOne) {
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
isEyeOne = false;
} else {
setInputType(InputType.TYPE_CLASS_TEXT);
isEyeOne = true;
}
} else {//普通型別 刪除文字
setText("");
}
}
}
return super.onTouchEvent(event);
}
@Override
protected void finalize() throws Throwable {
super.finalize();
}
@Override
public void onFocusChange(View view, boolean b) {
if (b) {//有焦點
setDrawable();
((EditTextWithDel) view).setSelection(((EditTextWithDel) view).getText().length());
} else {//失去焦點
setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
}
}
public int getClickArea() {
return clickArea;
}
/**
* 設定點選區域大小
*
* @param clickArea
*/
public void setClickArea(int clickArea) {
this.clickArea = clickArea;
}
/**
* 設定游標顏色值
*/
public void setCursorColor() {
try {
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(this, R.drawable.shape_cursor_color);
} catch (Exception ignored) {
}
}
@Override
public void setInputType(int type) {
super.setInputType(type);
if (type == (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)) {
isPassword = true;
imgAble = mContext.getResources().getDrawable(R.drawable.btn_equip_setting_eye_off);
} else {
if (isPassword) {
imgAble = mContext.getResources().getDrawable(R.drawable.btn_equip_setting_eye_on);
}
}
setDrawable();
}
}
游標樣式shape_cursor_color.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/color_text_3582da"/>
<size android:width="1dp"/>
</shape>
btn_equip_clear
btn_equip_setting_eye_off
btn_equip_setting_eye_on