简体   繁体   English

Android - EditText 不可编辑且不可选择

[英]Android - EditText not editable and not selectable

In my activity code I have these lines在我的活动代码中,我有这些行

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

What I want to achieve is a EditText uneditable and unselectable.我想要实现的是EditText不可编辑和不可选择。 With the previous line I try to achieve the second option, but it'doesn't work.使用前一行,我尝试实现第二个选项,但它不起作用。 The EditText is still selectable. EditText仍然是可选的。

In order to make the EditText uneditable I have not found any method such as setEditable or similar.为了使EditText不可编辑,我还没有找到任何方法,例如setEditable或类似方法。 I read about setting InputType but I had no success.我阅读了有关设置InputType但没有成功。

However the unselectable constraint can be weakened.然而,不可选择的约束可以被削弱。 My main goal is that the text inside the EditText cannot change for any reason.我的主要目标是EditText中的文本不能因任何原因而改变。 For these reason the EditRext could be selectable but I want that no user can cut the text.由于这些原因, EditRext可以选择,但我希望没有用户可以剪切文本。

I want no XML but only Java code.我不想要 XML,只想要 Java 代码。 All these things are needed to to programmatically.所有这些都需要以编程方式进行。

Sorry guys, at least for my setup (Sdk Version 25 with AppCompatActivity)抱歉,至少对于我的设置(带有 AppCompatActivity 的 SDK 版本 25)

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);

This works fine;这很好用; just set focusable property of your edittext to "false" and you are done.只需将 edittext 的 focusable 属性设置为“false”即可。

<EditText
        android:id="@+id/EditTextInput"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:gravity="right"
        android:cursorVisible="true">
    </EditText>

I would implement the following code:我将实现以下代码:

editText.setFocusable(false);
editText.setClickable(false);
editText.setEnabled(false);

Which should prevent interacting with the EditText .这应该防止与EditText交互。 Read more about the View class here since EditText extends from it:在此处阅读有关View类的更多信息,因为EditText从它扩展而来的:

https://developer.android.com/reference/android/view/View.html#setFocusable(boolean) https://developer.android.com/reference/android/view/View.html#setFocusable(boolean)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM