将 EditText 设置为只读

我想做一个只读的 EditText视图。执行此代码的 XML 似乎是 android:editable="false",但我想在代码中执行此操作。

我怎么能这么做?

201095 次浏览

最好的方法是使用 TextView

editText.setEnabled(false);
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) : "";
}
} });

这将为您提供不可编辑的 EditText 筛选器。您首先需要将所需的文本放在 edit Text 字段上,然后应用此筛选器。

请用这个密码。

Edittext.setEnabled(false);

尝试使用

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

如果你的 setEnabled(false)那么你的 editText将看起来禁用(灰色等)。您可能不希望更改编辑器的可视方面。

使用 setFocusable(false)是一种侵入性较小的方法。

我相信这个答案更接近你最初的意图。

这对我有用:

EditText.setKeyListener(null);

尝试覆盖编辑文本的 onLongClick 监听器以删除上下文菜单:

EditText myTextField = (EditText)findViewById(R.id.my_edit_text_id);
myTextField.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});

写这两行对你的工作来说已经足够了。

yourEditText.setKeyListener(null);
yourEditText.setEnabled(false);
android:editable

如果设置,则指定此 TextView具有输入方法。除非另有说明,否则它将是文本的。对于 TextView,默认情况下为 false。对于 EditText,默认情况下为 true。

必须是 boolean值,无论是 true还是 false

这也可能是对包含此类型值的资源(表单 @[package:]type:name)或主题属性(表单 ?[package:][type:]name)的引用。

这对应于全局属性资源符号可编辑。

相关方法

XML中使用:

android:editable="false"

举个例子:

<EditText
android:id="@+id/EditText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:editable="false" />

这是我的实现(有点长,但对我很有用!) : 使用此代码可以使 EditView 成为只读或正常。即使处于只读状态,用户也可以复制文本。您可以更改背景,使其看起来不同于普通的 EditText。

public static TextWatcher setReadOnly(final EditText edt, final boolean readOnlyState, TextWatcher remove) {
edt.setCursorVisible(!readOnlyState);
TextWatcher tw = null;
final String text = edt.getText().toString();
if (readOnlyState) {
tw = new TextWatcher();


@Override
public void afterTextChanged(Editable s) {


}
@Override
//saving the text before change
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}


@Override
// and replace it with content if it is about to change
public void onTextChanged(CharSequence s, int start,int before, int count) {
edt.removeTextChangedListener(this);
edt.setText(text);
edt.addTextChangedListener(this);
}
};
edt.addTextChangedListener(tw);
return tw;
} else {
edt.removeTextChangedListener(remove);
return remove;
}
}

此代码的好处是,EditText 显示为普通 EditText,但内容不可更改。返回值应该保持为一个变量,以便能够从只读状态恢复到正常状态。

要使 EditText 成为只读的,只需将其设置为:

TextWatcher tw = setReadOnly(editText, true, null);

并使其正常使用前面陈述中的 tw:

setReadOnly(editText, false, tw);

我可以使用以下方法使 EditTextPreferences 成为只读的:

editTextPref.setSelectable(false);

这种方法在结合使用“总结”字段来显示只读字段时效果很好(例如,对于显示帐户信息非常有用)。动态更新从 http://gmariotti.blogspot.com/2013/01/preferenceactivity-preferencefragment.html中提取的摘要字段

private static final List<String> keyList;


static {
keyList = new ArrayList<String>();
keyList.add("field1");
keyList.add("field2");
keyList.add("field3");
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);


for(int i=0;i<getPreferenceScreen().getPreferenceCount();i++){
initSummary(getPreferenceScreen().getPreference(i));
}
}


private void initSummary(Preference p) {
if (p instanceof PreferenceCategory) {
PreferenceCategory pCat = (PreferenceCategory) p;
for (int i = 0; i < pCat.getPreferenceCount(); i++) {
initSummary(pCat.getPreference(i));
}
} else {
updatePrefSummary(p);
}
}


private void updatePrefSummary(Preference p) {
if (p instanceof ListPreference) {
ListPreference listPref = (ListPreference) p;
p.setSummary(listPref.getEntry());
}
if (p instanceof EditTextPreference) {
EditTextPreference editTextPref = (EditTextPreference) p;
//editTextPref.setEnabled(false); // this can be used to 'gray out' as well
editTextPref.setSelectable(false);
if (keyList.contains(p.getKey())) {
p.setSummary(editTextPref.getText());
}
}
}

在 EditTextView xml 文件中设置此选项

android:focusable="false"

