android edittext设置多行,在Android的EditText视图中允许多行?

本文介绍了如何在Android EditText中启用多行输入,提供了解决方案,包括设置inputType、lines属性,以及如何配合滚动条使用。通过实例代码和技巧,帮助开发者快速实现多行文本输入的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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"," ");

Kapil Parmar answered 2019-03-31T12:51:50Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值