java 我们可以在edittext中有不可编辑的文本吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/910135/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:19:02  来源:igfitidea点击:

Can we have uneditable text in edittext

javaandroidandroid-edittext

提问by Sam97305421562

I am using an EditText. Is it possible to have a part of text uneditable and the rest editable in the same EditText?

我正在使用一个EditText. 是否有可能让一部分文本不可编辑而其余文本可编辑EditText

回答by Josef Pfleger

You could use

你可以用

editText.setFocusable(false);

or

或者

editText.setEnabled(false);

although disabling the EditTextdoes currently not ignore input from the on-screen keyboard (I think that's a bug).

虽然禁用EditText当前不会忽略来自屏幕键盘的输入(我认为这是一个错误)。

Depending on the application it might be better to use an InputFilterthat rejects all changes:

根据应用程序,最好使用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) : "";
        }
    }
});

Also see thisquestion.

另请参阅问题。

回答by Ulrich Scheller

You can implement a TextChangedListener where you make sure those parts of your text wont get deleted/overwritten.

您可以实现一个 TextChangedListener 来确保文本的那些部分不会被获取deleted/overwritten

class TextChangedListener implements TextWatcher {
    public void afterTextChanged(Editable s) {
                makeSureNothingIsDeleted();
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    public void onTextChanged(CharSequence s, int start, int before, int count) {}
}


    TextChangedListener tcl = new TextChangedListener();
    my_editable.addTextChangedListener(tcl);