[Android]實現帶顯示密碼按鈕的EditText(無記憶體泄露),androidedittext

來源:互聯網
上載者:User

[Android]實現帶顯示密碼按鈕的EditText(無記憶體泄露),androidedittext
原理:

通過自訂View繪製顯示密碼按鈕,當點擊密碼按鈕的時候調用setInputType來更改屬性。

解決方案:就直接上代碼了 
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 mThumbWidth;int mThumbHeight;int offsetX;int offsetY;boolean isShowPass = false;Drawable mThumbDrawable;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 > offsetX && 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();}/** * 建立預設圖片 *  * @return */private Bitmap getThumbDefault() {float density = getContext().getResources().getDisplayMetrics().density;int stokenWidth = (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(stokenWidth);RectF oval = new RectF(stokenWidth, stokenWidth, mThumbWidth - stokenWidth, mThumbHeight - stokenWidth);mCanvas.drawArc(oval, -175, 170, false, mPaint);return mBitmap;}public boolean isShowPassword() {return isShowPass;}}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.