Android 中的 EditText 视图中允许多行吗?

发布于 2024-10-03 20:53:47 字数 51 浏览 0 评论 0原文

如何在 Android 的 EditText 视图中允许多行?

How to allow multi-line in Android's EditText view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

听闻余生 2024-10-10 20:53:47

默认情况下,Android 中的所有 EditText 小部件都是多行的。

这是一些示例代码:

<EditText
    android:inputType="textMultiLine" <!-- Multiline input -->
    android:lines="8" <!-- Total Lines prior display -->
    android:minLines="6" <!-- Minimum lines -->
    android:gravity="top|start" <!-- Cursor Position -->
    android:maxLines="10" <!-- Maximum Lines -->
    android:layout_height="wrap_content" <!-- Height determined by content -->
    android:layout_width="match_parent" <!-- Fill entire width -->
    android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
/>

By default all the EditText widgets in Android are multi-lined.

Here is some sample code:

<EditText
    android:inputType="textMultiLine" <!-- Multiline input -->
    android:lines="8" <!-- Total Lines prior display -->
    android:minLines="6" <!-- Minimum lines -->
    android:gravity="top|start" <!-- Cursor Position -->
    android:maxLines="10" <!-- Maximum Lines -->
    android:layout_height="wrap_content" <!-- Height determined by content -->
    android:layout_width="match_parent" <!-- Fill entire width -->
    android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
/>
烟酉 2024-10-10 20:53:47

您可能会发现它更好用:

<EditText 
...
android:inputType="textMultiLine"
/>

这是因为 android:singleLine 已被弃用。

You may find it better to use:

<EditText 
...
android:inputType="textMultiLine"
/>

This is because android:singleLine is deprecated.

入画浅相思 2024-10-10 20:53:47

这对我有用,实际上这两个属性很重要:inputTypelines。此外,您可能需要一个滚动条,下面的代码展示了如何制作一个:

 <EditText
        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" />

This works for me, actually these 2 attributes are important: inputType and lines. Besides, you may need a scrollbar, the code below shows how to make one:

 <EditText
        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" />
鸩远一方 2024-10-10 20:53:47

这就是我应用下面的代码片段的方式,并且运行良好。希望这会对某人有所帮助。

<EditText 
    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" 
/>

干杯! ...谢谢。

This is how I applied the code snippet below and it's working fine. Hope, this would help somebody.

<EditText 
    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" 
/>

Cheers! ...Thanks.

无需解释 2024-10-10 20:53:47

EditText 具有 singleLine 属性。您可以在 XML 中设置或通过调用 setSingleLine(false);
http://developer.android.com/reference/android /widget/TextView.html#setSingleLine%28%29

EditText has singleLine property. You can set in the XML or by calling setSingleLine(false);
http://developer.android.com/reference/android/widget/TextView.html#setSingleLine%28%29

初吻给了烟 2024-10-10 20:53:47

试试这个,
将这些行添加到您的编辑文本视图中,我将添加我的。确保你理解它

android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"

<EditText
    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类上点击listner来编辑文本,如下所示,我将根据你的添加我的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;
            }

        });

这对我来说很好用

Try this,
add these lines to your edit text view, i'll add mine. make sure you understand it

android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"

<EditText
    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"/>

and on your java class make on click listner to this edit text as follows, i'll add mine, chane names according to yours.

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;
            }

        });

this works fine for me

遗心遗梦遗幸福 2024-10-10 20:53:47

如果有人使用 AppCompatEditText 那么你需要设置 inputType="textMultiLine" 这里是你可以复制


   <androidx.appcompat.widget.AppCompatEditText
        android:padding="@dimen/_8sdp"
        android:id="@+id/etNote"
        android:minLines="6"
        android:singleLine="false"
        android:lines="8"
        android:scrollbars="vertical"
        android:background="@drawable/rounded_border"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_25sdp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="@dimen/_25sdp"
        android:autofillHints=""

        android:gravity="start|top"
        android:inputType="textMultiLine"
         />

