简体   繁体   English

如何使EditText看起来像Android中的页面

[英]How to make edittext look like a page in android

I'm making notepad app and i want to make edittext look like a page. 我正在做记事本应用程序,我想使edittext看起来像一个页面。 I have no idea how to do that. 我不知道该怎么做。 I have just created a edittext view. 我刚刚创建了一个edittext视图。

   <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/et_note"
    android:layout_below="@+id/tv_note"
    android:hint="Write a note..."/>

You can make a custom Edittext like these and use these in your xml code 您可以像这样创建自定义Edittext并在xml代码中使用它们

//XML CODE // XML代码

<com.example.lineedittextdemo.LineEditText
android:id="@+id/edit_notepad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:inputType="textMultiLine|textNoSuggestions"
android:minLines="10"
android:singleLine="false"
android:imeOptions="actionNone"
 />

//JAVA CODE // JAVA代码

 public class LinedEditText extends EditText {
private Rect mRect;
private Paint mPaint;

// we need this constructor for LayoutInflater
public LinedEditText(Context context, AttributeSet attrs) {
    super(context, attrs);

    mRect = new Rect();
    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaint.setColor(Color.parseColor("#C0C0C0")); //SET YOUR OWN COLOR HERE
}

@Override
protected void onDraw(Canvas canvas) {
    //int count = getLineCount();

    int height = getHeight();
    int line_height = getLineHeight();

    int count = height / line_height;

    if (getLineCount() > count)
        count = getLineCount();//for long text with scrolling

    Rect r = mRect;
    Paint paint = mPaint;
    int baseline = getLineBounds(0, r);//first line

    for (int i = 0; i < count; i++) {

        canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        baseline += getLineHeight();//next line
    }

    super.onDraw(canvas);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM