How to replicate android:editable=“false” in code?

2020-01-24 21:19发布

In the layout you can set the EditText widget to be non-editable via the android:editable attribute.

How can I do this in code? I need to make the EditText widget to be editable depending on conditions.

14条回答
贪生不怕死
2楼-- · 2020-01-24 21:50

Have you tried setText(java.lang.CharSequence, android.widget.TextView.BufferType) ? It's described as:

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.

(emphasis mine)

查看更多
何必那么认真
3楼-- · 2020-01-24 21:51

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.

查看更多
爷的心禁止访问
4楼-- · 2020-01-24 21:55
editText.setFocusable(false);
editText.setClickable(false);

this ensure the EditText control can't be selected and focused, so it can't be edited.

查看更多
你好瞎i
5楼-- · 2020-01-24 21:59

I just tried this myself,

To disable editing text:

.setFocusable(false);

this also sets setFocusableInTouchMode to false!

To enable editing text:

setFocusableInTouchMode(true);

this also sets setFocusable to true;

查看更多
6楼-- · 2020-01-24 21:59

The only solution I have found for this scenario is to create 2 layouts. One is editable and one is not. You may have to create more than 2 layouts based on various conditions. Store the conditions in SharedPreferences or other means and load the relevant layout based on conditions after restarting the Activity. Here's an example:

in onCreate() of the activity:

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

The activity can be restarted based on testing for condition and calling functions as:

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

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

Hope this helps. There really has to be a better solution, but this is what I've been doing. Ideally, one would be able to do this on individual fields and not have to reload the activity / layouts. -bobby

查看更多
姐就是有狂的资本
7楼-- · 2020-01-24 22:01

I think an InputFilter that rejects all changes is a good solution:

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.

查看更多
登录 后发表回答