I have an editText that I want to fill in automatically with info from other controls in the form, and at the same time allow to change its contents, discouraging the user from doing so though.
So I set this control to not focusable so when you press actionNext it moves on to the next control. However if you click the edit text, I want to allow the user to change its contents.
This is what I did:
mNameEditText.setFocusable(false);
mNameEditText.setFocusableInTouchMode(false);
mNameEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mNameEditText.setFocusableInTouchMode(true);
mNameEditText.requestFocusFromTouch();
mNameEditText.setFocusable(true);
}
});
However this behaves very weirdly, the result is that when you click, you can edit, but suddenly the next control (an AutoCompleteTextView) is not focusable anymore! Actually it looks like the focus remains on the edit text and goes to the autocompletetextview at the same time, like so:
How can I fix this?
In touch mode, there is no focus and no selection. Any selected item in a list of in a grid becomes unselected as soon as the user enters touch mode. Similarly, any focused widgets become unfocused when the user enters touch mode.
In order to make an prevent an element from taking focus ("non-focusable"), you need to use Javascript to watch for the focus and prevent the default interaction. In order to prevent an element from being tabbed to, use tabindex=-1 attribute. Adding tabindex=-1 will make any element focusable, even div elements.
Your view can have different background when state is focused. Focusable in touch mode allows view to gain focus when user is touching the view, good example of this kind of component is EditText . With Button or any clickable component pressed state is usually what you are interested in.
I think you can make an element 'un-focusable' by defocusing it every time it is focused. You can accomplish this via: document.getElementById ("myElement").onfocus = function () { this.blur (); };
Any element with a tabindex. For an otherwise unfocusable element to become focusable, it must have a tabindex attribute. Elements with a negative tabindex can receive focus through scripting but not through keyboard tabbing. A web page’s focus order must preserve a page’s meaning and promote ease of use.
(Just added as an FYI for other googlers.) Note that divs with contenteditable="true" can also receive focus. Show activity on this post. Additionally, If you want to make a focussable element (form input elements etc.) as unfocussable. You can set : tabIndex = "-1" document.getElementById ("yourElement").setAttribute ("tabIndex", "-1");
In most browsers, users can move focus by pressing the Tab key and the Shift + Tab keys. The following elements can receive focus: Any element with a tabindex. For an otherwise unfocusable element to become focusable, it must have a tabindex attribute.
enabled
property to false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With