[Android] implements EditText with the display password button (no memory leakage) and androidedittext
Principle:
Use the custom View to display the password button. When you click the password button, call setInputType to change the attribute.
Solution: directly add the code.
Package com. finals. view; import com. example. test. r; import android. content. context; import android. content. res. typedArray; import android. graphics. bitmap; import android. graphics. bitmap. config; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paint. style; import android. graphics. rectF; import android. graphics. drawable. drawable; import android. Util. attributeSet; import android. view. motionEvent; import android. view. inputmethod. editorInfo; import android. widget. editText; public class PassEditText extends EditText {int vertex; int mThumbHeight; int offsetX; int offsetY; boolean isShowPass = false; Drawable vertex; Bitmap mThumbDefault; public PassEditText (Context context, AttributeSet attrs) {super (context, attrs); InitDrawable (context, Attrs);} public PassEditText (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); InitDrawable (context, attrs );} private void InitDrawable (Context context, AttributeSet attrs) {float density = context. getResources (). getDisplayMetrics (). density; TypedArray mTypedArray = context. obtainStyledAttributes (attrs, R. styleable. windows); mThumbDrawable = mTypedArray. getDrawable (R. Styleable. windows_thumb); mThumbWidth = (int) mTypedArray. getDimension (R. styleable. windows_thumb_width, 20 * density); mThumbHeight = (int) mTypedArray. getDimension (R. styleable. windows_thumb_height, 20 * density); if (mThumbDrawable = null) {mThumbDefault = getThumbDefault ();} else {mThumbDrawable. setBounds (0, 0, (int) mThumbWidth, (int) mThumbHeight);} mTypedArray. recycle (); if (getInputType ()! = (EditorInfo. TYPE_TEXT_VARIATION_PASSWORD | EditorInfo. TYPE_CLASS_TEXT) {isShowPass = true ;}@ Overridepublic int getCompoundPaddingRight () {return super. getCompoundPaddingRight () + mThumbWidth;} @ Overridepublic void draw (Canvas canvas) {super. draw (canvas); drawThumb (canvas);} void drawThumb (Canvas canvas) {offsetX = getWidth ()-mThumbWidth-getPaddingRight (); offsetY = (getHeight ()-mThumbHeight) /2; if (mThumbDrawable! = Null) {canvas. save (); canvas. translate (getScrollX () + offsetX, getScrollY () + offsetY); mThumbDrawable. draw (canvas); canvas. restore ();} else if (mThumbDefault! = Null) {canvas. drawBitmap (mThumbDefault, getScrollX () + offsetX, getScrollY () + offsetY, null) ;}@ Overridepublic boolean onTouchEvent (MotionEvent event) {int action = event. getAction (); switch (action) {case MotionEvent. ACTION_DOWN: checkThumb (event); break; default: break;} return super. onTouchEvent (event);} void checkThumb (MotionEvent event) {float x = event. getX (); float y = event. getY (); if (x> offset X & x <offsetX + mThumbWidth & y> offsetY & y <offsetY + mThumbHeight) {if (! IsShowPass) {isShowPass = true; this. setInputType (EditorInfo. TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);} else {isShowPass = false; this. setInputType (EditorInfo. TYPE_TEXT_VARIATION_PASSWORD | EditorInfo. TYPE_CLASS_TEXT) ;}}@ Overrideprotected void onAttachedToWindow () {if (mThumbDrawable = null) {mThumbDefault = getThumbDefault ();} super. onAttachedToWindow () ;}@ Overrideprotected void onDetachedFromWindow () {If (mThumbDefault! = Null) {mThumbDefault. recycle (); mThumbDefault = null;} super. onDetachedFromWindow ();}/*** create default image ** @ return */private Bitmap getThumbDefault () {float density = getContext (). getResources (). getDisplayMetrics (). density; int comment enwidth = (int) (3 * density); Paint mPaint = new Paint (); mPaint. setColor (Color. BLACK); mPaint. setStyle (Style. FILL); mPaint. setAntiAlias (true); Bitmap mBitmap = Bitmap. createBitmap (mThumbWidth, mThumbHeight, Config. ARGB_4444); Canvas mCanvas = new Canvas (mBitmap); mCanvas. drawCircle (mThumbWidth/2, mThumbHeight/2, mThumbWidth * 0.30F-stokenWidth, mPaint); mPaint. setStyle (Style. STROKE); mPaint. setStrokeWidth (specify enwidth); RectF oval = new RectF (specify enwidth, specify enwidth, mThumbWidth-specify enwidth, mThumbHeight-specify enwidth); mCanvas. drawArc (oval,-175,170, false, mPaint); return mBitmap;} public boolean isShowPassword () {return isShowPass ;}}