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


当前回答

另一件整洁的事情是使用android:minHeight和android:layout_height="wrap_content"。通过这种方式,您可以在编辑为空时设置编辑的大小,当用户输入内容时,它会根据用户输入的数量进行扩展,包括多行。

其他回答

另一件整洁的事情是使用android:minHeight和android:layout_height="wrap_content"。通过这种方式,您可以在编辑为空时设置编辑的大小,当用户输入内容时,它会根据用户输入的数量进行扩展,包括多行。

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

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

android:scrollHorizontally=“false”

如果它在xml中不起作用,那么以编程方式实现它就可以了。

listView.setHorizontallyScrolling(假);

android:inputType="textMultiLine"

这行代码对我有用。在xml文件中添加您编辑的代码。

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

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类使点击监听器到这个编辑文本如下,我将添加我的,改变名称根据你的。

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

        });

这对我来说很好