ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android-默认情况下如何在EditTExt中显示最少10行?

2019-11-22 13:26:04  阅读:196  来源: 互联网

标签:android-edittext android


我尝试在我的应用加载时默认在Edittext中添加10行.我为编辑文本定义了一个固定的高度.

无论我做什么,默认情况下它仅显示一行,并且当我按Enter键时会添加更多行.下面是我的代码.属性minHeight和minLines不起作用

<com.example.EditTextt
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="600dp"
                android:minLines="10"
                android:background="@android:color/transparent"
                android:fadingEdge="vertical"
                android:gravity="top"
                android:padding="10dp"
                android:scrollbars="vertical"
                android:textSize="22sp" >

                <requestFocus />
            </com.example.EditTextt>

参见下图.当我一次又一次按Enter键时,出现这些行.我希望这些行默认显示为记事本.

这是我的EditText的自定义类:
EditTextt.Java

package com.example;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

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

public LineEditText(Context context, AttributeSet attrs) {
    super(context, attrs);

    mRect = new Rect();
    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setColor(getResources().getColor(R.color.Exzeoblue)); //SET YOUR OWN COLOR HERE
}

@Override
protected void onDraw(Canvas canvas) {

    // Gets the number of lines of text in the View.
    int count = getLineCount();

    // Gets the global Rectangle and Paint objects
    Rect r = mRect;
    Paint paint = mPaint;

    // Draws one line in the rectangle for every line of text in the EditText
    for (int i = 0; i < count; i++) {

        // Gets the baseline coordinates for the current line of text
        int baseline = getLineBounds(i, r);

        /*
         * Draws a line in the background from the left of the rectangle to the
         * right, at a vertical position one dip below the baseline, using the
         * "paint" object for details.
         */
        canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        baseline += getLineHeight();//next line
    }

    // Finishes up by calling the parent method
    super.onDraw(canvas);
}
}

现在此代码一次只绘制一行.默认情况下,我无法绘制10条线

解决方法:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

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

    int initialCount = 0;

    public LineEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.BLUE);
        initialCount = getMinLines();
        setLines(initialCount);

    }

    @Override
    protected void onDraw(Canvas canvas) {

        // Gets the number of lines of text in the View.
        int count = getBaseline();
        // Gets the global Rectangle and Paint objects
        Rect r = mRect;
        Paint paint = mPaint;
        // Gets the baseline coordinates for the current line of text
        int baseline = getLineBounds(0, r);
        // Draws one line in the rectangle for every line of text in the EditText
        for (int i = 0; i < count; i++) {
            /*
             * Draws a line in the background from the left of the rectangle to the
             * right, at a vertical position one dip below the baseline, using the
             * "paint" object for details.
             */
            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            baseline += getLineHeight();//next line
        }

        // Finishes up by calling the parent method
        super.onDraw(canvas);
    }
}

标签:android-edittext,android
来源: https://codeday.me/bug/20191122/2060057.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有