如何有效清除 ScrollView 中多个 EditText 字段的焦点?
How to clear focus of multiple EditText fields in a ScrollView efficiently?
我有以下代码:
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 字段采用 table 格式。我知道我可以在 hideKeyboard
中分别对每个 EditText 调用 clearFocus()
,但这似乎效率不高。有没有更有效的方法来清除滚动时EditText的焦点?
因为只有一个 EditText
可以被聚焦并且你有 onFocused 侦听器你可以:
private Edittext focused;
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
((EditText) view).setSelection(((EditText) view).getText().length());
focused = view
}
}
然后在 hideKeyboard():
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
focused.clearFocus();
}
我认为这是一种糟糕的做事方式。您将搞砸除编辑文本以外的任何视图的任何触摸侦听器,并且其计算量很大。我真的建议你不要这样做 - 这不是 iOS,你应该点击后退按钮摆脱键盘。试图让它以这种方式工作是比其价值更多的努力,而且很难做到正确。
忽略它 - 更好的方法是使滚动视图的根视图可聚焦(并且在触摸模式下可聚焦),焦点为 BEFORE_DESCENDENTS,并将其聚焦在单击或滚动上。这样做意味着您不需要设置层次结构中每个视图的 onTouch,只需设置基础。然后在你的根视图 onFocus 中,当你获得焦点时隐藏键盘。
我有以下代码:
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 字段采用 table 格式。我知道我可以在 hideKeyboard
中分别对每个 EditText 调用 clearFocus()
,但这似乎效率不高。有没有更有效的方法来清除滚动时EditText的焦点?
因为只有一个 EditText
可以被聚焦并且你有 onFocused 侦听器你可以:
private Edittext focused;
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
((EditText) view).setSelection(((EditText) view).getText().length());
focused = view
}
}
然后在 hideKeyboard():
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
focused.clearFocus();
}
我认为这是一种糟糕的做事方式。您将搞砸除编辑文本以外的任何视图的任何触摸侦听器,并且其计算量很大。我真的建议你不要这样做 - 这不是 iOS,你应该点击后退按钮摆脱键盘。试图让它以这种方式工作是比其价值更多的努力,而且很难做到正确。
忽略它 - 更好的方法是使滚动视图的根视图可聚焦(并且在触摸模式下可聚焦),焦点为 BEFORE_DESCENDENTS,并将其聚焦在单击或滚动上。这样做意味着您不需要设置层次结构中每个视图的 onTouch,只需设置基础。然后在你的根视图 onFocus 中,当你获得焦点时隐藏键盘。