我有以下代码:如何有效清除ScrollView中多个EditText字段的焦点?
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public void setupDialog(View view) {
// Set up touch listener for non-text box views to hide keyboard.
if (!(view instanceof EditText)) {
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
hideKeyboard(view);
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupDialog(innerView);
}
}
}
public void setupEditTextFocusChangedListeners(View view) {
if (view instanceof EditText) {
view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
((EditText) view).setSelection(((EditText) view).getText().length());
}
}
});
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupEditTextFocusChangedListeners(innerView);
}
}
}
虽然键盘隐藏,因为它应该点击或滚动任何的EditText字段外时,滚动时的EditText仍然保留焦点即使键盘不解雇。
EditText字段为表格格式。我知道我可以在hideKeyboard
的每个EditText上分别拨打clearFocus()
,但看起来效率并不高。滚动时是否有更有效的方式来清除EditText的焦点?