Android – when another EditText clears the focus, stop EditText to get the focus
In my activity, I get some EditText. When I move out of focus from a topic and make it unfocused, the focus will be given to another EditText. I eliminate the focus in the following ways:
atxtTag.setFocusable(false);
atxtTag.setFocusableInTouchMode(false);
atxtTag.clearFocus();
I have nothing to shift to the focus on another EditText. After removing the focus from the first element, I try to achieve this by setting the focus on another view. Therefore, I focus on the main layout, but the rolling view is invalid
How to move the focus away without moving the focus to another EditText?
thank you.
resolvent:
The clearfocus method implementation calls clearfocusinternal and passes true as the value of the refocus parameter. This results in a request for focus on the first focusable element. Therefore, a possible solution is to call clearfocusinternal (true, false) Through reflection, but I didn't actually check it
After in-depth study of requestfocus implementation, I found that it checked the descendantfocusability of the root view. Using this tool, I built a feasible solution:
ViewGroup rootView = (ViewGroup) atxtTag.getRootView();
int dfValue = rootView.getDescendantFocusability();
rootView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
atxtTag.clearFocus();
rootView.setDescendantFocusability(dfValue);
In addition, clearing focus does not hide the software keyboard, so you may also need to hide it:
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(atxtTag.getWindowToken(), 0);