[Original] example of the Android custom View password box

Source: Internet
Author: User

Compliance rules

Expose all attributes or behaviors that affect the visible appearance in your view.

  • Add and set styles using XML
  • Controls the appearance and behavior of elements and supports event listeners that communicate with important events.

For detailed steps, refer to: Android custom View step

Appearance

Supported styles

You can use XML to define attributes that affect external and behavior as follows:

Border rounded corner value, border color, split line color, Border width, password length, password size, password color

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

It also supports the original EditText function to obtain data values, numeric keyboard settings, and so on.

Main Code for drawing Logic
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 + defacontcontmargin, rect. top + defacontcontmargin, rect. right-defacontcontmargin, rect. bottom-defaultContMargin); borderPaint. setColor (Color. WHITE); canvas. drawRoundRect (rectIn, borderRadius, borderRadius, borderPaint); // split line 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);} // password 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 );}}

 

Download complete code

Http://files.cnblogs.com/purediy/PasswordInputView.rar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.