在代码中,我设置了
android:enabled="false"
。虽然EditText的背景变暗,但当我单击它时,键盘弹出并且我可以更改文本。
要禁用EditText,应该设置什么?
android:enabled="false"
。我认为正确的做法是设置android:editable="false"
。
如果你想知道为什么我的链接指向TextView
的属性,答案是因为EditText
继承自TextView
:
EditText只是在TextView上覆盖了一层外壳,并将其配置为可编辑。
editText.setEnabled(false);
的意思是禁用文本编辑框,而 editText.setInputType(InputType.TYPE_NULL);
的意思是使文本编辑框无法输入任何文字。 - Asymptoteandroid:editable="false" android:focusable="false"
对我很管用(忽略它被废弃的事实,我认为android:editable与android:inputType无关)。 - Muhammad Babar使用EditText.setFocusable(false)
可以禁用编辑功能;
使用EditText.setFocusableInTouchMode(true)
可以启用编辑功能。
editText.setEnabled(false);
将更改文本颜色为灰色。然而,当使用setEnabled
时,下拉框不会更改文本颜色,这使得体验不一致。 - tbraunsetFocusable(true)
?请查看此链接以了解 setFocusable()
和 setFocusableInTouchMode()
之间的区别: https://dev59.com/WGAg5IYBdhLWcg3wQ5El - kenju您可以尝试以下方法:
private void disableEditText(EditText editText) {
editText.setFocusable(false);
editText.setEnabled(false);
editText.setCursorVisible(false);
editText.setKeyListener(null);
editText.setBackgroundColor(Color.TRANSPARENT);
}
启用的EditText:
禁用的EditText:
对我有用,希望能帮到您。
editText.setFocusableInTouchMode(true)
而不是editText.setFocusable(true)
。 - Dart Dega使用此功能禁用用户输入
android:focusable="false"
android:editable="false"这个方法已经被弃用了。
android:focusable="false"
之后添加android:longClickable="false"
,以便在触摸此视图时删除临时高亮显示。这也可以防止“全选”弹出窗口的出现。 - CzarMattandroid:editable="false"
已被废弃。你可以在EditText上使用InputType TYPE_NULL
像这样使用:
editText.setInputType(InputType.TYPE_NULL);
android:focusable="false"
一起工作,而android:inputType="none"
则不行。在这种情况下,我们可以通过长按来粘贴文本。 - undefinedandroid:editable="false"
已被弃用。在 xml
中使用 android:enabled="false"
更为简单。为什么要写更多的代码呢?
如果你想在 java class
中使用,可以通过编程方式实现。
editText.setEnabled(false);
简单来说:
editText.setEnabled(false);
我正在使用谷歌新发布的 Material Design 库。在我的情况下,当我使用 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>
EditText.setEnabled(false);
EditText.setInputType(InputType.TYPE_NULL);
EditText.setInputType(InputType.TYPE_CLASS_TEXT);
EditText
,只需要从 这个库 中使用UI.setReadOnly(myEditText, true)
。如果想要在没有该库的情况下复制此操作,请查看 此小方法的源代码。 - caw