1. 程式人生 > 其它 >android edittext設定多行,在Android的EditText檢視中允許多行?

android edittext設定多行,在Android的EditText檢視中允許多行?

在Android的EditText檢視中允許多行?

如何在Android的EditText檢視中允許多行?

12個解決方案

981 votes

預設情況下,Android中的所有EditText小部件都是多行的。

以下是一些示例程式碼:

 

android:inputType="textMultiLine"

android:lines="8"

android:minLines="6"

android:gravity="top|left"

android:maxLines="10"

android:layout_height="wrap_content"

android:layout_width="match_parent"

android:scrollbars="vertical"

/>

Shardul answered 2019-03-31T12:45:49Z

199 votes

你可能會發現它更好用:

 

...

android:inputType="textMultiLine"

/>

這是因為不推薦使用android:singleLine。

Knossos answered 2019-03-31T12:46:22Z

37 votes

這對我有用,實際上這兩個屬性很重要:inputType和lines。 此外,您可能需要一個滾動條,下面的程式碼顯示瞭如何製作一個:

 

android:id="@+id/addr_edittext"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:gravity="top|left"

android:inputType="textEmailAddress|textMultiLine"

android:lines="20"

android:minLines="5"

android:scrollHorizontally="false"

android:scrollbars="vertical" />

acoustic answered 2019-03-31T12:46:50Z

11 votes

這是我使用的方式,它的工作也很好。 希望,這會對某人有所幫助。

 

android:id="@+id/EditText02"

android:gravity="top|left"

android:inputType="textMultiLine"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:lines="5"

android:scrollHorizontally="false"

/>

乾杯! ...謝謝。

Nandagopal T answered 2019-03-31T12:47:24Z

9 votes

試試這個,將這些行新增到您的編輯文字檢視中,我將新增我的。 確保你理解它

android:overScrollMode="always"

android:scrollbarStyle="insideInset"

android:scrollbars="vertical"

 

android:inputType="textMultiLine"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/editText_newprom_description"

android:padding="10dp"

android:lines="5"

android:overScrollMode="always"

android:scrollbarStyle="insideInset"

android:minLines="5"

android:gravity="top|left"

android:scrollbars="vertical"

android:layout_marginBottom="20dp"/>

並在你的java類上使用onclicklistener編輯這個編輯文字如下,我將根據你的新增我的,chane名稱。

EditText description;

description = (EditText)findViewById(R.id.editText_newprom_description);

description.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View view, MotionEvent motionEvent) {

view.getParent().requestDisallowInterceptTouchEvent(true);

switch (motionEvent.getAction() & MotionEvent.ACTION_MASK){

case MotionEvent.ACTION_UP:

view.getParent().requestDisallowInterceptTouchEvent(false);

break;

}

return false;

}

});

這對我來說很好

Chathura Jayanath answered 2019-03-31T12:48:00Z

8 votes

EditText具有singleLine屬性。 您可以在XML中設定或通過呼叫setSingleLine(false);[http://developer.android.com/reference/android/widget/TextView.html#setSingleLine%28%29]

milan answered 2019-03-31T12:48:26Z

5 votes

所有這些都很好,但是如果你在上層滾動檢視中有你的edittext,它將不起作用:)也許最常見的例子是“設定”檢視,它有很多專案,超出了可見區域。 在這種情況下,您將它們全部放入滾動檢視以使設定可滾動。 如果您在設定中需要多行可滾動編輯文字,則其滾動將不起作用。

Cynichniy Bandera answered 2019-03-31T12:48:52Z

4 votes

要禁用先前在主題使用xml屬性中分配的行數:android:lines="@null"

Oleg Bozhko answered 2019-03-31T12:49:27Z

3 votes

 

android:id="@id/editText" //id of editText

android:gravity="start" // Where to start Typing

android:inputType="textMultiLine" // multiline

android:imeOptions="actionDone" // Keyboard done button

android:minLines="5" // Min Line of editText

android:hint="@string/Enter Data" // Hint in editText

android:layout_width="match_parent" //width editText

android:layout_height="wrap_content" //height editText

/>

Yogesh Sarvaiya answered 2019-03-31T12:49:47Z

1 votes

我從[http://www.pcsalt.com/android/edittext-with-single-line-line-wrapping-and-done-action-in-android/]瞭解到這一點,雖然我自己不喜歡這個網站。 如果您想要多行但是想要將輸入按鈕保留為釋出按鈕,請將listview的“水平滾動”設定為false。

listView.setHorizontallyScrolling(false);

如果它在xml中不起作用,那麼以程式設計方式執行它會很奇怪。

listView.setHorizontallyScrolling(false);

Fire3galaxy answered 2019-03-31T12:50:40Z

0 votes

另一個巧妙的做法是使用android:minHeight和android:layout_height="wrap_content".這樣你就可以設定編輯時的大小,當用戶輸入內容時,它會根據使用者輸入的內容(包括多行)進行擴充套件。

instanceof answered 2019-03-31T12:51:15Z

0 votes

 

android:hint="Address"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:inputType="textMultiLine|textNoSuggestions"

android:id="@+id/edittxtAddress"

android:layout_marginLeft="@dimen/textsize2"

android:textColor="@android:color/white"

android:scrollbars="vertical"

android:minLines="2"

android:textStyle="normal" />

並在活動中刪除“\ n”是使用者按下一行

String editStr= edittxtAddress.getText().toString().trim();

MyString= editStr.replaceAll("\n"," ");