我正在尝试在我的应用加载时在edittext中添加10行。我为我的edittext定义了一个固定的高度。
无论我做什么,默认只显示一行,按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行
答案 0 :(得分:2)
我使用了这段代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/edit_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1\n2\n3\n4\n5\n\6\n\7\n8\n9\n10"
android:minLines="10" >
</EditText>
</LinearLayout>
答案 1 :(得分:1)
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);
}
}
答案 2 :(得分:0)
见下图,光标位于红色箭头
package com.example.customedittext;
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);
}
}
<ScrollView
android:layout_width="fill_parent"
android:layout_height="500dp"
android:background="@android:color/holo_blue_light">
<com.example.customedittext.LineEditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:scrollbars="vertical"
android:background="@android:color/transparent"
android:ems="10"
android:gravity="top"
android:minLines="7" >
<requestFocus />
</com.example.customedittext.LineEditText>
</ScrollView>