如何在代码中复制 android:editable="false"?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/660151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to replicate android:editable="false" in code?
提问by AngryHacker
In the layout you can set the EditText
widget to be non-editable via the android:editable attribute
.
在布局中,您可以EditText
通过android:editable attribute
.
How can I do this in code? I need to make the EditText
widget to be editable depending on conditions.
我怎样才能在代码中做到这一点?我需要EditText
根据条件使小部件可编辑。
采纳答案by Josef Pfleger
I think an InputFilter
that rejects all changes is a good solution:
我认为InputFilter
拒绝所有更改是一个很好的解决方案:
editText.setFilters(new InputFilter[] {
new InputFilter() {
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend) {
return src.length() < 1 ? dst.subSequence(dstart, dend) : "";
}
}
});
Edit:dlazar suggested (below) to change the return
to dst.subSequence(dstart, dend)
to overcome behavior that removes words.
编辑:dlazar 建议(如下)更改return
todst.subSequence(dstart, dend)
以克服删除单词的行为。
回答by Gerry
editText.setFocusable(false);
editText.setClickable(false);
this ensure the EditText
control can't be selected and focused, so it can't be edited.
这确保EditText
无法选择和聚焦控件,因此无法对其进行编辑。
回答by atpanos
I just tried this myself,
我自己刚试过这个
To disable editing text:
要禁用编辑文本:
.setFocusable(false);
this also sets setFocusableInTouchMode to false!
这也将 setFocusableInTouchMode 设置为 false!
To enable editing text:
要启用编辑文本:
setFocusableInTouchMode(true);
this also sets setFocusable to true;
这也将 setFocusable 设置为 true;
回答by Christopher Perry
The best way to do this is with this single line of code:
最好的方法是使用这行代码:
textView.setKeyListener(null);
The docs say for this method:
文档对这种方法说:
Sets the key listener to be used with this TextView. This can be null to disallow user input.
设置要与此 TextView 一起使用的键侦听器。这可以为 null 以禁止用户输入。
回答by abhi
android:editable="false"
android:inputType="none"
in your xml or
在您的 xml 或
EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setEnabled(false);
or
或者
EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setKeyListener(null);
回答by Babasaheb
You can try this:
你可以试试这个:
mEditText.setFocusable(false);
mEditText.setClickable(false);
mEditText.setFocusableInTouchMode(false);
mEditText.setLongClickable(false);
mEditText.setInputType(InputType.TYPE_NULL);
This will completely disable EditText, disable long press if you don't want user to open edit text options.
这将完全禁用 EditText,如果您不希望用户打开编辑文本选项,请禁用长按。
回答by Anirudh Sharma
回答by dlazar
[Posting a new answer, since I can't comment on Josef's answer.]
[发布新答案,因为我无法评论约瑟夫的答案。]
The input filter works fine, but it has a subtle bug in it: typing over a selection will delete all the text.
输入过滤器工作正常,但它有一个微妙的错误:在选择上键入将删除所有文本。
For example, say you have the text "foo"
in the EditText
. If you select it all (e.g., by double-clicking on it) and type 'a'
, the text will disappear. This is because the InputFilter
will be called as:
例如,假设您"foo"
在EditText
. 如果您全选(例如,通过双击它)并键入'a'
,文本将消失。这是因为InputFilter
将被称为:
filter("a", 0, 1, "foo", 0, 3);
The proposed input filter will return the empty string in this case (because src.length() < 1
is false
), which explains the missing text.
在这种情况下,建议的输入过滤器将返回空字符串(因为src.length() < 1
是false
),这解释了丢失的文本。
The solution is to simply return dst.subSequence(dstart, dend)
in the filter function. This will work fine even for deletions.
解决方案是简单地dst.subSequence(dstart, dend)
在过滤器函数中返回。即使对于删除,这也能正常工作。
回答by Will
I do not see a related method for that attribute in the EditText
class. However, there are other similar things you could use such as
android:focus/setFocusable(boolean)
or create another TextView whose
android:editable="false"
and use setVisiblilty()
to switch between the
editable and not editable views. If you use View.GONE
the user will
never know there are two EditTexts.
我在 EditText 类中没有看到该属性的相关方法。但是,您还可以使用其他类似的东西,例如
android:focus/setFocusable(boolean)
或创建另一个 TextView,其
android:editable="false"
和 用于setVisiblilty()
在可编辑和不可编辑视图之间切换。如果你使用View.GONE
用户永远不会知道有两个 EditTexts。
If your feeling ambitious you could probably do something with the EditText's onTextChanged
listener like having it react with a setText
.
如果您感觉雄心勃勃,您可能可以对 EditText 的onTextChanged
侦听器做一些事情,例如让它与setText
.
回答by pjz
Have you tried setText(java.lang.CharSequence, android.widget.TextView.BufferType)? It's described as:
你试过setText(java.lang.CharSequence, android.widget.TextView.BufferType)吗?它被描述为:
Sets the text that this TextView is to display (see setText(CharSequence)) and also sets whether it is stored in a styleable/spannable buffer and whether it is editable.
设置此 TextView 要显示的文本(请参阅 setText(CharSequence)),并设置它是否存储在可样式化/可跨越缓冲区中以及它是否可编辑。
(emphasis mine)
(强调我的)