【问题标题】:Android EditText disable edit but allow scrollingAndroid EditText 禁用编辑但允许滚动
【发布时间】:2015-10-01 09:27:07
【问题描述】:

我知道可以禁用编辑EditText

答案是herehere

但是,当我禁用 EditText 时,它也会禁用此文本中的垂直滚动。

是否可以只禁用编辑,但允许滚动?

【问题讨论】:

标签: android android-edittext


【解决方案1】:
    editText.setFocusableInTouchMode(false);
    editText.clearFocus();

【讨论】:

  • 这是最好的解决方案。干净优雅,因为您可以来回切换而不会丢失事件。谢谢!!!
  • 这有一个问题是仍然可以将文本复制并粘贴到该字段中。要修复此集:editText.isLongClickable = false
  • 很好的解决方案。谢谢。
【解决方案2】:

我发现只是禁用关键监听器、可聚焦和光标似乎可以工作:

editText.setKeyListener( null );
editText.setFocusable( false );
editText.setCursorVisible(false);

只要启用编辑文本,您似乎仍然可以滚动文本。

【讨论】:

  • 如果我以这种方式禁用 EditText 小部件,如何重新启用它?我应该将什么传递给 setKeyListener?
【解决方案3】:

在您的 EdittText 中使用此属性:

android:enabled="true"
android:focusableInTouchMode="false"
android:scrollbars="vertical"

您的 EditText 将被启用,但无法进行编辑。然后,您将能够滚动(在这种情况下为垂直)并且无法进行编辑。

【讨论】:

    【解决方案4】:

    如果您将 EditText 包装在 ScrollView 中,那么您应该能够保留滚动行为。

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <EditText ... />
    
    </ScrollView>
    

    【讨论】:

      【解决方案5】:

      editText.setKeyListener(null) 应该可以帮到你。

      【讨论】:

      • 你的答案在我的手机里不起作用:),你检查了吗?
      • @BNK 是的,我用它来禁用EditText 中的编辑,它仍然可以滚动。
      • 你是如何在布局文件中声明editText的?我的是&lt;EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView" android:scrollbars="vertical" android:text="111111111111111112222222222222444444444444444222222222"/&gt;
      • @BNK 我没有滚动条="vertical"。
      • 我只是删除了滚动条,setOnKeyListener(null) 仍然不适用于我的手机(Android 4.1.2)
      【解决方案6】:

      尝试在你的editext中使用这两个属性,它会使你的editext只能滚动而不是可编辑 机器人:输入类型=“无” 安卓:textIsSelectable="真" 下面是例子

                     <EditText
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:gravity="start"
                      android:padding="20dp"
                      android:layout_margin="10dp"
                      android:textSize="@dimen/text_size"
                      android:inputType="none"
                      android:textIsSelectable="true"
                      android:textStyle="normal"
                      android:id="@+id/toeditext"
                      android:textColor="@android:color/white"
                      />
      

      【讨论】:

        猜你喜欢
        • 2016-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-05
        • 1970-01-01
        • 2012-05-19
        • 1970-01-01
        • 2019-10-17
        相关资源
        最近更新 更多