Android自定义软键盘-更改KEYCODE\u ENTER行为以跳过非EditText对象,如默认软键盘行为
遵循此示例 ,我有自己的软键盘。 我想修改KEYCODE\u ENTER行为 示例:我的活动A及其布局为: EditText edt_1+复选框chb_1+EditText edt_2 Android默认软键盘的行为:Android自定义软键盘-更改KEYCODE\u ENTER行为以跳过非EditText对象,如默认软键盘行为,android,android-edittext,android-softkeyboard,Android,Android Edittext,Android Softkeyboard,遵循此示例 ,我有自己的软键盘。 我想修改KEYCODE\u ENTER行为 示例:我的活动A及其布局为: EditText edt_1+复选框chb_1+EditText edt_2 Android默认软键盘的行为: focus edt_1 > KEYCODE_ENTER > skip chb_1 > focus edt_2 >> what I want focus edt_1 > KEYCODE_ENTER > focus chb_1 >&g
focus edt_1 > KEYCODE_ENTER > skip chb_1 > focus edt_2 >> what I want
focus edt_1 > KEYCODE_ENTER > focus chb_1 >> FAIL
我的软键盘:
focus edt_1 > KEYCODE_ENTER > skip chb_1 > focus edt_2 >> what I want
focus edt_1 > KEYCODE_ENTER > focus chb_1 >> FAIL
谁有安卓默认软键盘的源代码?(带数字文本键盘和全文键盘布局)
任何建议都可能有用,非常感谢
public class SoftKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener
@Override public void onStartInput(EditorInfo attribute, boolean restarting) {
super.onStartInput(attribute, restarting);
// Reset our state. We want to do this even if restarting, because
// the underlying state of the text editor could have changed in any way.
mComposing.setLength(0);
updateCandidates();
if (!restarting) {
// Clear shift states.
mMetaState = 0;
}
boolean isEditText = true;
mPredictionOn = false;
mCompletionOn = false;
mCompletions = null;
//TODO: setup own behavior
// We are now going to initialize our state based on the type of
// text being edited.
switch (attribute.inputType & InputType.TYPE_MASK_CLASS) {
case InputType.TYPE_CLASS_NUMBER:
mCurKeyboard = mNumberKeyboard;
break;
case InputType.TYPE_CLASS_DATETIME:
// Numbers and dates default to the symbols keyboard, with
// no extra features.
mCurKeyboard = mSymbolsKeyboard;
break;
case InputType.TYPE_CLASS_PHONE:
// Phones will also default to the symbols keyboard, though
// often you will want to have a dedicated phone keyboard.
mCurKeyboard = mSymbolsKeyboard;
break;
case InputType.TYPE_CLASS_TEXT:
// This is general text editing. We will default to the
// normal alphabetic keyboard, and assume that we should
// be doing predictive text (showing candidates as the
// user types).
mCurKeyboard = mQwertyKeyboard;
mPredictionOn = true;
// We now look for a few special variations of text that will
// modify our behavior.
int variation = attribute.inputType & InputType.TYPE_MASK_VARIATION;
if (variation == InputType.TYPE_TEXT_VARIATION_PASSWORD ||
variation == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
// Do not display predictions / what the user is typing
// when they are entering a password.
mPredictionOn = false;
}
if (variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
|| variation == InputType.TYPE_TEXT_VARIATION_URI
|| variation == InputType.TYPE_TEXT_VARIATION_FILTER) {
// Our predictions are not useful for e-mail addresses
// or URIs.
mPredictionOn = false;
}
if ((attribute.inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE) != 0) {
// If this is an auto-complete text view, then our predictions
// will not be shown and instead we will allow the editor
// to supply their own. We only show the editor's
// candidates when in fullscreen mode, otherwise relying
// own it displaying its own UI.
mPredictionOn = false;
mCompletionOn = isFullscreenMode();
}
// We also want to look at the current state of the editor
// to decide whether our alphabetic keyboard should start out
// shifted.
updateShiftKeyState(attribute);
break;
default:
// For all unknown input types, default to the alphabetic
// keyboard with no special features.
mCurKeyboard = mNonEditTextKeyboard;
updateShiftKeyState(attribute);
isEditText = false;
}
// Update the label on the enter key, depending on what the application
// says it will do.
mCurKeyboard.setImeOptions(getResources(), attribute.imeOptions);
if ((mInputView!= null) && (!isEditText)) {
//TODO: handle non edit text case.
handleClose();
}
}
要修改软键盘上的enter行为,必须创建自定义EditText类并重写onCreateInputConnections。请参阅下面的示例,其中当用户使用此特定文本时,我总是显示“完成”而不是“下一步”
@Override
public InputConnection onCreateInputConnection(@NonNull 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;
}
您还可以使用设置在布局中下一步应获得焦点的编辑文本
android:nextFocusDown=""
输入下一个要聚焦的编辑文本的id。使用android:nextFocusDown参数指定当用户按enter键时要聚焦的字段。输入键盘上的我的代码服务(非活动):
但是,我建议不要在键盘上输入keycode 10\n Enter,而是使用code-4 IME ActionDone。在我的示例中,它工作得更好,因为它允许应用程序选择焦点,它将跳过非对象文本并正确清除焦点。感谢您的解决方案,但我正在使用键盘服务,它不会绑定到特定的活动。我想知道Android默认软键盘是如何工作的?它如何跳过非编辑文本对象并聚焦到下一个编辑文本?默认情况下应该这样做。它要看哪一种景色?或者它只是崩溃了?啊,我看到了复选框。。您可以在不同的对象上设置焦点。例如,如果不希望单选按钮可聚焦,则可以在其上设置可聚焦(false)。但是nextFocusDown应该做到这一点吗?在复选框对象上的android:focusableInTouchMode=“false”问题是:它总是关注下一个对象:复选框,按钮。。。所以我不想设置所有这些对象。我们可以做一些类似安卓默认软键盘的事情吗?android ASOP如何跳过非编辑文本对象?谢谢,但我的版面中有很多类似的东西,所以我正在寻找输入键盘服务(如默认键盘)的解决方案。您只需在XML中定义一次,它是相当自动的。您还可以使复选框不可聚焦。复选框只是一个示例,我的键盘专注于其他对象,如按钮、图像等。。。太:(每次我按enter键,它都会聚焦到下一个对象(目前,我无法列出所有类型,但至少它聚焦于复选框和按钮)