Android 自定義View 密碼框?qū)嵗a
暴露您view中所有影響可見(jiàn)外觀的屬性或者行為。
•通過(guò)XML添加和設(shè)置樣式
•通過(guò)元素的屬性來(lái)控制其外觀和行為,支持和重要事件交流的事件監(jiān)聽(tīng)器
詳細(xì)步驟見(jiàn):Android 自定義View步驟
效果圖展示:

支持的樣式
可以通過(guò)XML定義影響外邊和行為的屬性如下
邊框圓角值,邊框顏色,分割線顏色,邊框?qū)挾?,密碼長(zhǎng)度,密碼大小,密碼顏色
<declare-styleable name="PasswordInputView"> <attr name="borderWidth" format="dimension"/> <attr name="borderColor" format="color"/> <attr name="borderRadius" format="dimension"/> <attr name="passwordLength" format="integer"/> <attr name="passwordWidth" format="dimension"/> <attr name="passwordColor" format="color"/> <attr name="passwordRadius" format="dimension"/> </declare-styleable>
同時(shí)支持原來(lái)EditText功能,可以獲得數(shù)據(jù)值,數(shù)字鍵盤(pán)設(shè)置等
繪制邏輯的主要代碼
protected void onDraw(Canvas canvas) {
int width = getWidth();
int height = getHeight();
// 外邊框
RectF rect = new RectF(0, 0, width, height);
borderPaint.setColor(borderColor);
canvas.drawRoundRect(rect, borderRadius, borderRadius, borderPaint);
// 內(nèi)容區(qū)
RectF rectIn = new RectF(rect.left + defaultContMargin, rect.top + defaultContMargin,
rect.right - defaultContMargin, rect.bottom - defaultContMargin);
borderPaint.setColor(Color.WHITE);
canvas.drawRoundRect(rectIn, borderRadius, borderRadius, borderPaint);
// 分割線
borderPaint.setColor(borderColor);
borderPaint.setStrokeWidth(defaultSplitLineWidth);
for (int i = 1; i < passwordLength; i++) {
float x = width * i / passwordLength;
canvas.drawLine(x, 0, x, height, borderPaint);
}
// 密碼
float cx, cy = height/ 2;
float half = width / passwordLength / 2;
for(int i = 0; i < textLength; i++) {
cx = width * i / passwordLength + half;
canvas.drawCircle(cx, cy, passwordWidth, passwordPaint);
}
}
相關(guān)文章
Android 截取手機(jī)屏幕兩種實(shí)現(xiàn)方法
這篇文章主要介紹了Android 截取手機(jī)屏幕兩種實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-05-05
一文詳解無(wú)痕埋點(diǎn)在Android中的實(shí)現(xiàn)
很多時(shí)候因?yàn)楫a(chǎn)品都會(huì)要獲取用戶(hù)的行為,需要客戶(hù)端進(jìn)行相關(guān)的埋點(diǎn),下面這篇文章主要給大家介紹了關(guān)于無(wú)痕埋點(diǎn)在Android中實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
Android編程實(shí)現(xiàn)TextView部分顏色變動(dòng)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)TextView部分顏色變動(dòng)的方法,涉及Android針對(duì)TextView樣式操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android中EditText實(shí)現(xiàn)不可編輯解決辦法
這篇文章主要介紹了Android中EditText實(shí)現(xiàn)不可編輯解決辦法,需要的朋友可以參考下2014-12-12
android中soap協(xié)議使用(ksoap調(diào)用webservice)
kSOAP是如何調(diào)用ebservice的呢,首先要使用SoapObject,這是一個(gè)高度抽象化的類(lèi),完成SOAP調(diào)用。可以調(diào)用它的addProperty方法填寫(xiě)要調(diào)用的webservice方法的參數(shù)2014-02-02
Android ViewPager畫(huà)廊效果詳解及實(shí)例
這篇文章主要介紹了Android ViewPager畫(huà)廊效果詳解及實(shí)例的相關(guān)資料,這里提供實(shí)例代碼及實(shí)現(xiàn)效果圖,具有參考價(jià)值,需要的朋友可以參考下2016-12-12
android APP登陸頁(yè)面適配的實(shí)現(xiàn)
這篇文章主要介紹了android APP登陸頁(yè)面適配的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
android studio logcat 無(wú)篩選 顯示全部日志 無(wú)應(yīng)用包名區(qū)分方式
這篇文章主要介紹了android studio logcat 無(wú)篩選 顯示全部日志 無(wú)應(yīng)用包名區(qū)分方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04

