)[原] Android 自訂View 密碼框 例子,androidview

來源:互聯網
上載者:User

(轉)[原] Android 自訂View 密碼框 例子,androidview
遵從準則

暴露您view中所有影響可見外觀的屬性或者行為。

  • 通過XML添加和設定樣式
  • 通過元素的屬性來控制其外觀和行為,支援和重要事件交流的事件監聽器

詳細步驟見:Android 自訂View步驟

樣子

支援的樣式

可以通過XML定義影響外邊和行為的屬性如下

邊框圓角值,邊框顏色,分割線顏色,邊框寬度,密碼長度,密碼大小,密碼顏色

<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>

 

同時支援原來EditText功能,可以獲得資料值,數字鍵台設定等

繪製邏輯的主要代碼
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);     // 內容區    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);    }}   

 

完整代碼下載

https://github.com/tianshaojie/Android-PasswordInputView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.