├── ClearAndSeeEdittext.gif ├── res └── drawable-xhdpi │ ├── password_invisible.png │ ├── password_visible.png │ └── icon_edittext_delete.png ├── README.md ├── .gitattributes ├── .gitignore └── src └── cn └── schope └── lightning └── view └── CanSeeEditText.java /ClearAndSeeEdittext.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/g707175425/ClearAndSeeEditText/HEAD/ClearAndSeeEdittext.gif -------------------------------------------------------------------------------- /res/drawable-xhdpi/password_invisible.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/g707175425/ClearAndSeeEditText/HEAD/res/drawable-xhdpi/password_invisible.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/password_visible.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/g707175425/ClearAndSeeEditText/HEAD/res/drawable-xhdpi/password_visible.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/icon_edittext_delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/g707175425/ClearAndSeeEditText/HEAD/res/drawable-xhdpi/icon_edittext_delete.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ClearAndSeeEditText 2 | #### (带清除和查看密码按钮的EditText) 3 | 4 | ### 预览效果: 5 | ![](https://github.com/g707175425/ClearAndSeeEditText/blob/master/ClearAndSeeEdittext.gif) 6 | ### 代码中实现: 7 | 在Xml中引入即可 8 | 9 | 10 | 11 | by QQ:707175425 12 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /src/cn/schope/lightning/view/CanSeeEditText.java: -------------------------------------------------------------------------------- 1 | package cn.schope.lightning.view; 2 | 3 | 4 | import android.content.Context; 5 | import android.graphics.Bitmap; 6 | import android.graphics.Canvas; 7 | import android.graphics.Paint; 8 | import android.graphics.drawable.BitmapDrawable; 9 | import android.text.Editable; 10 | import android.text.InputType; 11 | import android.text.TextUtils; 12 | import android.text.TextWatcher; 13 | import android.util.AttributeSet; 14 | import android.view.MotionEvent; 15 | import android.widget.EditText; 16 | import cn.schope.lightning.R; 17 | import cn.schope.lightning.utils.UIUtils; 18 | 19 | public class CanSeeEditText extends EditText{ 20 | 21 | private Context context; 22 | private Bitmap clearBut; 23 | private Bitmap passwordEye; 24 | private Paint bitmapPaint; 25 | private Bitmap passwordEyeInvisible; 26 | private Bitmap passwordEyeVisible; 27 | private int rightOffset; 28 | private int rightPadding; 29 | private Bitmap clearButShow; 30 | private int initPaddingRight; 31 | 32 | public CanSeeEditText(Context context, AttributeSet attrs, int defStyle) { 33 | super(context, attrs, defStyle); 34 | this.context = context; 35 | init(); 36 | } 37 | 38 | public CanSeeEditText(Context context, AttributeSet attrs) { 39 | super(context, attrs); 40 | this.context = context; 41 | init(); 42 | } 43 | 44 | public CanSeeEditText(Context context) { 45 | super(context); 46 | this.context = context; 47 | init(); 48 | } 49 | 50 | private void init() { 51 | rightOffset = UIUtils.dip2px(5); 52 | clearButShow = ((BitmapDrawable)getResources().getDrawable(R.drawable.icon_edittext_delete)).getBitmap(); 53 | passwordEyeInvisible = ((BitmapDrawable)getResources().getDrawable(R.drawable.password_invisible)).getBitmap(); 54 | passwordEyeVisible = ((BitmapDrawable)getResources().getDrawable(R.drawable.password_visible)).getBitmap(); 55 | passwordEye = passwordEyeInvisible; 56 | bitmapPaint = new Paint(); 57 | bitmapPaint.setAntiAlias(true); 58 | 59 | initPaddingRight = getPaddingRight(); 60 | if(TextUtils.isEmpty(getText().toString())){ 61 | clearBut = null; 62 | rightPadding = initPaddingRight+passwordEye.getWidth()+rightOffset; 63 | }else{ 64 | clearBut = clearButShow; 65 | rightPadding = initPaddingRight+clearBut.getWidth()+passwordEye.getWidth()+rightOffset+rightOffset; 66 | } 67 | setPadding(getPaddingLeft(), getPaddingTop(), rightPadding, getPaddingBottom()); 68 | // setOnTouchListener(this); 69 | initTextChange(); 70 | } 71 | 72 | private void initTextChange() { 73 | addTextChangedListener(new TextWatcher() { 74 | @Override 75 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { 76 | } 77 | @Override 78 | public void afterTextChanged(Editable s) { 79 | } 80 | @Override 81 | public void onTextChanged(CharSequence s, int start, int before, int count) { 82 | if (s.length() > 0) { 83 | clearBut = clearButShow; 84 | rightPadding = initPaddingRight+clearBut.getWidth()+passwordEye.getWidth()+rightOffset+rightOffset; 85 | invalidate(); 86 | } else { 87 | clearBut = null; 88 | rightPadding = initPaddingRight+passwordEye.getWidth()+rightOffset; 89 | invalidate(); 90 | } 91 | setPadding(getPaddingLeft(), getPaddingTop(), rightPadding, getPaddingBottom()); 92 | } 93 | }); 94 | } 95 | 96 | @Override 97 | public boolean onTouchEvent(MotionEvent event) { 98 | switch (event.getAction()) { 99 | case MotionEvent.ACTION_UP: 100 | float distanceToDrawLeft = event.getX() - (getMeasuredWidth()-rightPadding); 101 | if(distanceToDrawLeft >= 0){ 102 | if(distanceToDrawLeft >= (rightPadding - (passwordEye.getWidth()+rightOffset))){ 103 | //眼睛区域被点击了 104 | System.out.println("眼睛区域被点击了"); 105 | if(getInputType() == (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)){ 106 | setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); 107 | setSelection(getText().length()); 108 | passwordEye = passwordEyeVisible; 109 | invalidate(); 110 | }else{ 111 | setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 112 | setSelection(getText().length()); 113 | passwordEye = passwordEyeInvisible; 114 | invalidate(); 115 | } 116 | }else{ 117 | //清除区域被点击了 118 | setText(""); 119 | clearBut = null; 120 | rightPadding = initPaddingRight+passwordEye.getWidth()+rightOffset; 121 | setPadding(getPaddingLeft(), getPaddingTop(), rightPadding, getPaddingBottom()); 122 | invalidate(); 123 | } 124 | } 125 | break; 126 | } 127 | return super.onTouchEvent(event); 128 | } 129 | 130 | @Override 131 | protected void onDraw(Canvas canvas) { 132 | super.onDraw(canvas); 133 | canvas.save(); 134 | canvas.translate(getScrollX(),0); 135 | int eyeLeft = getMeasuredWidth()-passwordEye.getWidth()-rightOffset; 136 | canvas.drawBitmap(passwordEye,eyeLeft,(getMeasuredHeight()-passwordEye.getHeight())/2,bitmapPaint); 137 | if(clearBut != null){ 138 | canvas.drawBitmap(clearBut,eyeLeft-clearBut.getWidth()-rightOffset,(getMeasuredHeight()-clearBut.getHeight())/2,bitmapPaint); 139 | } 140 | canvas.restore(); 141 | } 142 | 143 | } 144 | --------------------------------------------------------------------------------