Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The design you've described isn't recommended. You're violating the <code>focusable</code> attribute's purpose which is not to control whether the user can alter the text in a <code>EditText</code> component.</p> <p>If you plan to provide an alternative input method because the use case seems to require this (e.g. you allow only a certain set of symbols in the editable text field) then you should probably disable text editing altogether for the time the user isn't allowed to change the value of the field.</p> <p>Declare your editable field:</p> <pre><code>&lt;EditText android:id="@+id/edit_text_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" /&gt; </code></pre> <p>notice that its <code>focusable</code> attribute is left with the default value. That's ok, we'll handle that later. Declare a button which will start editing process:</p> <pre><code>&lt;Button android:id="@+id/button_show_ime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start editing" /&gt; </code></pre> <p>Now, in your <code>Activity</code> declare:</p> <pre><code>private EditText editText2; private KeyListener originalKeyListener; private Button buttonShowIme; </code></pre> <p>And in <code>onCreate()</code> do this:</p> <pre><code>@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ime_activity); // Find out our editable field. editText2 = (EditText)findViewById(R.id.edit_text_2); // Save its key listener which makes it editable. originalKeyListener = editText2.getKeyListener(); // Set it to null - this will make the field non-editable editText2.setKeyListener(null); // Find the button which will start editing process. buttonShowIme = (Button)findViewById(R.id.button_show_ime); // Attach an on-click listener. buttonShowIme.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Restore key listener - this will make the field editable again. editText2.setKeyListener(originalKeyListener); // Focus the field. editText2.requestFocus(); // Show soft keyboard for the user to enter the value. InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText2, InputMethodManager.SHOW_IMPLICIT); } }); // We also want to disable editing when the user exits the field. // This will make the button the only non-programmatic way of editing it. editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { // If it loses focus... if (!hasFocus) { // Hide soft keyboard. InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText2.getWindowToken(), 0); // Make it non-editable again. editText2.setKeyListener(null); } } }); } </code></pre> <p>I hope the code with all the comments is self-explanatory. Tested on APIs 8 and 17.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload