我认为拒绝所有更改的InputFilter是一个很好的解决scheme:

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) : ""; } } });

编辑: dlazarbuild议(下面)改变return到dst.subSequence(dstart, dend)克服行为,消除单词。

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

这确保EditText控件不能被select和聚焦,所以它不能被编辑。

我只是自己试了一下,

禁用编辑文本:

.setFocusable(false);

这也setFocusableInTouchMode设置为false!

要启用编辑文字:

setFocusableInTouchMode(true);

这也将setFocusable设置为true;

最好的办法是用这一行代码:

textView.setKeyListener(null);

文档说这个方法:

设置与此TextView一起使用的关键侦听器。 这可以为空以禁止用户input。

android:editable="false" android:inputType="none"

在你的XML或

EditText mEdit = (EditText) findViewById(R.id.yourid); mEdit.setEnabled(false);

要么

EditText mEdit = (EditText) findViewById(R.id.yourid); mEdit.setKeyListener(null);

你可以试试这个:

mEditText.setFocusable(false); mEditText.setClickable(false); mEditText.setFocusableInTouchMode(false); mEditText.setLongClickable(false); mEditText.setInputType(InputType.TYPE_NULL);

这将完全禁用EditText,禁止长按,如果你不想让用户打开编辑文本选项。

在EditText类中没有看到该属性的相关方法。 但是,您可以使用其他类似的东西,例如android:focus/setFocusable(boolean)或者创build另一个TextView,其android:editable="false"并使用setVisiblilty()在可编辑视图和不可编辑视图之间切换。 如果使用View.GONE ,用户将永远不会知道有两个EditText。

如果你的感觉雄心勃勃,你可能会用EditText的onTextChanged监听器来做一些事情,比如让它和一个setText做反应。

[发表一个新的答案,因为我不能评论约瑟夫的答案。]

inputfilter工作正常,但它有一个微妙的错误:在select上键入将删除所有文本。

例如,假设你在EditText有文本"foo" 。 如果全部选中(例如,双击它)并input'a' ,则文本将消失。 这是因为InputFilter将被调用为:

filter("a", 0, 1, "foo", 0, 3);

build议的inputfilter将在这种情况下返回空string(因为src.length() < 1为false ),这解释了缺失的文本。

解决方法是简单地在过滤函数中返回dst.subSequence(dstart, dend) 。 即使删除,这也可以正常工作。

你有没有尝试setText(java.lang.CharSequence,android.widget.TextView.BufferType) ? 它被描述为:

设置此TextView要显示的文本(请参阅setText(CharSequence)),并设置它是否存储在可修改的/可扩展的缓冲区中以及是否可编辑 。

(重点是我的)

我想指出一个很好的替代解决scheme,如果你正在创build一个EditView的新实例。 您可以按照文档build议的方式覆盖方法getDefaultEditable()以返回false。 例如

EditText view = new EditText(DiscountCalculator.this) { public boolean getDefaultEditable() { return false; } };

我猜

Edittext.setEnabled(false);

通过代码

android:enabled="false"

通过xml.Also检查这个post上。

他们应该工作,你可以再次编程启用Edittext.setEnabled(true);

我发现这个场景的唯一解决scheme是创build2个布局。 一个是可编辑的,一个不是。 您可能必须根据各种条件创build2个以上的布局。 将条件存储在SharedPreferences或其他方式中,并在重新启动活动后根据条件加载相关的布局。 这是一个例子:

在onCreate()的活动中:

configuration = new Configuration(this.getSharedPreferences(Configuration.SHARED_PREFERENCES_FILE_NAME, Context.MODE_PRIVATE)); manualSettingsMode = configuration.isManualSettingsMode(); if(manualSettingsMode){ setContentView(R.layout.editableconfigurationsettings); }else { setContentView(R.layout.configurationsettings); }

活动可以根据对条件和调用函数的testing重新启动,如下所示:

private void setManualEditing(){ configuration.set_isManualSettingsMode(true); this.recreate(); } private void setAutoEditing(){ configuration.set_isManualSettingsMode(false); this.recreate(); }

希望这可以帮助。 真的有一个更好的解决scheme,但这是我一直在做的。 理想情况下,人们可以在个别领域做到这一点,而不必重新加载活动/布局。 -bobby

我认为达到预期效果的正确方法是:

mEditView.setText("my text", BufferType.NORMAL);

如果要在可编辑和不可编辑之间切换,可以执行以下操作:

// Switch to non-editable mEditView.setText(mEditView.getText(), BufferType.NORMAL); // Switch back to editable mEditView.setText(mEditView.getText(), BufferType.EDITABLE);

尝试这个:

mEditText.setFilters(new InputFilter[]{new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { if (XXX) { return ""; } else { return null; } } }});