帶有右側(cè)點(diǎn)擊圖片顯示和隱藏密碼的功能.
<com.yuanin.aimifinance.view.PasswordEditText
android:id="@+id/etPwd"
style="@style/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/small_margin"
android:background="@mipmap/login_edittext_bg"
android:drawableLeft="@mipmap/login_password"
android:drawablePadding="@dimen/margin_eight"
android:gravity="center_vertical"
android:hint="@string/input_login_password"
android:paddingLeft="@dimen/margin_eight"
android:paddingRight="@dimen/margin_eight"
android:textColor="@color/text_black"
android:textColorHint="@color/text_light_gray"
android:textSize="@dimen/normal_text"/>
public class PasswordEditText extends EditText implements View.OnFocusChangeListener,
TextWatcher {
//EditText右側(cè)的眼睛按鈕
private Drawable mHideDrawable, mShowDrawable, mCurrentDrawable;
private boolean hasFoucs;
private boolean isShowPwd = false;
public PasswordEditText(Context context) {
this(context, null);
}
public PasswordEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle);
}
public PasswordEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
// 獲取EditText的DrawableRight,假如沒有設(shè)置我們就使用默認(rèn)的圖片,獲取圖片的順序是左上右下(0,1,2,3,)
mHideDrawable = getResources().getDrawable(
R.mipmap.login_hide_pwd);
mShowDrawable = getResources().getDrawable(
R.mipmap.login_show_pwd);
mHideDrawable.setBounds(0, 0, mHideDrawable.getIntrinsicWidth(),
mHideDrawable.getIntrinsicHeight()+10);
mCurrentDrawable = mHideDrawable;
// 默認(rèn)設(shè)置隱藏圖標(biāo)
setClearIconVisible(false);
// 設(shè)置焦點(diǎn)改變的監(jiān)聽
setOnFocusChangeListener(this);
// 設(shè)置輸入框里面內(nèi)容發(fā)生改變的監(jiān)聽
addTextChangedListener(this);
}
/* *@說明:isInnerWidth, isInnerHeight為ture盒粮,觸摸點(diǎn)在刪除圖標(biāo)之內(nèi)嚷闭,則視為點(diǎn)擊了刪除圖標(biāo)
* event.getX() 獲取相對(duì)應(yīng)自身左上角的X坐標(biāo)
* event.getY() 獲取相對(duì)應(yīng)自身左上角的Y坐標(biāo)
* getWidth() 獲取控件的寬度
* getHeight() 獲取控件的高度
* getTotalPaddingRight() 獲取刪除圖標(biāo)左邊緣到控件右邊緣的距離
* getPaddingRight() 獲取刪除圖標(biāo)右邊緣到控件右邊緣的距離
* isInnerWidth:
* getWidth() - getTotalPaddingRight() 計(jì)算刪除圖標(biāo)左邊緣到控件左邊緣的距離
* getWidth() - getPaddingRight() 計(jì)算刪除圖標(biāo)右邊緣到控件左邊緣的距離
* isInnerHeight:
* distance 刪除圖標(biāo)頂部邊緣到控件頂部邊緣的距離
* distance + height 刪除圖標(biāo)底部邊緣到控件頂部邊緣的距離
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (getCompoundDrawables()[2] != null) {
int x = (int) event.getX();
int y = (int) event.getY();
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) {
if (isShowPwd) {
//設(shè)置EditText文本為隱藏的
this.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
mHideDrawable.setBounds(0, 0, mHideDrawable.getIntrinsicWidth(),
mHideDrawable.getIntrinsicHeight()+10);
mCurrentDrawable = mHideDrawable;
setClearIconVisible(true);
} else {
//設(shè)置EditText文本為可見的
this.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
mShowDrawable.setBounds(0, 0, mShowDrawable.getIntrinsicWidth(),
mShowDrawable.getIntrinsicHeight()+10);
mCurrentDrawable = mShowDrawable;
setClearIconVisible(true);
}
isShowPwd = !isShowPwd;
this.setSelection(TextUtils.isEmpty(this.getText()) ? 0 : this.length());//光標(biāo)挪到最后
}
}
}
return super.onTouchEvent(event);
}
/**
* 當(dāng)ClearEditText焦點(diǎn)發(fā)生變化的時(shí)候,
* 輸入長度為零卤恳,隱藏刪除圖標(biāo)鳞滨,否則洞焙,顯示刪除圖標(biāo)
*/
@Override
public void onFocusChange(View v, boolean hasFocus) {
this.hasFoucs = hasFocus;
}
protected void setClearIconVisible(boolean visible) {
Drawable right = visible ? mCurrentDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0],
getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
}
@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
if (hasFoucs) {
setClearIconVisible(s.length() > 0);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
}
<style name="passwordEditText">
<item name="android:inputType">textPassword</item>
<item name="android:maxLength">16</item>
</style>