【问题标题】:Scrollable vertically and horizontally EditText that doesn't automatically break text to the next line可垂直和水平滚动的 EditText,不会自动将文本分到下一行
【发布时间】:2015-11-28 12:17:36
【问题描述】:

我需要一个看起来像简单文本编辑器的 Activity:
全屏 EditText,下方带有提交按钮。

重要:
1. EditText 必须垂直和水平滚动
2. 仅当用户单击下一行按钮时,输入文本才会转到下一行
3. EditText 填充屏幕顶部和按钮之间的整个空间

我的代码:

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

为什么不好:
EditText 在到达行尾时自动将文本分到下一行

在我将 fillViewport 设置为 true 之前它运行良好。但否则 EditText 不会填满整个屏幕

提前致谢

【问题讨论】:

    标签: android android-layout android-edittext


    【解决方案1】:

    EditText 实际上已经内置了滚动功能,因此您不必使用ScrollView。唯一的问题是它不支持甩动。

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

    要停止自动换行,您可以激活水平滚动:

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

    【讨论】:

    • 这正是我想要的。非常感谢
    【解决方案2】:

    我认为您最初可以设置您的 EditText 属性 android:maxLines="1"。

    当点击“新行”按钮时,您可以使用 setMaxLines(int) 方法以编程方式增加该值。

    EditText.setMaxLines(int) 
    

    这是文档 -> link

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 1970-01-01
      • 2014-01-09
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 2015-05-31
      相关资源
      最近更新 更多