




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android中怎么自定義一個(gè)密碼輸入框
這篇文章給大家介紹Android中怎么自定義一個(gè)密碼輸入框,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。思路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.重寫onDrawint
passwordWidth
=
getWidth()
-
(mPasswordNumber
-
1)
*
mDivisionLineSize;
mPasswordItemWidth
=
passwordWidth
/
mPasswordNumber;
//
繪制背景外圓
drawOutRing(canvas);
//
繪制背景內(nèi)圓
drawInRing(canvas);
//
繪制密碼
drawHidePassword(canvas);5.xml引用<com***.PasswordView
android:id="@+id/password"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="@null">
</com***.PasswordView>6.還可以設(shè)置些屬性在sytle中設(shè)置,通過xml中的app:xxx引用。<com.*.PasswordView
android:id="@+id/password"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
xmlns:app="/apk/res-auto"
app:bgColor="#ffffff"
android:background="@null">
</com.*.PasswordView>完整代碼一些樣式,我設(shè)置了,結(jié)果直接沒用上<declare-styleable
name="PasswordView">
<!--
密碼的個(gè)數(shù)
-->
<attr
name="passwordNumber"
format="integer"/>
<!--
密碼圓點(diǎn)的半徑
-->
<attr
name="passwordRadius"
format="dimension"/>
<!--
密碼圓點(diǎn)的顏色
-->
<attr
name="passwordColor"
format="color"/>
<!--
外圈顏色
-->
<attr
name="outRingColor"
format="color"/>
<!--
外圓線條大小
-->
<attr
name="outRingLineSize"
format="color"/>
<!--
背景邊框的顏色
-->
<attr
name="bgColor"
format="color"/>
<!--
背景邊框的大小
-->
<attr
name="bgSize"
format="dimension"/>
<!--
背景邊框的圓角大小
-->
<attr
name="bgCorner"
format="dimension"/>
</declare-styleable>自定義Edittextimport
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);
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030年中國(guó)食品產(chǎn)業(yè)園行業(yè)現(xiàn)狀調(diào)研及發(fā)展?jié)摿Ψ治鰣?bào)告
- 2025-2030年中國(guó)隨車起重機(jī)市場(chǎng)前景趨勢(shì)及發(fā)展?jié)摿Ψ治鰣?bào)告
- 2025-2030年中國(guó)防火門行業(yè)發(fā)展策略規(guī)劃分析報(bào)告
- 2025-2030年中國(guó)鉆機(jī)電控系統(tǒng)市場(chǎng)十三五規(guī)劃與發(fā)展策略研究報(bào)告
- 2025-2030年中國(guó)訓(xùn)練健身器材行業(yè)需求現(xiàn)狀及發(fā)展趨勢(shì)分析報(bào)告
- 2025-2030年中國(guó)組合電器產(chǎn)業(yè)發(fā)展趨勢(shì)及前景調(diào)研分析報(bào)告
- 武漢體育學(xué)院《機(jī)制設(shè)計(jì)理論及應(yīng)用》2023-2024學(xué)年第二學(xué)期期末試卷
- 鄂爾多斯職業(yè)學(xué)院《功能表面設(shè)計(jì)》2023-2024學(xué)年第二學(xué)期期末試卷
- 大同師范高等專科學(xué)?!堆芯啃詫W(xué)習(xí)指導(dǎo)與管理》2023-2024學(xué)年第二學(xué)期期末試卷
- 合肥職業(yè)技術(shù)學(xué)院《社會(huì)企業(yè)》2023-2024學(xué)年第二學(xué)期期末試卷
- 新版統(tǒng)編版一年級(jí)道德與法治下冊(cè)全冊(cè)教案(完整版)教學(xué)設(shè)計(jì)含教學(xué)反思
- 城市開放空間-課件
- 2025年春季學(xué)期學(xué)校德育工作計(jì)劃安排表(完整版)
- 湖南2024年湖南省水利廳所屬事業(yè)單位招聘57人筆試歷年參考題庫附帶答案詳解
- 2025年全球及中國(guó)調(diào)頻儲(chǔ)能行業(yè)頭部企業(yè)市場(chǎng)占有率及排名調(diào)研報(bào)告
- 2024年加氫站技術(shù)規(guī)范
- 《幼兒教育政策與法規(guī)》教案-單元4 幼兒園的保育和教育
- 小學(xué)思政培訓(xùn)
- 《森林火災(zāi)預(yù)防與撲救技術(shù)課件教程》
- 2025年度會(huì)計(jì)人員繼續(xù)教育會(huì)計(jì)法律法規(guī)答題活動(dòng)測(cè)試100題答案
- 第15課 人機(jī)對(duì)話的實(shí)現(xiàn) 說課稿 六上信息科技浙教版(2023)001
評(píng)論
0/150
提交評(píng)論