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

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

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

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


当前回答

从@渐近线对已接受答案的评论中,使用:

myEditText.setEnabled(false);
myEditText.setInputType(InputType.TYPE_NULL);

...鲍勃是你的叔叔。

其他回答

使用此选项可禁用用户输入

android:focusable="false"

android:editable="false"此方法已弃用。

使用android:editable="false"是Depracted。相反,你需要使用android:focusable="false"

As android:editable="false"已弃用 在xml中

使用android:enabled="false",这很简单。为什么要使用更多的代码?

如果你想在java课上使用,你也可以通过编程的方式使用它

 editText.setEnabled(false);

要禁用EditText的功能,只需使用:

EditText.setInputType(InputType.TYPE_NULL);

如果你想以某种方式启用它,那么你可以使用:

EditText.setInputType(InputType.TYPE_CLASS_TEXT);

我使用谷歌新发布的材料设计库。在我的情况下,它工作时,我使用 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>