2013-01-01 66 views
1

我想創建一個記事本。我指的是android sdk提供的示例記事本。我的問題是我未能在editText上繪製多行。我成功運行了該程序,但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); 
      } 

    } 

} 
+0

你想做什麼?想用多線劃劃文本,或者你想在edittext框中多行? –

+0

我想強調文字。我想創建一個充滿線條的背景的記事本。 – edisonthk

+0

如果你已經解決了你的問題,請將答案標記爲已接受。不要在你的問題的標題中放置類似「[已解決]」的假標籤。 – meagar

回答

0

謝謝大家的回答。我通過從最外層的relativeLayout替換內層relativeLayout來解決它。以下代碼是答案。

<?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:id="@+id/toplayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="5dp" 
     android:paddingTop="5dp"   
     android:layout_alignParentTop="true">  
     <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"/> 
     <TextView 
      android:id="@+id/notelist_date" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true"    
      android:paddingRight="10sp"    
      android:textSize="18sp" />  
    </RelativeLayout> 

    <view 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     class="com.example.note1.NoteEdit$LineEditText" 
     android:id="@+id/body" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/layouteditbottom" 
     android:layout_below="@+id/toplayout"  
     android:background="@android:color/transparent" 
     android:capitalize="sentences" 
     android:fadingEdge="vertical" 
     android:gravity="top" 
     android:padding="5dp" 
     android:scrollbars="vertical" 
     android:textSize="22sp" /> 
</RelativeLayout> 
0

使用android:lines="2"固定多線固定單行使用android:singleLine="true"在你的XML你已經使用單行刪除,並如示例使用。

更改您的EditText如下圖所示

<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:lines="2"// fix the multiline limit 2,3,4,5,6,7..... 
     android:imeOptions="actionNext" 
     android:textSize="18sp"/> 

希望這可以幫助你.. !!

+0

謝謝你的回答,但同時我想道歉,我沒有把我的問題弄清楚。我想在正文文本上創建一行。 – edisonthk

+0

沒有prblm .. !!好你解決了它:) –

3

只需將一個背景圖像添加到您的editText並在其上放置線條即可。

編輯

創建自己與TILEMODE設置爲重複,像這樣的位圖繪製:

<?xml version="1.0" encoding="utf-8"?> 
<bitmap 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@[package:]drawable/drawable_resource" 
    android:tileMode="repeat" /> 

對於android:src指定用線和一些空間高度匹配一個線的圖像的文字(取決於你的字體大小)。

+1

我想創建一個背景,當我像一些簡單的記事本一樣向下滾動時,它會移動。 – edisonthk

+0

創建一個BitmapDrawable作爲背景,並在該drawable中設置android:tileMode =「repeat」。查看示例的更新答案。 – Ridcully