在我的应用程序中,我有一个EditText,用户只有读访问权,没有写访问权。

在代码中,我设置android:enabled="false"。

虽然EditText的背景变成了黑色,但当我点击它时,键盘就会弹出来,我可以更改文本。

我应该设置什么来禁用EditText?


当前回答

在我的情况下,我需要我的EditText滚动文本,如果没有。当它被禁用时,行数超过maxLines。这个实现非常适合我。

private void setIsChatEditTextEditable(boolean value)
{
    if(value)
    {
        mEdittext.setCursorVisible(true);
        mEdittext.setSelection(chat_edittext.length());
       // use new EditText(getApplicationContext()).getKeyListener()) if required below
        mEdittext.setKeyListener(new AppCompatEditText(getApplicationContext()).getKeyListener());  
    }
    else
    {
        mEdittext.setCursorVisible(false);
        mEdittext.setKeyListener(null);
    }
}

其他回答

对于禁用编辑EditText,我认为我们可以使用可聚焦或启用但是

使用android:启用=…或editText.setEnabled(…) 它还将EditText中的文本颜色更改为灰色。 当点击它没有效果 使用android: focusable =…或者editText.setFocusableInTouchMode(true) 它不会改变EditText的文本颜色 当点击它时,它会突出显示EditText的底线大约几毫秒

输出

使用EditText.setFocusable(false)禁用编辑 setfocusableintouchmode (true)启用编辑;

有多重是如何实现多重伤残的。

editText.setShowSoftInputOnFocus(假);和editText.setFocusable

防止编辑文本在某些文本中显示键盘书写。但光标仍然可见,用户可以粘贴一些文本。

editText.setCursorVisible(假)

隐藏光标。我不知道你为什么要这么做。用户可以输入文本和粘贴。

editText.setKeyListener(空)

我觉得这样最方便。没有办法用户如何输入文本,但小部件仍然与OnClickListener工作,如果你想触发操作时,用户触摸它

editText.setEnabled(假);

完全禁用EditText。它是字面上的“只读”,用户不能在其中输入任何文本,(例如)OnClickListener不能与它一起工作。

文本编辑文档

今天我仍然使用editable="false",但也使用focusable="false"。

我认为我们需要使EditText不可编辑的情况,是因为我们想保持它的EditText样式(有下划线,有提示等),但它接受其他输入而不是文本。例如一个下拉列表。

在这种用例中,我们需要EditText可点击(因此enabled="false"不合适)。设置focusable="false"完成这个技巧,但是,我仍然可以长时间按住EditText并从剪贴板粘贴我自己的文本到它。这取决于你的代码和处理方式,甚至可能导致应用程序崩溃。

所以我也使用了editable="false"现在一切都很好,除了警告。

我使用谷歌新发布的材料设计库。在我的情况下,它工作时,我使用 android: focusable = " false " android: cursorVisible = " false "

<com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/to_time_input_layout"
                    app:endIconMode="custom"
                    app:endIconDrawable="@drawable/ic_clock"
                    app:endIconContentDescription="ToTime"
                    app:endIconTint="@color/colorAccent"
                    style="@style/OutlinedEditTextStyle"
                    android:hint="To Time">

                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/to_time_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:focusable="false"
                        android:cursorVisible="false" />

</com.google.android.material.textfield.TextInputLayout>