2011-09-27 53 views
3

嗨兩行,我需要給EditText上限制爲兩行,我用下面的代碼如何限制的EditText在Android的

<EditText 
android:layout_toRightOf="@+id/valettext" 
android:layout_centerVertical="true" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_marginRight="70dp" 
android:maxLines="2" 
android:ellipsize="end" 
android:id="@+id/note"/> 

,但它不工作,它要到下一行上按下一個按鈕。

如何做到這一點。

在此先感謝。

+0

重複的 http://stackoverflow.com/questions/7092961/edittext-maxlines-not-working-user-can-still-input-more-lines-than-set – MarkB

+0

請檢查我代碼在這裏... http://stackoverflow.com/questions/8911638/set-maximum-number-of-text-lines-for-an-edittext/23310236#23310236 –

回答

2

你需要補充一點:

android:singleLine="false" 

而且,設定的行數顯示前:

android:lines="2" 
+0

其不工作 – surendra

+0

這顯示用戶兩行但是當第三個輸入時,EditText被擴展。查看我的解決方案:http://stackoverflow.com/questions/7092961/edittext-maxlines-not-working-user-can-still-input-more-lines-than-set/7103931 –

2

覆蓋您的EditText的addaddTextChangedListener()和內部afterTextChanged()方法試試這個代碼。

@Override 
public void afterTextChanged(Editable editable) { 
    if (null != edittext.getLayout() && edittext.getLayout().getLineCount() > 2) { 
     edittext.getText().delete(edittext.getText().length() - 1, edittext.getText().length()); 
    } 
}