Block and unlock editing an Edittext

Asked

Viewed 4,041 times

0

In the code snippet below I disable the freight value edition when the freight type is SIF put the code snippet Library.editTexPermission(edtFrete, true); that should release editText edition does not work in the following situation: Select SIF and then select FOB. Ask how can I release the edition of edtFrete after blocking it?

        spFrete.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            switch (spFrete.getSelectedItemPosition()) {
                case 0: {  //FOB
                    Library.editTexPermission(edtFrete, true);

                    break;
                }

                case 1: {  //SIF
                    Library.editTexPermission(edtFrete, false);
                    edtFrete.setText("0.0");
                    break;
                }
            }
        }
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

Follow below the code snippet from editTexPermission with it becomes evident how the edit is blocked now the question is: how to set the Keylistener again?

public static void editTexPermission(EditText editText, Boolean enabled) {
    editText.setFocusable(enabled);
    if (!enabled)
        editText.setKeyListener(null);
}
  • You can post the code of editTexPermission ?

  • Ready Marco, code posted in question

  • 1

    I didn’t really understand the context of the situation, but to enable and disable the field you should not use the editText.setEnabled(true/false) ?

  • Yeah, as I said this excerpt was not mine, I even thought that the Library was something of Android itself! With simple search on the net found that the setKeyListener prevents any kind of action on the EditText, even selecting and copying your content but it is complicated to make it editable again without having to reload Ragment. I will change the text to what you suggested. Please post as reply so you can positive it.

1 answer

4


To block and unlock edittext editing use the property setEnabledof the component, for example :

editText.setEnabled(true);
editText.setEnabled(false);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.