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