实现代码如下:
主方法,main.java
import java.text.SimpleDateFormat;
import java.util.Date;
import Android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
MyNote mn = new MyNote(this);
mn.setHeight(380);
layout.addView(mn);
LinearLayout layout1=new LinearLayout(this);
layout1.setOrientation(LinearLayout.HORIZONTAL);
Button button = new Button(this);
button.setText("确定");
button.setWidth(60);
layout1.addView(button);
TextView textview=new TextView(this);
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String dateString=sdf.format(date);
textview.setText(dateString);
textview.setWidth(200);
textview.setPadding(60, 0, 0, 0);
layout1.addView(textview);
Button button1=new Button(this);
button1.setText("取消");
button1.setWidth(60);
layout1.addView(button1);
layout.addView(layout1);
this.setContentView(layout);
}
}
主方法中主要实现了整个画布的布局的简单设置。以下是自定义EditText的具体实现方法。
MyNote.java
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Gravity;
import android.widget.EditText;
public class MyNote extends EditText{
public MyNote(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
protected void onDraw(Canvas canvas){
int lineHeight=this.getLineHeight();
Paint mPaint=getPaint();
mPaint.setColor(Color.GRAY);//文本编辑线
int topPadding=this.getPaddingTop();
int leftPadding=this.getPaddingLeft();
float textSize=getTextSize();
setGravity(Gravity.LEFT|Gravity.TOP);
int y =(int)(topPadding+textSize);
for(int i=0;i<getLineCount();i++){
canvas.drawLine(leftPadding, y+2, getRight()-leftPadding, y+2, mPaint);
y+=lineHeight;
}
canvas.translate(0, 0);
super.onDraw(canvas);
}
}