下面列出了android.view.inputmethod.EditorInfo#IME_MASK_ACTION 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static String imeActionName(final int imeOptions) {
final int actionId = imeOptions & EditorInfo.IME_MASK_ACTION;
switch (actionId) {
case EditorInfo.IME_ACTION_UNSPECIFIED:
return "actionUnspecified";
case EditorInfo.IME_ACTION_NONE:
return "actionNone";
case EditorInfo.IME_ACTION_GO:
return "actionGo";
case EditorInfo.IME_ACTION_SEARCH:
return "actionSearch";
case EditorInfo.IME_ACTION_SEND:
return "actionSend";
case EditorInfo.IME_ACTION_NEXT:
return "actionNext";
case EditorInfo.IME_ACTION_DONE:
return "actionDone";
case EditorInfo.IME_ACTION_PREVIOUS:
return "actionPrevious";
default:
return "actionUnknown(" + actionId + ")";
}
}
/**
* 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;
}
/**
* Return text that can be used as a button label for the given
* {@link EditorInfo#imeOptions EditorInfo.imeOptions}. Returns null
* if there is no action requested. Note that there is no guarantee that
* the returned text will be relatively short, so you probably do not
* want to use it as text on a soft keyboard key label.
*
* @param imeOptions The value from {@link EditorInfo#imeOptions EditorInfo.imeOptions}.
*
* @return Returns a label to use, or null if there is no action.
*/
public CharSequence getTextForImeAction(int imeOptions) {
switch (imeOptions&EditorInfo.IME_MASK_ACTION) {
case EditorInfo.IME_ACTION_NONE:
return null;
case EditorInfo.IME_ACTION_GO:
return getText(com.android.internal.R.string.ime_action_go);
case EditorInfo.IME_ACTION_SEARCH:
return getText(com.android.internal.R.string.ime_action_search);
case EditorInfo.IME_ACTION_SEND:
return getText(com.android.internal.R.string.ime_action_send);
case EditorInfo.IME_ACTION_NEXT:
return getText(com.android.internal.R.string.ime_action_next);
case EditorInfo.IME_ACTION_DONE:
return getText(com.android.internal.R.string.ime_action_done);
case EditorInfo.IME_ACTION_PREVIOUS:
return getText(com.android.internal.R.string.ime_action_previous);
default:
return getText(com.android.internal.R.string.ime_action_default);
}
}
/**
* Return a drawable resource id that can be used as a button icon for the given
* {@link EditorInfo#imeOptions EditorInfo.imeOptions}.
*
* @param imeOptions The value from @link EditorInfo#imeOptions EditorInfo.imeOptions}.
*
* @return Returns a drawable resource id to use.
*/
@DrawableRes
private int getIconForImeAction(int imeOptions) {
switch (imeOptions&EditorInfo.IME_MASK_ACTION) {
case EditorInfo.IME_ACTION_GO:
return com.android.internal.R.drawable.ic_input_extract_action_go;
case EditorInfo.IME_ACTION_SEARCH:
return com.android.internal.R.drawable.ic_input_extract_action_search;
case EditorInfo.IME_ACTION_SEND:
return com.android.internal.R.drawable.ic_input_extract_action_send;
case EditorInfo.IME_ACTION_NEXT:
return com.android.internal.R.drawable.ic_input_extract_action_next;
case EditorInfo.IME_ACTION_DONE:
return com.android.internal.R.drawable.ic_input_extract_action_done;
case EditorInfo.IME_ACTION_PREVIOUS:
return com.android.internal.R.drawable.ic_input_extract_action_previous;
default:
return com.android.internal.R.drawable.ic_input_extract_action_return;
}
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection connection = super.onCreateInputConnection(outAttrs);
int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION;
// if ((imeActions&EditorInfo.IME_ACTION_DONE) != 0) {
// // clear the existing action
// outAttrs.imeOptions ^= imeActions;
// // set the DONE action
// outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;
// }
if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
}
return connection;
}
public void setTransport(TransportOption transport) {
final String enterKeyType = SilencePreferences.getEnterKeyType(getContext());
final boolean isIncognito = SilencePreferences.isIncognitoKeyboardEnabled(getContext());
int imeOptions = (getImeOptions() & ~EditorInfo.IME_MASK_ACTION) | EditorInfo.IME_ACTION_SEND;
int inputType = getInputType();
if (isLandscape()) setImeActionLabel(transport.getComposeHint(), EditorInfo.IME_ACTION_SEND);
else setImeActionLabel(null, 0);
inputType = enterKeyType.equals("emoji")
? inputType | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE
: inputType & ~InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE;
setInputType(inputType);
setImeOptions(imeOptions);
setHint(transport.getComposeHint(),
transport.getSimName().isPresent()
? getContext().getString(R.string.conversation_activity__via_sim_name, transport.getSimName().get())
: null);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection connection = super.onCreateInputConnection(outAttrs);
int imeActions = outAttrs.imeOptions & EditorInfo.IME_MASK_ACTION;
if ((imeActions & EditorInfo.IME_ACTION_DONE) != 0) {
// clear the existing action
outAttrs.imeOptions ^= imeActions;
// set the DONE action
outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;
}
if ((outAttrs.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
}
outAttrs.actionId = EditorInfo.IME_ACTION_DONE;
outAttrs.actionLabel = getContext().getString(R.string.done);
return connection;
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
//Override normal multiline text handling of enter/done and force a done button
TokenInputConnection connection = new TokenInputConnection(super.onCreateInputConnection(outAttrs), true);
int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION;
if ((imeActions&EditorInfo.IME_ACTION_DONE) != 0) {
// clear the existing action
outAttrs.imeOptions ^= imeActions;
// set the DONE action
outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;
}
if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
}
return connection;
}
@Override
public InputConnection onCreateInputConnection(final EditorInfo outAttrs)
{
final InputConnection connection=super.onCreateInputConnection(outAttrs);
final int imeActions=outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION;
if((imeActions&EditorInfo.IME_ACTION_DONE)!=0)
{
// clear the existing action
outAttrs.imeOptions^=imeActions;
// set the DONE action
outAttrs.imeOptions|=EditorInfo.IME_ACTION_DONE;
}
if((outAttrs.imeOptions&EditorInfo.IME_FLAG_NO_ENTER_ACTION)!=0)
outAttrs.imeOptions&=~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
outAttrs.actionId=EditorInfo.IME_ACTION_DONE;
outAttrs.actionLabel=getContext().getString(R.string.done);
return connection;
}
private void handleDone() {
if (mComposingDisplayText.length() > 0) {
// Finish current composing
mComposingText = "";
postInputCommand(() -> {
displayComposingText(StringUtils.removeSpaces(mComposingDisplayText), ComposingAction.FINISH);
postUICommand(this::updateCandidates);
});
return;
}
final InputConnection connection = mInputConnection;
final int action = mEditorInfo.imeOptions & EditorInfo.IME_MASK_ACTION;
postInputCommand(() -> connection.performEditorAction(action));
boolean hide = (action == EditorInfo.IME_ACTION_DONE) || (action == EditorInfo.IME_ACTION_GO) ||
(action == EditorInfo.IME_ACTION_SEARCH) || (action == EditorInfo.IME_ACTION_SEND);
if (hide && mFocusedView != null) {
mFocusedView.clearFocus();
}
}
@Override
public String getEnterKeyText(int aIMEOptions, String aComposingText) {
Locale locale = getLocale();
switch (aIMEOptions & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
case EditorInfo.IME_ACTION_GO:
return StringUtils.getStringByLocale(mContext, R.string.keyboard_go_label, locale);
case EditorInfo.IME_ACTION_NEXT:
return StringUtils.getStringByLocale(mContext, R.string.keyboard_next_label, locale);
case EditorInfo.IME_ACTION_SEARCH:
return StringUtils.getStringByLocale(mContext, R.string.keyboard_search_label, locale);
case EditorInfo.IME_ACTION_SEND:
return StringUtils.getStringByLocale(mContext, R.string.keyboard_send_label, locale);
default:
return StringUtils.getStringByLocale(mContext, R.string.keyboard_enter_label, locale);
}
}
public static String imeActionName(final int imeOptions) {
final int actionId = imeOptions & EditorInfo.IME_MASK_ACTION;
switch (actionId) {
case EditorInfo.IME_ACTION_UNSPECIFIED:
return "actionUnspecified";
case EditorInfo.IME_ACTION_NONE:
return "actionNone";
case EditorInfo.IME_ACTION_GO:
return "actionGo";
case EditorInfo.IME_ACTION_SEARCH:
return "actionSearch";
case EditorInfo.IME_ACTION_SEND:
return "actionSend";
case EditorInfo.IME_ACTION_NEXT:
return "actionNext";
case EditorInfo.IME_ACTION_DONE:
return "actionDone";
case EditorInfo.IME_ACTION_PREVIOUS:
return "actionPrevious";
default:
return "actionUnknown(" + actionId + ")";
}
}
@Subscriber(value = @Param(value = IME_SEARCH_MESSAGE, sticky = false), thread = RunningThread.MAIN_THREAD)
public boolean inputSearchText(String text) {
if (text != null) {
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.commitText(text, 1);
// 需要额外点击发送
EditorInfo editorInfo = getCurrentInputEditorInfo();
if (editorInfo != null) {
int options = editorInfo.imeOptions;
final int actionId = options & EditorInfo.IME_MASK_ACTION;
switch (actionId) {
case EditorInfo.IME_ACTION_SEARCH:
sendDefaultEditorAction(true);
break;
case EditorInfo.IME_ACTION_GO:
sendDefaultEditorAction(true);
break;
case EditorInfo.IME_ACTION_SEND:
sendDefaultEditorAction(true);
break;
default:
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
}
}
return true;
}
}
return false;
}
protected void imeOptionsChanged(int imeOptions) {
final int relevantImeOptions = imeOptions & EditorInfo.IME_MASK_ACTION;
if (BaldInputMethodService.defaultEditorActionExists(imeOptions)) {
switch (relevantImeOptions) {
case IME_ACTION_SEARCH:
tv_enter.setText(R.string.search);
iv_enter.setImageResource(R.drawable.search_on_button);
break;
case IME_ACTION_DONE:
tv_enter.setText(R.string.done);
iv_enter.setImageResource(R.drawable.check_on_button);
break;
case IME_ACTION_GO:
tv_enter.setText(R.string.go);
iv_enter.setImageResource(R.drawable.check_on_button);
break;
case IME_ACTION_SEND:
tv_enter.setText(R.string.send);
iv_enter.setImageResource(R.drawable.send_on_button);
break;
case IME_ACTION_NEXT:
tv_enter.setText(R.string.next);
iv_enter.setImageResource(R.drawable.arrow_end_on_background);
break;
//should never be here
case IME_ACTION_NONE:
case IME_ACTION_UNSPECIFIED:
default:
tv_enter.setText(R.string.enter);
iv_enter.setImageResource(R.drawable.enter_on_keyboard);
break;
}
} else {
tv_enter.setText(R.string.enter);
iv_enter.setImageResource(R.drawable.enter_on_keyboard);
}
}
/**
* This looks at the ime options given by the current editor, to set the
* appropriate label on the keyboard's enter key (if it has one).
*/
void setImeOptions(Resources res, int options) {
if (mEnterKey == null) {
return;
}
int valnorm = KeyEvent.KEYCODE_ENTER;
switch (options & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
case EditorInfo.IME_ACTION_GO:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.codes = NORMAL_ENTER;
mEnterKey.label = res.getText(R.string.label_go_key);
break;
case EditorInfo.IME_ACTION_NEXT:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.codes = NORMAL_ENTER;
mEnterKey.label = res.getText(R.string.label_next_key);
break;
case EditorInfo.IME_ACTION_SEARCH:
mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search);
mEnterKey.codes = NORMAL_ENTER;
mEnterKey.label = null;
break;
case EditorInfo.IME_ACTION_SEND:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.codes = NORMAL_ENTER;
mEnterKey.label = res.getText(R.string.label_send_key);
break;
default:
mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_return);
mEnterKey.label = null;
mEnterKey.codes = TERMINAL_ENTER;
break;
}
}
public static int getImeOptionsActionIdFromEditorInfo(final EditorInfo editorInfo) {
if ((editorInfo.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
return EditorInfo.IME_ACTION_NONE;
} else if (editorInfo.actionLabel != null) {
return IME_ACTION_CUSTOM_LABEL;
} else {
// Note: this is different from editorInfo.actionId, hence "ImeOptionsActionId"
return editorInfo.imeOptions & EditorInfo.IME_MASK_ACTION;
}
}
public void setImeOptions(Resources res, int options) {
if (mEnterKey == null) {
return;
}
switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
case EditorInfo.IME_ACTION_GO:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_keyboard_key_go);
break;
case EditorInfo.IME_ACTION_NEXT:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_keyboard_key_next);
break;
case EditorInfo.IME_ACTION_SEARCH:
mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search);
mEnterKey.label = null;
break;
case EditorInfo.IME_ACTION_SEND:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_keyboard_key_send);
break;
default:
mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_return);
mEnterKey.label = null;
break;
}
}
public static int getImeOptionsActionIdFromEditorInfo(final EditorInfo editorInfo) {
if ((editorInfo.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
return EditorInfo.IME_ACTION_NONE;
} else if (editorInfo.actionLabel != null) {
return IME_ACTION_CUSTOM_LABEL;
} else {
// Note: this is different from editorInfo.actionId, hence "ImeOptionsActionId"
return editorInfo.imeOptions & EditorInfo.IME_MASK_ACTION;
}
}
/**
* This looks at the ime options given by the current editor, to set the
* appropriate label on the keyboard's enter key (if it has one).
*/
void setImeOptions(Resources res, int options) {
if (mEnterKey == null) {
return;
}
switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
case EditorInfo.IME_ACTION_GO:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_go_key);
break;
case EditorInfo.IME_ACTION_NEXT:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_next_key);
break;
case EditorInfo.IME_ACTION_SEARCH:
mEnterKey.icon = res.getDrawable(R.drawable.ic_search_45dp);
mEnterKey.label = null;
break;
case EditorInfo.IME_ACTION_SEND:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_send_key);
break;
default:
mEnterKey.icon = res.getDrawable(R.drawable.ic_check_circle_45dp);
mEnterKey.label = null;
break;
}
}
void setImeOptions(Resources res, int mode, int options) {
mMode = mode;
// TODO should clean up this method
if (mEnterKey != null) {
// Reset some of the rarely used attributes.
mEnterKey.popupCharacters = null;
mEnterKey.popupResId = 0;
mEnterKey.text = null;
switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
case EditorInfo.IME_ACTION_GO:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_go_key);
break;
case EditorInfo.IME_ACTION_NEXT:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_next_key);
break;
case EditorInfo.IME_ACTION_DONE:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_done_key);
break;
case EditorInfo.IME_ACTION_SEARCH:
mEnterKey.iconPreview = res.getDrawable(
R.drawable.sym_keyboard_feedback_search);
mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search);
mEnterKey.label = null;
break;
case EditorInfo.IME_ACTION_SEND:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_send_key);
break;
default:
// Keep Return key in IM mode, we have a dedicated smiley key.
mEnterKey.iconPreview = res.getDrawable(
R.drawable.sym_keyboard_feedback_return);
mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_return);
mEnterKey.label = null;
break;
}
// Set the initial size of the preview icon
if (mEnterKey.iconPreview != null) {
setDefaultBounds(mEnterKey.iconPreview);
}
}
}