简体   繁体   中英

Multiline Edittext with Horizontal Lines

I am making a Assignment don't know how to add a multilines edittext follow this one Tutorial but don't know how to implement it in MainActivity class

Drawing multiple lines in edittext eg notepad

i am using following class

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(getResources().getColor()); //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);
    }

}

and i have following xml view wanted to inflat them via this class

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

</LinearLayout>

multilinedittext.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/multiline_exdittext_layout"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/edittxt_multilines"
        />

</LinearLayout>

try this

 <EditText
    android:inputType="textMultiLine" 
    android:lines="8"
    android:minLines="6" 
    android:gravity="top|left" 
    android:maxLines="10" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:scrollbars="vertical"
/>

Change your xml to this

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/multiline_exdittext_layout"
    android:layout_width="match_parent"  
    android:layout_height="match_parent">

<your.packagename.LinedEditText
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inputType="textMultiLine"
    android:ems="10"
    android:id="@+id/edittxt_multilines"
    />

</LinearLayout>

Whats your mistake

Your custom class is public class LinedEditText extends EditText

  1. Did you call this in your XML

Just Change your EditText Xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/multiline_exdittext_layout"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <YOUR_PACKAGE_NAME.LinedEditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/edittxt_multilines"
        />

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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