下面列出了android.view.inputmethod.InputConnection#performEditorAction ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Ask the input target to execute its default action via
* {@link InputConnection#performEditorAction
* InputConnection.performEditorAction()}.
*
* @param fromEnterKey If true, this will be executed as if the user had
* pressed an enter key on the keyboard, that is it will <em>not</em>
* be done if the editor has set {@link EditorInfo#IME_FLAG_NO_ENTER_ACTION
* EditorInfo.IME_FLAG_NO_ENTER_ACTION}. If false, the action will be
* sent regardless of how the editor has set that flag.
*
* @return Returns a boolean indicating whether an action has been sent.
* If false, either the editor did not specify a default action or it
* does not want an action from the enter key. If true, the action was
* sent (or there was no input connection at all).
*/
public boolean sendDefaultEditorAction(boolean fromEnterKey) {
EditorInfo ei = getCurrentInputEditorInfo();
if (ei != null &&
(!fromEnterKey || (ei.imeOptions &
EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0) &&
(ei.imeOptions & EditorInfo.IME_MASK_ACTION) !=
EditorInfo.IME_ACTION_NONE) {
// If the enter key was pressed, and the editor has a default
// action associated with pressing enter, then send it that
// explicit action instead of the key event.
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.performEditorAction(ei.imeOptions&EditorInfo.IME_MASK_ACTION);
}
return true;
}
return false;
}
public boolean sendDefaultAction() {
if (!allowsDefaultAction()) {
return false;
}
EditorInfo ei = getCurrentInputEditorInfo();
InputConnection ic = getCurrentInputConnection();
if (ei == null || ic == null) {
return false;
}
cancelComposingText();
int actionId = ei.actionId;
if (actionId != 0) {
return ic.performEditorAction(actionId);
} else {
return sendDefaultEditorAction(false);
}
}
/**
* Figure out how we are connected to the edittext and what it expects the
* enter key to do.
*/
private void handleEnterKey(InputConnection con) {
EditorInfo ei = myService.getCurrentInputEditorInfo();
if (ei != null
&& ((ei.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
int[] acts = { EditorInfo.IME_ACTION_DONE, EditorInfo.IME_ACTION_SEARCH,
EditorInfo.IME_ACTION_GO, EditorInfo.IME_ACTION_NEXT };
for (int i : acts) {
if ((ei.imeOptions & i) == i) {
con.performEditorAction(i);
return;
}
}
}
typeKey(con, KeyEvent.KEYCODE_ENTER);
}
@Subscriber(value = @Param(value = IME_EDITORCODE, sticky = false), thread = RunningThread.MAIN_THREAD)
public boolean inputEditorCode(int code) {
if (code != -1) {
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.performEditorAction(code);
return true;
}
}
return false;
}
@Override
public void perform(UiController uiController, View view) {
EditorInfo editorInfo = new EditorInfo();
InputConnection inputConnection = view.onCreateInputConnection(editorInfo);
if (inputConnection == null) {
throw new PerformException.Builder()
.withActionDescription(this.toString())
.withViewDescription(HumanReadables.describe(view))
.withCause(new IllegalStateException("View does not support input methods"))
.build();
}
int actionId =
editorInfo.actionId != 0
? editorInfo.actionId
: editorInfo.imeOptions & EditorInfo.IME_MASK_ACTION;
if (actionId == EditorInfo.IME_ACTION_NONE) {
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(new IllegalStateException("No available action on view"))
.build();
}
if (!inputConnection.performEditorAction(actionId)) {
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(
new RuntimeException(
String.format(
Locale.ROOT,
"Failed to perform action %#x. Input connection no longer valid",
actionId)))
.build();
}
}
@Override
public void onClick(View v) {
final char actualCode = (char) v.getTag();
final InputConnection ic = getCurrentInputConnection();
final EditorInfo editorInfo = getCurrentInputEditorInfo();
switch (actualCode) {
case BaldKeyboard.BACKSPACE:
backspace();
break;
case BaldKeyboard.SHIFT:
try {
((BaldKeyboard.Capitalised) keyboard).setCaps();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
break;
case BaldKeyboard.ENTER:
if (defaultEditorActionExists(editorInfo.imeOptions)) {
ic.performEditorAction(editorInfo.imeOptions & EditorInfo.IME_MASK_ACTION);
} else {
sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);
}
break;
case BaldKeyboard.HIDE:
hideWindow();
break;
case BaldKeyboard.LANGUAGE:
changeLanguage(keyboard.nextLanguage());
break;
case BaldKeyboard.NUMBERS:
changeLanguage(onNumbers ? lastLanguage : NumberKeyboard.LANGUAGE_ID);
onNumbers = !onNumbers;
break;
case BaldKeyboard.SPEECH_TO_TEXT:
startVoiceListening();
break;
default:
final String str = String.valueOf(actualCode);
ic.commitText(str, 1);
}
}