在我的应用程序中,我有一个EditText,用户只有读访问权,没有写访问权。
在代码中,我设置android:enabled="false"。
虽然EditText的背景变成了黑色,但当我点击它时,键盘就会弹出来,我可以更改文本。
我应该设置什么来禁用EditText?
在我的应用程序中,我有一个EditText,用户只有读访问权,没有写访问权。
在代码中,我设置android:enabled="false"。
虽然EditText的背景变成了黑色,但当我点击它时,键盘就会弹出来,我可以更改文本。
我应该设置什么来禁用EditText?
当前回答
android:editable="false"
现在已弃用和使用
YourEditText.setInputType(InputType.TYPE_NULL);
其他回答
今天我仍然使用editable="false",但也使用focusable="false"。
我认为我们需要使EditText不可编辑的情况,是因为我们想保持它的EditText样式(有下划线,有提示等),但它接受其他输入而不是文本。例如一个下拉列表。
在这种用例中,我们需要EditText可点击(因此enabled="false"不合适)。设置focusable="false"完成这个技巧,但是,我仍然可以长时间按住EditText并从剪贴板粘贴我自己的文本到它。这取决于你的代码和处理方式,甚至可能导致应用程序崩溃。
所以我也使用了editable="false"现在一切都很好,除了警告。
禁用=焦点+点击+光标
禁用焦点、单击和光标可见性对我来说很管用。
下面是XML格式的代码
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:cursorVisible="false"
android:clickable="false"
/>
使用EditText.setFocusable(false)禁用编辑 setfocusableintouchmode (true)启用编辑;
正如一些回答提到的,如果你禁用editText,他变成灰色,如果你设置focusable false光标显示。
如果你只想用xml来做,这就可以了
<YourFloatLabel
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/view_ads_search_select"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"/>
</YourFloatLabel>
我只是添加了一个FrameLayout出现在editText上面,并将其设置为可聚焦和可点击,这样editText就不能被点击了。
使用此选项可禁用用户输入
android:focusable="false"
android:editable="false"此方法已弃用。