這篇文章給大家介紹Android中怎么自定義一個(gè)密碼輸入框,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
我們提供的服務(wù)有:網(wǎng)站制作、成都網(wǎng)站制作、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、賓陽(yáng)ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的賓陽(yáng)網(wǎng)站制作公司
思路
1.自定義EditText。
2.背景為一個(gè)外圓環(huán)加內(nèi)實(shí)心圓。
3.edittext的長(zhǎng)度變化時(shí)候重新繪制背景或者紅色環(huán)位置。
關(guān)鍵代碼
代碼其實(shí)也很簡(jiǎn)單,順手拿資源的請(qǐng)到文末。
1.畫背景
/** * 繪制背景外圓 */ private void drawOutRing(Canvas canvas) { mPaint.setColor(mBgColor); // 設(shè)置畫筆為空心 mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(mBgSize); RectF rectF = new RectF(mBgSize, mBgSize, getWidth() - mBgSize, getHeight() - mBgSize); // 畫圓 for (int i = 0; i < mPasswordNumber; i++) { int cx = i * mDivisionLineSize + i * mPasswordItemWidth + mPasswordItemWidth / 2 + mBgSize; canvas.drawCircle(cx, getHeight() / 2, mOutRadius, mPaint); } }
2.畫實(shí)心內(nèi)圓背景
/** * 繪制背景內(nèi)圓 */ private void drawInRing(Canvas canvas) { mPaint.setColor(mDivisionLineColor); // 設(shè)置畫筆為實(shí)心 mPaint.setStyle(Paint.Style.FILL); // 畫圈圈 for (int i = 0; i < mPasswordNumber; i++) { int cx = i * mDivisionLineSize + i * mPasswordItemWidth + mPasswordItemWidth / 2 + mBgSize; canvas.drawCircle(cx, getHeight() / 2, mPasswordRadius, mPaint); } } }
3.繪制輸入密碼的變化動(dòng)作
/** * 繪制隱藏的密碼 */ private void drawHidePassword(Canvas canvas) { int passwordLength = getText().length(); if (passwordLength > 6) passwordLength = 6; mPaint.setColor(mPasswordColor); // 畫實(shí)心內(nèi)圓 mPaint.setStyle(Paint.Style.FILL); for (int i = 0; i < passwordLength; i++) { int cx = i * mDivisionLineSize + i * mPasswordItemWidth + mPasswordItemWidth / 2 + mBgSize; canvas.drawCircle(cx, getHeight() / 2, mPasswordRadius, mPaint); } //外圓顏色 mPaint.setColor(mPasswordColor); // 設(shè)置畫筆為空心 mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(mBgSize); RectF rectF = new RectF(mBgSize, mBgSize, getWidth() - mBgSize, getHeight() - mBgSize); // 畫空心外圓 for (int i = 0; i < passwordLength; i++) { int cx = i * mDivisionLineSize + i * mPasswordItemWidth + mPasswordItemWidth / 2 + mBgSize; canvas.drawCircle(cx, getHeight() / 2, mOutRadius, mPaint); } }
4.重寫onDraw
int passwordWidth = getWidth() - (mPasswordNumber - 1) * mDivisionLineSize; mPasswordItemWidth = passwordWidth / mPasswordNumber; // 繪制背景外圓 drawOutRing(canvas); // 繪制背景內(nèi)圓 drawInRing(canvas); // 繪制密碼 drawHidePassword(canvas);
5.xml引用
6.還可以設(shè)置些屬性
在sytle中設(shè)置,通過xml中的app:xxx引用。
完整代碼
一些樣式,我設(shè)置了,結(jié)果直接沒用上
自定義Edittext
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.util.TypedValue; import android.view.inputmethod.EditorInfo; import android.widget.EditText; /** *自定義密碼輸入框 */ public class PasswordView extends EditText { // 畫筆 private Paint mPaint; // 一個(gè)密碼所占的寬度 private int mPasswordItemWidth; // 密碼的個(gè)數(shù)默認(rèn)為6位數(shù) private int mPasswordNumber = 6; // 背景圓顏色 private int mBgColor = Color.parseColor("#d1d2d6"); // 背景大小 private int mBgSize = 1; // 背景邊框圓角大小 private int mBgCorner = 0; // 外圓的顏色 private int outRingLineColor = mBgColor; // 外圓線條的大小 private int outRingLineSize = 1; // 密碼輸入的顏色 private int mPasswordColor = Color.parseColor("#cb3435"); // 密碼圓點(diǎn)的半徑大小 private int mPasswordRadius = 6; // 外圓半徑大小 private int mOutRadius = 25; public PasswordView(Context context) { this(context, null); } public PasswordView(Context context, AttributeSet attrs) { super(context, attrs); initPaint(); initAttributeSet(context, attrs); // 設(shè)置輸入模式是密碼 setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD); // 不顯示光標(biāo) setCursorVisible(false); } /** * 初始化屬性 */ private void initAttributeSet(Context context, AttributeSet attrs) { TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PasswordView); // 獲取大小 outRingLineSize = (int) array.getDimension(R.styleable.PasswordView_outRingLineSize, dip2px(outRingLineSize)); mPasswordRadius = (int) array.getDimension(R.styleable.PasswordView_passwordRadius, dip2px(mPasswordRadius)); mBgSize = (int) array.getDimension(R.styleable.PasswordView_bgSize, dip2px(mBgSize)); mBgCorner = (int) array.getDimension(R.styleable.PasswordView_bgCorner, 0); // 獲取顏色 mBgColor = array.getColor(R.styleable.PasswordView_bgColor, mBgColor); outRingLineColor = array.getColor(R.styleable.PasswordView_outRingColor, outRingLineColor); mPasswordColor = array.getColor(R.styleable.PasswordView_passwordColor, mPasswordColor); array.recycle(); } /** * 初始化畫筆 */ private void initPaint() { mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); } /** * dip 轉(zhuǎn) px */ private int dip2px(int dip) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, getResources().getDisplayMetrics()); } @Override protected void onDraw(Canvas canvas) { int passwordWidth = getWidth() - (mPasswordNumber - 1) * outRingLineSize; mPasswordItemWidth = passwordWidth / mPasswordNumber; // 繪制背景外圓 drawOutRing(canvas); // 繪制背景內(nèi)圓 drawInRing(canvas); // 繪制密碼 drawHidePassword(canvas); } @Override public void setText(CharSequence text, BufferType type) { super.setText(text, type); } /** * 繪制背景外圓 */ private void drawOutRing(Canvas canvas) { mPaint.setColor(mBgColor); // 設(shè)置畫筆為空心 mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(mBgSize); RectF rectF = new RectF(mBgSize, mBgSize, getWidth() - mBgSize, getHeight() - mBgSize); // 畫圓 for (int i = 0; i < mPasswordNumber; i++) { int cx = i * outRingLineSize + i * mPasswordItemWidth + mPasswordItemWidth / 2 + mBgSize; canvas.drawCircle(cx, getHeight() / 2, mOutRadius, mPaint); } } /** * 繪制隱藏的密碼 */ private void drawHidePassword(Canvas canvas) { int passwordLength = getText().length(); if (passwordLength > 6) passwordLength = 6; mPaint.setColor(mPasswordColor); // 設(shè)置畫筆為實(shí)心 mPaint.setStyle(Paint.Style.FILL); for (int i = 0; i < passwordLength; i++) { int cx = i * outRingLineSize + i * mPasswordItemWidth + mPasswordItemWidth / 2 + mBgSize; canvas.drawCircle(cx, getHeight() / 2, mPasswordRadius, mPaint); } //外圓 mPaint.setColor(mPasswordColor); // 設(shè)置畫筆為空心 mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(mBgSize); RectF rectF = new RectF(mBgSize, mBgSize, getWidth() - mBgSize, getHeight() - mBgSize); // 如果沒有設(shè)置圓角,就畫矩形 for (int i = 0; i < passwordLength; i++) { int cx = i * outRingLineSize + i * mPasswordItemWidth + mPasswordItemWidth / 2 + mBgSize; canvas.drawCircle(cx, getHeight() / 2, mOutRadius, mPaint); } } /** * 繪制背景內(nèi)圓 */ private void drawInRing(Canvas canvas) { mPaint.setColor(outRingLineColor); // 設(shè)置畫筆為實(shí)心 mPaint.setStyle(Paint.Style.FILL); // 畫圈圈 for (int i = 0; i < mPasswordNumber; i++) { int cx = i * outRingLineSize + i * mPasswordItemWidth + mPasswordItemWidth / 2 + mBgSize; canvas.drawCircle(cx, getHeight() / 2, mPasswordRadius, mPaint); } } }
關(guān)于Android中怎么自定義一個(gè)密碼輸入框就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。