Scrollable vertically and horizontally EditText that doesn't automatically break text to the next line

烂漫一生 提交于 2020-01-20 08:59:07

问题


I need an activity that look like a simple text editor:
full-screen EditText with submit button below it.

Important:
1. EditText has to scroll both vertically and horizontally
2. Input text goes to the next line ONLY when the user clicks nextline button
3. EditText fills the whole space between the top of the screen and the button

My code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fillViewport="true">

            <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true">

                    <EditText
                        android:id="@+id/some_edittext"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="top|left"
                        />

            </HorizontalScrollView>

    </ScrollView>

    <Button
        android:id="@+id/submit_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit"/>

</LinearLayout>

Why bad:
EditText AUTOMATICALLY breaks text to the next line as it reaches the end of the row

It worked fine before I set fillViewport to true. But otherwise EditText doesn't fill entire screen

Thanks in advance


回答1:


EditText actually has scrolling already built in, so you don't have to use a ScrollView. The only thing is that it doesn't support flings.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/some_edittext"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="top|left"
        />

    <Button
        android:id="@+id/submit_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit"/>

</LinearLayout>

To stop the automatic wrapping, you can activate horizontal scrolling:

EditText editText = (EditText) findViewById(R.id.some_edittext);
editText.setHorizontallyScrolling(true);
editText.setMovementMethod(new ScrollingMovementMethod());



回答2:


I think you could set your EditText attribute android:maxLines="1" initially.

And when "new line" button is clicked, you can increase that value programmatically using setMaxLines(int) method.

EditText.setMaxLines(int) 

here's the doc -> link

Hope this helped.



来源:https://stackoverflow.com/questions/32362480/scrollable-vertically-and-horizontally-edittext-that-doesnt-automatically-break

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!