我有以下代碼:如何有效清除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的焦點?