Android-EditText不可编辑且不可选择 [英] Android - EditText not editable and not selectable

查看:236
本文介绍了Android-EditText不可编辑且不可选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动代码中,我有以下几行

In my activity code I have these lines

EditText editText=(EditText)findViewById(R.id.editText);
editText.setTextIsSelectable(false);

我要实现的是EditText无法编辑且无法选择. 在上一行中,我尝试实现第二个选项,但是它不起作用. EditText仍然是可选的.

What I want to achieve is a EditText uneditable and unselectable. With the previous line I try to achieve the second option, but it'doesn't work. The EditText is still selectable.

为了使EditText不可编辑,我没有找到任何方法,例如setEditable或类似方法.我读过有关设置InputType的信息,但没有成功.

In order to make the EditText uneditable I have not found any method such as setEditable or similar. I read about setting InputType but I had no success.

但是,可以弱化不可选择的约束.我的主要目标是EditText中的文本不能由于任何原因而更改.由于这些原因,EditRext可以选择,但是我希望没有用户可以剪切文本.

However the unselectable constraint can be weakened. My main goal is that the text inside the EditText cannot change for any reason. For these reason the EditRext could be selectable but I want that no user can cut the text.

我不想要XML,而只想要Java代码.所有这些东西都需要以编程方式进行.

推荐答案

抱歉,至少对于我的设置(带有AppCompatActivity的Sdk版本25)

Sorry guys, at least for my setup (Sdk Version 25 with AppCompatActivity)

editText.setFocusable(false);

仍然允许我按下虚拟键盘的返回按钮,在文本中添加行.如果您添加

will still let me push the return button of the virtual keyboard, adding lines to the text. If you add

editText.setEnabled(false);

此行为停止了,但是文本变成了灰色.

this behaviour stopps, however, the text is greyed out.

因此,我认为以下是更好的解决方案:

Therefore, I think the following would be a better solution:

editText.setInputType(InputType.TYPE_NULL);

这篇关于Android-EditText不可编辑且不可选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