效果圖如下:
兩個類,一個繼承Activity的類,一個繼承EditText的類,一個xml文件
繼承Activity的類:
import Android.app.Activity;
import android.os.Bundle;
public class EditTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edittest);
}
}
繼承EditText的類:
package com.mrzhu.edittest;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.EditText;
public class EditTextTest extends EditText {
public EditTextTest(Context context) {
super(context);
}
public EditTextTest(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public EditTextTest(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setTextSize(20);
paint.setColor(Color.GRAY);
canvas.drawText("輸入提示文本:", 10, getHeight() / 2 + 5, paint);
super.onDraw(canvas);
}
}
edittext.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.mrzhu.edittest.EditTextTest
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="100dp"
android:text="從此處輸入文本"
android:id="@+id/test"
/>
</LinearLayout>
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11