Mygod Mygod - 4 months ago 27
Java Question

Readonly selectable EditText

I have an EditText that need to be switched between normal mode and readonly selectable mode (more specifically it's a kind of highlight mode where selection is set by

setSelection
and user can't change the selection). The problem is that sometimes the selection is not showing up. And after I toggle EditText to readonly selectable mode then toggle it back, when I press somewhere the cursor moved but the soft keyboard isn't showing up.

Here's the code to toggle it to readonly selectable mode:

inputText.setFocusable(false);
inputText.setTextIsSelectable(true);
inputText.setCursorVisible(true);
inputText.selectAll(); // for test purpose


Here's the code to toggle it back to normal mode:

inputText.setFocusableInTouchMode(true);
inputText.setFocusable(true); // this line can be omitted according to the source code


It seems selecting texts works for the first time and after that it never works unless the application is restarted.

Answer

At last I decided to use another TextView containing exactly the same text and display that one (and hiding EditText) when I need to "disable" my EditText.

Comments