我想创建一个记事本。我指的是 android sdk 提供的示例记事本。我的问题是我未能在 editText 上绘制多行。我已成功运行该程序,但 editText 上没有显示任何内容,更糟糕的是我无法在其上输入单词。这是我的代码。谢谢你的帮忙。
就观点而言,我对我的课程路径进行了双重确认。所以,我认为我的课程路径没有任何问题。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/layouteditbottom"
android:layout_alignParentBottom="true"
android:paddingBottom="18dp"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:layout_above="@+id/layouteditbottom">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="@string/title"
android:id="@+id/title_text1" />
<EditText android:id="@+id/title"
android:layout_toRightOf="@+id/title_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionNext"
android:textSize="18sp"/>
<View xmlns:android="http://schemas.android.com/apk/res/android"
class="com.example.apps2.MainActivity$LineEditText"
android:id="@+id/body"
android:layout_below="@+id/title"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:padding="5dp"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:gravity="top"
android:textSize="22sp"
android:capitalize="sentences"/>
</RelativeLayout>
</RelativeLayout>
下面的代码是java。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class LineEditText extends EditText{
// we need this constructor for LayoutInflater
public LineEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setColor(Color.BLUE);
}
private Rect mRect;
private Paint mPaint;
@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);
}
}
}