2015-10-13 87 views
2

按照此示例 https://android.googlesource.com/platform/development/+/master/samples/SoftKeyboard/ ,我有我自己的软键盘。 我想修改KEYCODE_ENTER行为。Android自定义软键盘 - 更改KEYCODE_ENTER行为以跳过非默认软键盘行为的非EditText对象

例子:我有活性的,其布局layout_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 >> FAIL 

任何一个有Android默认软键盘的源代码? (与数字文本键盘和全文键盘布局)

任何建议可能会有所帮助,非常感谢你。

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(); 
     } 
    } 

回答

1

我的输入键盘服务(​​不活动)代码:

@Override public void onStartInput(EditorInfo attribute, boolean restarting) { 
    super.onStartInput(attribute, restarting); 

    ... 

    if ((getCurrentInputConnection() != null) && (!isEditText)) 
     getCurrentInputConnection().performEditorAction(EditorInfo.IME_ACTION_NEXT); 
    } 
} 

不过,我建议不要把键码10 \你的键盘,但使用代码-4 IME ActionDone n个输入。在我的示例中,它使得应用程序更好地工作,因为它让应用程序进行焦点选择,它将跳过非对象文本并正确清除焦点。

1

要修改软键盘上的输入行为,您将不得不创建自定义EditText类并重写onCreateInputConnections。看到我的下面的例子,当用户在这个特定的EditText上时,我总是显示'Done'而不是next。

@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; 
    } 

您还可以设置哪些EditTexts应该

android:nextFocusDown="" 

传递您接下来要集中在EditText上的ID为下在你的布局获得焦点。

+0

这不是他要求的... – 2015-10-13 15:45:31

+0

感谢您的解决方案,但我正在使用键盘服务,它不绑定到特定的活动。我不知道Android如何默认软键盘工作?它如何跳过非编辑文本对象并将焦点集中到下一个编辑文本? –

+0

默认情况下应该这样做。哪个视图会去?或者它只是崩溃? – vguzzi

1

使用android:nextFocusDown参数指定当用户点击回车键时您想要获得焦点的字段。

+0

谢谢,但我在布局中有很多像这样的东西,所以我正在寻找输入键盘服务(​​如默认键盘)的解决方案。 –

+0

那么你在XML中只定义它一次,它是非常自动的。 – 2015-10-13 15:53:16

+0

您也可以使复选框不可聚焦。 – 2015-10-13 15:54:21