美文网首页
Android 输入法如何获取EditText的inputTyp

Android 输入法如何获取EditText的inputTyp

作者: 败家的猫 | 来源:发表于2019-11-24 13:05 被阅读0次

    inputType属性的作用在于给输入法足够的上下文
    Properly set the {@link android.R.attr#inputType} in your editable
    text views, so that the input method will have enough context to help the
    user in entering text into them.

    android.widget.TextView.java
    public TextView(
    Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    if (mEditor != null) {
    //实际是mEditor
    mEditor.adjustInputType(password, passwordInputType, webPasswordInputType,
    numberPasswordInputType);
    }
    }

    android.widget.Editor.java
    Helper class used by TextView to handle editable text views.

    InputContentType mInputContentType;

    静态内部类
    static class InputContentType {
    int imeOptions = EditorInfo.IME_NULL;
    String privateImeOptions;
    CharSequence imeActionLabel;
    int imeActionId;
    Bundle extras;
    OnEditorActionListener onEditorActionListener;
    boolean enterDown;
    LocaleList imeHintLocales;
    }

    android.view.inputmethod.EditorInfo.java
    public class EditorInfo implements InputType, Parcelable {

    /**
     * The content type of the text box, whose bits are defined by
     * {@link InputType}.
     *
     * @see InputType
     * @see #TYPE_MASK_CLASS
     * @see #TYPE_MASK_VARIATION
     * @see #TYPE_MASK_FLAGS
     */
    public int inputType = TYPE_NULL;
    

    }

    android.text.InputType.java

    /**

    • Masks for {@link inputType}
    • <pre>
    • |-------|-------|-------|-------|
    •                          1111 TYPE_MASK_CLASS
      
    •                  11111111     TYPE_MASK_VARIATION
      
    •      111111111111             TYPE_MASK_FLAGS
      
    • |-------|-------|-------|-------|
    •                               TYPE_NULL
      
    • |-------|-------|-------|-------|
    •                             1 TYPE_CLASS_TEXT
      
    •                         1     TYPE_TEXT_VARIATION_URI
      
    •                        1      TYPE_TEXT_VARIATION_EMAIL_ADDRESS
      
    •                        11     TYPE_TEXT_VARIATION_EMAIL_SUBJECT
      
    •                       1       TYPE_TEXT_VARIATION_SHORT_MESSAGE
      
    •                       1 1     TYPE_TEXT_VARIATION_LONG_MESSAGE
      
    •                       11      TYPE_TEXT_VARIATION_PERSON_NAME
      
    •                       111     TYPE_TEXT_VARIATION_POSTAL_ADDRESS
      
    •                      1        TYPE_TEXT_VARIATION_PASSWORD
      
    •                      1  1     TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
      
    •                      1 1      TYPE_TEXT_VARIATION_WEB_EDIT_TEXT
      
    •                      1 11     TYPE_TEXT_VARIATION_FILTER
      
    •                      11       TYPE_TEXT_VARIATION_PHONETIC
      
    •                      11 1     TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS
      
    •                      111      TYPE_TEXT_VARIATION_WEB_PASSWORD
      
    •                 1             TYPE_TEXT_FLAG_CAP_CHARACTERS
      
    •                1              TYPE_TEXT_FLAG_CAP_WORDS
      
    •               1               TYPE_TEXT_FLAG_CAP_SENTENCES
      
    •              1                TYPE_TEXT_FLAG_AUTO_CORRECT
      
    •             1                 TYPE_TEXT_FLAG_AUTO_COMPLETE
      
    •            1                  TYPE_TEXT_FLAG_MULTI_LINE
      
    •           1                   TYPE_TEXT_FLAG_IME_MULTI_LINE
      
    •          1                    TYPE_TEXT_FLAG_NO_SUGGESTIONS
      
    • |-------|-------|-------|-------|
    •                            1  TYPE_CLASS_NUMBER
      
    •                         1     TYPE_NUMBER_VARIATION_PASSWORD
      
    •                 1             TYPE_NUMBER_FLAG_SIGNED
      
    •                1              TYPE_NUMBER_FLAG_DECIMAL
      
    • |-------|-------|-------|-------|
    •                            11 TYPE_CLASS_PHONE
      
    • |-------|-------|-------|-------|
    •                           1   TYPE_CLASS_DATETIME
      
    •                         1     TYPE_DATETIME_VARIATION_DATE
      
    •                        1      TYPE_DATETIME_VARIATION_TIME
      
    • |-------|-------|-------|-------|</pre>
      */

    /**

    • Mask of bits that determine the overall class
    • of text being given. Currently supported classes are:
    • {@link #TYPE_CLASS_TEXT}, {@link #TYPE_CLASS_NUMBER},
    • {@link #TYPE_CLASS_PHONE}, {@link #TYPE_CLASS_DATETIME}.
    • <p>IME authors: If the class is not one you
    • understand, assume {@link #TYPE_CLASS_TEXT} with NO variation
    • or flags.<p>
      */
      public static final int TYPE_MASK_CLASS = 0x0000000f;

    /**

    • Mask of bits that determine the variation of
    • the base content class.
      */
      public static final int TYPE_MASK_VARIATION = 0x00000ff0;

    /**

    • Mask of bits that provide addition bit flags
    • of options.
      */
      public static final int TYPE_MASK_FLAGS = 0x00fff000;

    android.inputmethodservice.InputMethodService.java

    /**

    • {@inheritDoc}
      */
      @MainThread
      @Override
      public void startInput(InputConnection ic, EditorInfo attribute) {
      if (DEBUG) Log.v(TAG, "startInput(): editor=" + attribute);
      doStartInput(ic, attribute, false);
      }

    /**

    • {@inheritDoc}
      */
      @MainThread
      @Override
      public void restartInput(InputConnection ic, EditorInfo attribute) {
      if (DEBUG) Log.v(TAG, "restartInput(): editor=" + attribute);
      doStartInput(ic, attribute, true);
      }

    void doStartInput(InputConnection ic, EditorInfo attribute, boolean restarting) {
    if (!restarting) {
    doFinishInput();
    }
    mInputStarted = true;
    mStartedInputConnection = ic;
    mInputEditorInfo = attribute;
    initialize();
    if (DEBUG) Log.v(TAG, "CALL: onStartInput");
    onStartInput(attribute, restarting);
    if (mWindowVisible) {
    if (mShowInputRequested) {
    if (DEBUG) Log.v(TAG, "CALL: onStartInputView");
    mInputViewStarted = true;
    onStartInputView(mInputEditorInfo, restarting);
    startExtractingText(true);
    } else if (mCandidatesVisibility == View.VISIBLE) {
    if (DEBUG) Log.v(TAG, "CALL: onStartCandidatesView");
    mCandidatesViewStarted = true;
    onStartCandidatesView(mInputEditorInfo, restarting);
    }
    }
    }

    /**

    • Called to inform the input method that text input has started in an
    • editor. You should use this callback to initialize the state of your
    • input to match the state of the editor given to it.
    • @param attribute The attributes of the editor that input is starting
    • in.
    • @param restarting Set to true if input is restarting in the same
    • editor such as because the application has changed the text in
    • the editor. Otherwise will be false, indicating this is a new
    • session with the editor.
      */
      public void onStartInput(EditorInfo attribute, boolean restarting) {
      // Intentionally empty
      }
      到onStartInput这里,输入法就可以从EditorInfo拿到inputType的值了

    相关文章

      网友评论

          本文标题:Android 输入法如何获取EditText的inputTyp

          本文链接:https://www.haomeiwen.com/subject/sgyaictx.html