package com.jiutouxiang.activity.login;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import com.jiutouxiang.R;
/**
* Created by Administrator on 2016/9/29.
*/
public class PasswordEditText extends EditText implements View.OnFocusChangeListener,TextWatcher {
//EditText右側(cè)眼睛圖標
private Drawable mLookDrawable;
private Drawable mClearDrawable;
private boolean hasFoucs;
//判斷是否可見
private boolean visible=false;
public PasswordEditText(Context context){
super(context,null);
}
public PasswordEditText(Context context, AttributeSet attris){
this(context,attris, android.support.v7.appcompat.R.attr.editTextStyle);
init();
}
public PasswordEditText(Context context, AttributeSet attrist, int defStyle){
super(context,attrist,defStyle);
init();
}
private void init(){
// 獲取EditText的DrawableRight,假如沒有設(shè)置我們就使用默認的圖片,獲取圖片的順序是左上右下(0,1,2,3,)
mLookDrawable=getCompoundDrawables()[2];
if(mLookDrawable==null){
mLookDrawable=getResources().getDrawable(R.mipmap.login_password_normal);
mClearDrawable=getResources().getDrawable(R.mipmap.login_edittext_clear);
}
mLookDrawable.setBounds(0,0,mLookDrawable.getIntrinsicWidth(),mLookDrawable.getIntrinsicHeight());
//默認設(shè)置圖標
setClearIconVisible(true);
//設(shè)置焦點改變的監(jiān)聽
setOnFocusChangeListener(this);
//設(shè)置輸入框內(nèi)容發(fā)生改變的監(jiān)聽
addTextChangedListener(this);
}
/* @說明:isInnerWidth, isInnerHeight為ture,觸摸點在刪除圖標之內(nèi),則視為點擊了刪除圖標
* event.getX() 獲取相對應(yīng)自身左上角的X坐標
* event.getY() 獲取相對應(yīng)自身左上角的Y坐標
* getWidth() 獲取控件的寬度
* getHeight() 獲取控件的高度
* getTotalPaddingRight() 獲取刪除圖標左邊緣到控件右邊緣的距離
* getPaddingRight() 獲取刪除圖標右邊緣到控件右邊緣的距離
* isInnerWidth:
* getWidth() - getTotalPaddingRight() 計算刪除圖標左邊緣到控件左邊緣的距離
* getWidth() - getPaddingRight() 計算刪除圖標右邊緣到控件左邊緣的距離
* isInnerHeight:
* distance 刪除圖標頂部邊緣到控件頂部邊緣的距離
* distance + height 刪除圖標底部邊緣到控件頂部邊緣的距離
*/
@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) {
//如果點擊
//this.setText("");
if(visible){
//需要隱藏
this.setTransformationMethod(PasswordTransformationMethod.getInstance());
visible=false;
// 獲取EditText的DrawableRight,假如沒有設(shè)置我們就使用默認的圖片,獲取圖片的順序是左上右下(0,1,2,3,)
mLookDrawable=getResources().getDrawable(R.mipmap.login_password_normal);
mLookDrawable.setBounds(0,0,mLookDrawable.getIntrinsicWidth(),mLookDrawable.getIntrinsicHeight());
//默認設(shè)置圖標
setClearIconVisible(true);
Log.e("需要隱藏","需要隱藏");
}else{
//需要看見
this.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
visible=true;
// 獲取EditText的DrawableRight,假如沒有設(shè)置我們就使用默認的圖片,獲取圖片的順序是左上右下(0,1,2,3,)
mLookDrawable=getResources().getDrawable(R.mipmap.login_password_selected);
//getIntrinsicWidth()和getIntrinsicHeight()返回的寬高應(yīng)該是dp為單位的哦。
mLookDrawable.setBounds(0,0,mLookDrawable.getIntrinsicWidth(),mLookDrawable.getIntrinsicHeight());
//默認設(shè)置圖標
setClearIconVisible(true);
Log.e("需要看見","需要看見");
}
}
}
}
return super.onTouchEvent(event);
}
/**
* 當ClearEditText焦點發(fā)生變化的時候,
* 輸入長度為零,隱藏刪除圖標,否則,顯示刪除圖標
*/
@Override
public void onFocusChange(View v, boolean hasFocus) {
//獲取焦點的情況
/*this.hasFoucs= hasFocus;
if (hasFocus) {
setClearIconVisible(getText().length() > 0);
} else {
setClearIconVisible(false);
}*/
}
@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
//內(nèi)部數(shù)據(jù)發(fā)生改變時
/*if (hasFoucs) {
setClearIconVisible(s.length() > 0);
}*/
}
private void setClearIconVisible(boolean visibke){
Drawable right=visibke?mLookDrawable:null;
setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1],right,getCompoundDrawables()[3]);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
}
xml文件直接引用就好了