Custom view (use Edittetx for Notepad effects)

Source: Internet
Author: User



First Look at:






Idea: Create a class, inherit from EditText, draw our underscore in the OnDraw method, the height of the screen and the height of each line of control to draw how many lines in the screen to underline, then to implement some custom properties, set the control basic margin and the content margin is OK, The process is very simple, the code of the comments written in a very detailed, the need for students to read



Custom Class Mynoteedittext.class

package com.wjt.day43_01_mynoteedittext;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.EditText;

public class MyNoteEditText extends EditText {
   private int lineColor = Color.RED;
   private int lineStrokWidth = 1;
   private int padding = 10;
public MyNoteEditText (Context context, AttributeSet attrs) {
super (context, attrs);
// This sentence is to make the input data of each line are located in his upper left part
setGravity (Gravity.TOP);
The
TypedArray array = context.obtainStyledAttributes (attrs, R.styleable.MyNoteEditText);
lineColor = array.getColor (R.styleable.MyNoteEditText_lineColor, lineColor);
lineStrokWidth = (int) array.getDimension (R.styleable.MyNoteEditText_lineStrokWidth, lineStrokWidth);
padding = (int) array.getDimension (R.styleable.MyNoteEditText_padding, padding);
The
array.recycle ();
The
The
// One-to-one correspondence between content and underline
setPadding (padding, 0, padding, 0);
The
}

@Override
protected void onDraw (Canvas canvas) {
super.onDraw (canvas);
// 1, create a brush
Paint paint = new Paint ();
paint.setAntiAlias (true);
paint.setColor (lineColor);
paint.setStrokeWidth (lineStrokWidth);
The
// 2, Get the current width and height of the entire control
int viewHeight = getHeight ();
int viewWidth = getWidth ();
The
// 3, Get the width of each line in EditText
int lineHeight = getLineHeight ();
The
// 4, by calculating how many lines can be placed on the screen
int pageLineCounts = viewHeight / lineHeight;
The
// 5, use canvas to draw
for (int i = 0; i <pageLineCounts; i ++) {
canvas.drawLine (padding, (i + 1) * lineHeight, viewHeight-padding, (i + 1) * lineHeight, paint);
}
The
// 6, to achieve the dash when the text is written to the next page
int textLineCount = getLineCount ();
The
if (textLineCount> pageLineCounts) {
for (int i = pageLineCounts; i <textLineCount; i ++) {
canvas.drawLine (padding, (i + 1) * lineHeight, viewHeight-padding, (i + 1) * lineHeight, paint);
The
}
}
The
The
}
}
Custom attribute part attrs.xml

<? xml version = "1.0" encoding = "utf-8"?>
<resources>
    <declare-styleable name = "NoteEditText">
        <attr name = "lineColor" format = "color | reference" />
        <attr name = "lineStrokeWidth" format = "dimension | reference" />
        <attr name = "padding" format = "dimension | reference" />
    </ declare-styleable>
</ resources>


 

Custom view (using EditTetx to realize special effects of notepad)

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.