下面列出了android.widget.EditText#focusSearch ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@OnClick({ R.id.zero, R.id.one, R.id.two, R.id.three, R.id.four,
R.id.five, R.id.six, R.id.seven, R.id.eight, R.id.nine })
void onClick(TextView view) {
if (mFocusGrabber.isFocused())
return;
EditText field = getFocusedField();
int at = field.getSelectionStart();
field.getText().replace(at, at + 1, view.getText());
field.setSelection(at + 1);
// updateStartButtonVisibility();
if (field.getSelectionStart() == FIELD_LENGTH) {
// At the end of the current field, so try to focus to the next field.
// The search will return null if no view can be focused next.
View next = field.focusSearch(View.FOCUS_RIGHT);
if (next != null) {
next.requestFocus();
if (next instanceof EditText) {
// Should always start off at the beginning of the field
((EditText) next).setSelection(0);
}
}
}
}
private void handleEnter() {
View currentFocusView = getRootView().findFocus();
if (currentFocusView == null) {
return;
}
if (currentFocusView instanceof EditText) {
EditText currentFocusEt = (EditText) currentFocusView;
if (currentFocusEt.getImeActionId() > 0) {
currentFocusEt.onEditorAction(currentFocusEt.getImeActionId());
} else {
View nextFocusView = currentFocusEt.focusSearch(View.FOCUS_DOWN);
if (nextFocusView != null) {
nextFocusView.requestFocus(View.FOCUS_DOWN);
return;
} else {
if (imm.isActive(currentFocusEt)) {
imm.hideSoftInputFromWindow(currentFocusEt.getWindowToken(), 0);
}
hideKeyboard();
return;
}
}
}
}
@OnClick(R.id.backspace)
void backspace() {
if (mFocusGrabber.isFocused()) {
mEditFieldsLayout.focusSearch(mFocusGrabber, View.FOCUS_LEFT).requestFocus();
}
EditText field = getFocusedField();
if (field == null)
return;
int at = field.getSelectionStart();
if (at == 0) {
// At the beginning of current field, so move focus
// to the preceding field
View prev = field.focusSearch(View.FOCUS_LEFT);
if (null == prev) {
// Reached the beginning of the hours field
return;
}
if (prev.requestFocus()) {
if (prev instanceof EditText) {
// Always move the cursor to the end when moving focus back
((EditText) prev).setSelection(FIELD_LENGTH);
}
// Recursively backspace on the newly focused field
backspace();
}
} else {
field.getText().replace(at - 1, at, "0");
field.setSelection(at - 1);
// updateStartButtonVisibility();
}
}
private void configureDoneButton() {
View currentFocusView = getRootView().findFocus();
if (currentFocusView == null) {
return;
}
if (currentFocusView instanceof EditText) {
EditText currentFocusEt = (EditText) currentFocusView;
if (!Utils.isEmpty(currentFocusEt.getImeActionLabel() == null ? null :
currentFocusEt.getImeActionLabel().toString())) {
setEnterKeyText(currentFocusEt.getImeActionLabel());
} else if (currentFocusEt.getImeActionId() > 0) {
switch (currentFocusEt.getImeActionId()) {
case EditorInfo.IME_ACTION_DONE:
case EditorInfo.IME_ACTION_GO:
case EditorInfo.IME_ACTION_SEND:
setEnterKeyText(getResources().getString(R.string.password_keyboard_done));
break;
case EditorInfo.IME_ACTION_NEXT:
setEnterKeyText(getResources().getString(R.string.password_keyboard_next));
break;
default:
break;
}
} else {
View nextFocusView = currentFocusEt.focusSearch(View.FOCUS_DOWN);
if (nextFocusView != null) {
setEnterKeyText(getResources().getString(R.string.password_keyboard_next));
} else {
setEnterKeyText(getResources().getString(R.string.password_keyboard_done));
}
}
EntryKeyboard keyboard = (EntryKeyboard) getKeyboard();
invalidateKey(keyboard.getEnterKeyIndex());
}
}