背景的代码

   <?xml version="1.0" encoding="utf-8"?>
   <shape android:shape="rectangle" 
    xmlns:android="http://schemas.android.com/apk/res/android">
   <corners android:radius="@dimen/_5sdp"/>
   <stroke android:width="1dp" android:color="#C8C8C8"/>
   </shape>

if some one is using AppCompatEditText then you need to set inputType="textMultiLine" here is the code you can copy


   <androidx.appcompat.widget.AppCompatEditText
        android:padding="@dimen/_8sdp"
        android:id="@+id/etNote"
        android:minLines="6"
        android:singleLine="false"
        android:lines="8"
        android:scrollbars="vertical"
        android:background="@drawable/rounded_border"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_25sdp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="@dimen/_25sdp"
        android:autofillHints=""

        android:gravity="start|top"
        android:inputType="textMultiLine"
         />

for background

   <?xml version="1.0" encoding="utf-8"?>
   <shape android:shape="rectangle" 
    xmlns:android="http://schemas.android.com/apk/res/android">
   <corners android:radius="@dimen/_5sdp"/>
   <stroke android:width="1dp" android:color="#C8C8C8"/>
   </shape>
一影成城 2024-10-10 20:53:47
android:inputType="textMultiLine"

该代码行对我有用。将这段代码添加到您的 xml 文件中的 edittext 中。

android:inputType="textMultiLine"

This code line work for me. Add this code you edittext in your xml file.

喜你已久 2024-10-10 20:53:47

只需添加这个
android:inputType="textMultiLine"
在相关字段的 XML 文件中。

Just add this
android:inputType="textMultiLine"
in XML file on relevant field.

明月松间行 2024-10-10 20:53:47

所有这些都很好,但如果您将编辑文本放在上层滚动视图中,则不起作用:) 也许最常见的示例是“设置”视图,该视图包含太多项目,以至于它们超出了可见区域。在这种情况下,您将它们全部放入滚动视图以使设置可滚动。如果您在设置中需要多行可滚动编辑文本,则其滚动将不起作用。

All of these are nice but will not work in case you have your edittext inside upper level scroll view :) Perhaps most common example is "Settings" view that has so many items that the they go beyond of visible area. In this case you put them all into scroll view to make settings scrollable. In case that you need multiline scrollable edit text in your settings, its scroll will not work.

装迷糊 2024-10-10 20:53:47

要禁用先前在主题中分配的行数,请使用 xml 属性:android:lines="@null"

To disable number of lines that was previously assigned in theme use xml attribute: android:lines="@null"

云淡风轻 2024-10-10 20:53:47
 <EditText
           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
           /> 
 <EditText
           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
           /> 
千柳 2024-10-10 20:53:47

我从 http://www.pcsalt.com/android/edittext-with-single-line-line-wrapping-and-done-action-in-android/,尽管我自己不喜欢这个网站。如果您想要多行但希望保留输入按钮作为发布按钮,请将列表视图的“水平滚动”设置为 false。

android:scrollHorizo​​ntally="false"

如果它在 xml 中不起作用,那么以编程方式执行它会很奇怪。

listView.setHorizo​​ntallyScrolling(false);

I learned this from http://www.pcsalt.com/android/edittext-with-single-line-line-wrapping-and-done-action-in-android/, though I don't like the website myself. If you want multiline BUT want to retain the enter button as a post button, set the listview's "horizontally scrolling" to false.

android:scrollHorizontally="false"

If it doesn't work in xml, doing it programmatically weirdly works.

listView.setHorizontallyScrolling(false);

卷耳 2024-10-10 20:53:47

另一个巧妙的做法是将 android:minHeightandroid:layout_height="wrap_content" 一起使用。这样,您可以在编辑为空时设置编辑的大小,并且当用户输入内容时,它会根据用户输入的数量(包括多行)进行拉伸。

Another neat thing to do is to use android:minHeight along with android:layout_height="wrap_content". This way you can set the size of the edit when it's empty, and when the user inputs stuff, it stretches according to how much the user inputs, including multiple lines.

海螺姑娘 2024-10-10 20:53:47
<EditText
                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"," ");
<EditText
                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" />

and in Activity to remove "\n" is user press nextline

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

 MyString= editStr.replaceAll("\n"," ");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文