考虑到上面的几个建议,这对我来说很有效。使 TextEdit 可聚焦,但是如果用户单击或聚焦,我们将在 PopupWindow 中显示选择列表。(我们正在替换古怪的 Spinner 小部件)。TextEdit xml 是非常通用的..。

public void onCreate(Bundle savedInstanceState) {
....
fEditState = (EditText) findViewById(R.id.state_edit);


fEditState.setLongClickable(false);
fEditState.setKeyListener(null);
fEditState.setFocusable(true);


fEditState.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
showStatesPopup();


}
}
});


fEditState.setOnClickListener(new Button.OnClickListener(){


public void onClick(View arg0)
{
showStatesPopup();
}
});
....
}


private void showStatesPopup()
{
// fPopupWindowStates instantiated in OnCreate()


if (!fPopupWindowStates.isShowing()) {
// show the list view as dropdown
fPopupWindowStates.showAsDropDown(fEditState, -5, 0);
}
}

设置为 XML

android:inputType="none"
editText.setInputType(InputType.TYPE_NULL);

根据 医生,这可以防止软键盘被显示。它还可以防止粘贴,允许滚动,并且不改变视图的视觉方面。但是,这也会阻止在视图中选择和复制文本。

从我的测试设置 setInputTypeTYPE_NULL似乎是功能等同于折旧的 android:editable="false"。此外,android:inputType="none"似乎没有明显的影响。

如果您只想能够从控件中复制文本,但不能编辑它,那么可能需要使用 文本视图,并将 短信设置为 可供选择

密码:

myTextView.setTextIsSelectable(true);
myTextView.setFocusable(true);
myTextView.setFocusableInTouchMode(true);
// myTextView.setSelectAllOnFocus(true);

Xml:

<TextView
android:textIsSelectable="true"
android:focusable="true"
android:focusableInTouchMode="true"
...
/>
<!--android:selectAllOnFocus="true"-->


SetTextIsSelectable的文件显示:

当您调用这个方法来设置 textIsSelectable 的值时,它会将标志 FocusableInTouchMode、 clickable 和 longClickable 设置为相同的值..。

然而,我必须明确地设置可聚焦和可聚焦 InTouchMode 为 true,以使其能够与触摸输入一起工作。

使用以下代码:

editText.setEnabled(false);
editText.setTextColor(ContextCompat.getColor(context, R.color.black);

禁用 editText只读的外观和行为,但也将 text-color改为灰色,因此需要设置其文本颜色。

Android: editable = “ false” 已被弃用。因此您不能使用它来使编辑文本只读。

我已经做到这一点使用波纹管的解决方案。在这里,我已经使用

Android: inputType = “ none”

Android: textIsSelectable = “ true”

机器人: 可聚焦 = “ false”

试试看:)

<EditText
android:id="@+id/et_newsgpa_university"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/hint_educational_institute"
android:textSize="@dimen/regular_text"
android:inputType="none"
android:textIsSelectable="true"
android:focusable="false"
android:maxLines="1"
android:imeOptions="actionNext"/>

对我来说,这是唯一完全简单的解决办法。

editText.setEnabled(false);   // Prevents data entry
editText.setFocusable(false); // Prevents being able to tab to it from keyboard

由于 android:editable=""已被废弃,

设置

  • android:clickable="false"
  • android:focusable="false"
  • android:inputType="none"
  • android:cursorVisible="false"

将使其成为“只读”。< br > < br > 但是,用户仍然可以粘贴到字段中或执行任何其他长单击操作。要禁用此选项,只需覆盖 LongClickListener ()。
在 Kotlin:

  • myEditText.setOnLongClickListener { true }

足够了。

我的方法是创建一个自定义 TextWatcher 类,如下所示:

class ReadOnlyTextWatcher implements TextWatcher {
private final EditText textEdit;
private String originalText;
private boolean mustUndo = true;


public ReadOnlyTextWatcher(EditText textEdit) {
this.textEdit = textEdit;
}


@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (mustUndo) {
originalText = charSequence.toString();
}
}


@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


}


@Override
public void afterTextChanged(Editable editable) {
if (mustUndo) {
mustUndo = false;
textEdit.setText(originalText);
} else {
mustUndo = true;
}
}
}

然后你只需要将这个观察者添加到任何你想要只读的字段中,尽管它已经启用:

editText.addTextChangedListener(new ReadOnlyTextWatcher(editText));

在 java 文件中:

Edittext.setEnabled(false);

在 xml 文件中:

android:editable="false"