2015-09-26 68 views
1

我正在做記事本應用程序,我想讓edittext看起來像一個頁面。我不知道該怎麼做。我剛剛創建了一個edittext視圖。如何使edittext看起來像一個頁面在android

<EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/et_note" 
    android:layout_below="@+id/tv_note" 
    android:hint="Write a note..."/> 

回答

2

您可以自定義EDITTEXT像這些 並在你的XML代碼中使用這些

// XML代碼

<com.example.lineedittextdemo.LineEditText 
android:id="@+id/edit_notepad" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@null" 
android:inputType="textMultiLine|textNoSuggestions" 
android:minLines="10" 
android:singleLine="false" 
android:imeOptions="actionNone" 
/> 

// Java代碼

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

// we need this constructor for LayoutInflater 
public LinedEditText(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    mRect = new Rect(); 
    mPaint = new Paint(); 
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
    mPaint.setColor(Color.parseColor("#C0C0C0")); //SET YOUR OWN COLOR HERE 
} 

@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); 
} 
} 
+0

這項工作很好。但有一個問題。當minLines設置爲10時,它只顯示5行和5行超過提示文本,並在那裏顯示空白空間 –

相關問題