android 9 启动登录界面编辑框有焦点无弹出全键盘

问题描述

启动登录界面编辑框有焦点无弹出全键盘

问题分析

通过dumpsys input_methodke可以看到当前编辑框确实是有焦点的。

查看代码

    override fun onResume() {
        super.onResume()
        if (mEditText?.requestFocus() == true) {
            val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.showSoftInput(mEditText, InputMethodManager.SHOW_FORCED)
        }
    }

在onResume时requestFocus如果拿到焦点,获取弹出全键盘。

 <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:windowSoftInputMode="stateVisible|adjustResize"
            android:theme="@style/Theme.MalsDemo.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

在AndroidMainfest.xml中也有声明android:windowSoftInputMode ,可就是弹不出来。

看log。

把input相关的log都打开(这个打开需要源码支持。)

2022-04-08 11:21:39.260 4369-4369/com.android.launcher3 V/InputMethodManager: focusOut: view=com.android.launcher3.allapps.AllAppsRecyclerView{41d7cd9 VFED..... .F...... 0,32-1920,1080 #7f0a0016 app:id/apps_list_view},focus=true,windowFocus=false,autofillUiShowing=false,window=android.view.ViewRootImpl$W@bc40603,temporaryDetach=false mServedView=com.android.launcher3.allapps.AllAppsRecyclerView{41d7cd9 VFED..... .F...... 0,32-1920,1080 #7f0a0016 app:id/apps_list_view},focus=true,windowFocus=false,autofillUiShowing=false,window=android.view.ViewRootImpl$W@bc40603,temporaryDetach=false
2022-04-08 11:21:39.265 2071-2087/system_process D/InputMethodManagerService: --- calledFromForegroundUserOrSystemProcess ? calling uid = 10032 system uid = 1000 calling userId = 0, foreground user id = 0, calling pid = 12420com.android.server.InputMethodManagerService.showSoftInput(InputMethodManagerService.java:2585)
2022-04-08 11:21:39.265 2071-2087/system_process W/InputMethodManagerService: Ignoring showSoftInput of uid 10032: com.android.internal.view.IInputMethodClient$Stub$Proxy@b46356
2022-04-08 11:21:39.289 12420-12420/com.example.teamsdemo V/InputMethodManager: focusIn: androidx.appcompat.widget.AppCompatEditText{381cff VFED..CL. .F..H.ID 0,303-1920,363 #7f0801c8 app:id/textview_first},focus=true,windowFocus=true,autofillUiShowing=false,window=android.view.ViewRootImpl$W@f44b072,temporaryDetach=false
2022-04-08 11:21:39.292 12420-12420/com.example.teamsdemo V/InputMethodManager: onWindowFocus: androidx.appcompat.widget.AppCompatEditText{381cff VFED..CL. .F..H.ID 0,303-1920,363 #7f0801c8 app:id/textview_first} softInputMode=STATE_ALWAYS_VISIBLE|ADJUST_RESIZE|IS_FORWARD_NAVIGATION first=true flags=#81810100
2022-04-08 11:21:39.292 12420-12420/com.example.teamsdemo V/InputMethodManager: Restarting due to mRestartOnNextWindowFocus
2022-04-08 11:21:39.293 12420-12420/com.example.teamsdemo V/InputMethodManager: focusIn: androidx.appcompat.widget.AppCompatEditText{381cff VFED..CL. .F..H.ID 0,303-1920,363 #7f0801c8 app:id/textview_first},focus=true,windowFocus=true,autofillUiShowing=false,window=android.view.ViewRootImpl$W@f44b072,temporaryDetach=false
2022-04-08 11:21:39.293 12420-12420/com.example.teamsdemo V/InputMethodManager: checkFocus: view=androidx.appcompat.widget.AppCompatEditText{381cff VFED..CL. .F..H.ID 0,303-1920,363 #7f0801c8 app:id/textview_first} next=androidx.appcompat.widget.AppCompatEditText{381cff VFED..CL. .F..H.ID 0,303-1920,363 #7f0801c8 app:id/textview_first} forceNewFocus=true package=com.example.teamsdemo
2022-04-08 11:21:39.293 12420-12420/com.example.teamsdemo V/InputMethodManager: Starting input: view=androidx.appcompat.widget.AppCompatEditText{381cff VFED..CL. .F..H.ID 0,303-1920,363 #7f0801c8 app:id/textview_first},focus=true,windowFocus=true,autofillUiShowing=false,window=android.view.ViewRootImpl$W@f44b072,temporaryDetach=false reason=WINDOW_FOCUS_GAIN
2022-04-08 11:21:39.300 12420-12420/com.example.teamsdemo V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@e0d4fd2 ic=androidx.emoji2.viewsintegration.EmojiInputConnection@86928a3
2022-04-08 11:21:39.300 12420-12420/com.example.teamsdemo V/InputMethodManager: START INPUT: view=androidx.appcompat.widget.AppCompatEditText{381cff VFED..CL. .F..H.ID 0,303-1920,363 #7f0801c8 app:id/textview_first},focus=true,windowFocus=true,autofillUiShowing=false,window=android.view.ViewRootImpl$W@f44b072,temporaryDetach=false ic=androidx.emoji2.viewsintegration.EmojiInputConnection@86928a3 tba=android.view.inputmethod.EditorInfo@e0d4fd2 controlFlags=#107
2022-04-08 11:21:39.302 2071-3080/system_process D/InputMethodManagerService: --- calledFromForegroundUserOrSystemProcess ? calling uid = 10032 system uid = 1000 calling userId = 0, foreground user id = 0, calling pid = 12420com.android.server.InputMethodManagerService.startInputOrWindowGainedFocus(InputMethodManagerService.java:2754)
2022-04-08 11:21:39.302 2071-3080/system_process V/InputMethodManagerService: windowGainedFocus: reason=WINDOW_FOCUS_GAIN client=android.os.BinderProxy@497f086 inputContext=com.android.internal.view.IInputContext$Stub$Proxy@d96afd7 missingMethods= attribute=android.view.inputmethod.EditorInfo@a0690c4 controlFlags=#107 softInputMode=STATE_ALWAYS_VISIBLE|ADJUST_RESIZE|IS_FORWARD_NAVIGATION windowFlags=#81810100 unverifiedTargetSdkVersion=32
2022-04-08 11:21:39.302 2071-3080/system_process V/InputMethodManagerService: Window asks to always show input
2022-04-08 11:21:39.302 2071-3080/system_process V/InputMethodManagerService: unbindCurrentInputLocked: client=android.os.BinderProxy@38c5d29
2022-04-08 11:21:39.302 2071-3080/system_process V/InputMethodManagerService: Hide switching menu
2022-04-08 11:21:39.302 2071-3080/system_process V/InputMethodManagerService: switching to client: client=android.os.BinderProxy@497f086 keyguard=false
2022-04-08 11:21:39.303 4369-4369/com.android.launcher3 I/InputMethodManager: handleMessage: MSG_SET_ACTIVE false, was true
2022-04-08 11:21:39.303 2071-3080/system_process V/InputMethodManagerService: Disabling: SessionState{uid 10009 pid 4369 method 897a721 session 4e829fb channel ClientState{411b9e3 uid 10009 pid 4369} (server)}
2022-04-08 11:21:39.303 2071-3080/system_process V/InputMethodManagerService: Enabling: SessionState{uid 10032 pid 12420 method 897a721 session e838736 channel ClientState{28cee6a uid 10032 pid 12420} (server)}
2022-04-08 11:21:39.303 4369-4369/com.android.launcher3 I/InputMethodManager: handleMessage: MSG_UNBIND 60 reason=SWITCH_CLIENT
2022-04-08 11:21:39.303 4369-4369/com.android.launcher3 V/InputMethodManager: Clearing binding!
2022-04-08 11:21:39.303 2071-3080/system_process D/InputMethodManagerService: showCurrentInputLocked: mCurToken=android.os.Binder@14f2b7b
2022-04-08 11:21:39.303 2071-3080/system_process V/InputMethodManagerService: Calling com.android.internal.view.IInputMethod$Stub$Proxy@897a721.showSoftInput(0, null)
2022-04-08 11:21:39.303 4714-4714/com.test.tvime V/InputMethodService: unbindInput(): binding=InputBinding{android.os.BinderProxy@86e0336 / uid 10009 / pid 4369} ic=InputConnectionWrapper{idHash=#7b6430e mMissingMethods=}
2022-04-08 11:21:39.306 4714-4714/com.test.tvime V/InputMethodService: bindInput(): binding=InputBinding{android.os.BinderProxy@cd0033c / uid 10032 / pid 12420} ic=InputConnectionWrapper{idHash=#1f5d8c5 mMissingMethods=}
2022-04-08 11:21:39.307 2071-2087/system_process D/InputMethodManagerService: --- calledFromForegroundUserOrSystemProcess ? calling uid = 10007 system uid = 1000 calling userId = 0, foreground user id = 0, calling pid = 4714com.android.server.InputMethodManagerService.reportFullscreenMode(InputMethodManagerService.java:4616)
2022-04-08 11:21:39.314 4714-4714/com.test.tvime V/InputMethodService: startInput(): editor=android.view.inputmethod.EditorInfo@b17821a
2022-04-08 11:21:39.314 12420-12420/com.example.teamsdemo V/InputMethodManager: Starting input: Bind result=InputBindResult{result=SUCCESS_WITH_IME_SESSION method=com.android.internal.view.IInputMethodSession$Stub$Proxy@3a27a0 id=com.test.tvime/com.test.ime.softkeyboard.IME sequence=61 userActionNotificationSequenceNumber=3}
2022-04-08 11:21:39.316 4714-4714/com.test.tvime V/InputMethodService: CALL: onFinishInput
2022-04-08 11:21:39.316 4714-4714/com.test.tvime V/InputMethodService: CALL: onStartInput
2022-04-08 11:21:39.316 4714-4714/com.test.tvime V/InputMethodService: showSoftInput()
2022-04-08 11:21:39.316 4714-4714/com.test.tvime V/InputMethodService: clearInsetOfPreviousIme()  mShouldClearInsetOfPreviousIme=false
2022-04-08 11:21:39.339 12420-12420/com.example.teamsdemo I/InputMethodManager: handleMessage: MSG_SET_ACTIVE true, was false
2022-04-08 11:21:39.757 4369-4369/com.android.launcher3 V/InputMethodManager: onViewDetachedFromWindow: view=android.view.View{436647f V.ED..... ......ID 385,70-473,158},focus=false,windowFocus=false,autofillUiShowing=false,window=android.view.ViewRootImpl$W@bc40603,temporaryDetach=false mServedView=com.android.launcher3.allapps.AllAppsRecyclerView{41d7cd9 VFED..... .F....ID 0,32-1920,1080 #7f0a0016 app:id/apps_list_view},focus=true,windowFocus=false,autofillUiShowing=false,window=android.view.ViewRootImpl$W@bc40603,temporaryDetach=false

这边关键的log在这里,都走到InputMethodService的showSoftInput,还没显示全键盘。

2022-04-08 11:21:39.316 4714-4714/com.test.tvime V/InputMethodService: showSoftInput()

看源码

//InputMethodService#showSoftInput ,传入参数(0,null)

public void showSoftInput(int flags, ResultReceiver resultReceiver) {
            if (DEBUG) Log.v(TAG, "showSoftInput()");
            boolean wasVis = isInputViewShown();
            if (dispatchOnShowInputRequested(flags, false)) {
                try {
                    showWindow(true);
                } catch (BadTokenException e) {
                    // We have ignored BadTokenException here since Jelly Bean MR-2 (API Level 18).
                    // We could ignore BadTokenException in InputMethodService#showWindow() instead,
                    // but it may break assumptions for those who override #showWindow() that we can
                    // detect errors in #showWindow() by checking BadTokenException.
                    // TODO: Investigate its feasibility.  Update JavaDoc of #showWindow() of
                    // whether it's OK to override #showWindow() or not.
                }
            }
            clearInsetOfPreviousIme();
            // If user uses hard keyboard, IME button should always be shown.
            mImm.setImeWindowStatus(mToken, mStartInputToken,
                    mapToImeWindowStatus(isInputViewShown()), mBackDisposition);
            if (resultReceiver != null) {
                resultReceiver.send(wasVis != isInputViewShown()
                        ? InputMethodManager.RESULT_SHOWN
                        : (wasVis ? InputMethodManager.RESULT_UNCHANGED_SHOWN
                                : InputMethodManager.RESULT_UNCHANGED_HIDDEN), null);
            }
        }

//InputMethodService#dispatchOnShowInputRequested ,传入参数(0,false)

private boolean dispatchOnShowInputRequested(int flags, boolean configChange) {
    final boolean result = onShowInputRequested(flags, configChange);
    if (result) {
        mShowInputFlags = flags;
    } else {
        mShowInputFlags = 0;
    }
    return result;
}

//InputMethodService#onShowInputRequested ,传入参数(0,false)

public boolean onShowInputRequested(int flags, boolean configChange) {
    if (!onEvaluateInputViewShown()) {
        return false;
    }
    if ((flags&InputMethod.SHOW_EXPLICIT) == 0) {//true
        if (!configChange && onEvaluateFullscreenMode()) {true && false
            // Don't show if this is not explicitly requested by the user and
            // the input method is fullscreen.  That would be too disruptive.
            // However, we skip this change for a config change, since if
            // the IME is already shown we do want to go into fullscreen
            // mode at this point.
            return false;
        }
        if (!mSettingsObserver.shouldShowImeWithHardKeyboard() && //true && true
                getResources().getConfiguration().keyboard != Configuration.KEYBOARD_NOKEYS) {
            // And if the device has a hard keyboard, even if it is
            // currently hidden, don't show the input method implicitly.
            // These kinds of devices don't need it that much.
            return false;
        }
    }
    return true;
}

//InputMethodService#onEvaluateInputViewShown

public boolean onEvaluateInputViewShown() {
    if (mSettingsObserver == null) {
        Log.w(TAG, "onEvaluateInputViewShown: mSettingsObserver must not be null here.");
        return false;
    }
    if (mSettingsObserver.shouldShowImeWithHardKeyboard()) {
        return true;
    }
    Configuration config = getResources().getConfiguration();
    return config.keyboard == Configuration.KEYBOARD_NOKEYS
            || config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES;
}

SettingsObserver#shouldShowImeWithHardKeyboard()

private boolean shouldShowImeWithHardKeyboard() {
    // Lazily initialize as needed.
    if (mShowImeWithHardKeyboard == ShowImeWithHardKeyboardType.UNKNOWN) {
        mShowImeWithHardKeyboard = Settings.Secure.getInt(mService.getContentResolver(),
                Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD, 0) != 0 ?
                ShowImeWithHardKeyboardType.TRUE : ShowImeWithHardKeyboardType.FALSE;
    }
    switch (mShowImeWithHardKeyboard) {
        case ShowImeWithHardKeyboardType.TRUE:
            return true;
        case ShowImeWithHardKeyboardType.FALSE:
            return false;
        default:
            Log.e(TAG, "Unexpected mShowImeWithHardKeyboard=" + mShowImeWithHardKeyboard);
            return false;
    }
}

这边输入 settings get secure show_ime_with_hard_keyboard 获取到的值是0
所以shouldShowImeWithHardKeyboard 返回false。导致没有执行到showWindow(),从而没有显示输入框.

猜测

settings put secure show_ime_with_hard_keyboard 1 把这个值设置为1,验证是否可行。
从这边看,如果shouldShowImeWithHardKeyboard返回false,还会判断

config.keyboard == Configuration.KEYBOARD_NOKEYS
            || config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES

如果这边条件为true,也是可以走到下一个流程的。可以代码测试一下这个条件是否为真,我这边验证时可以的。

config.keyboard == Configuration.KEYBOARD_NOKEYS // false
config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES //true

修改

这边涉及到设备环境问题了。
如果设备没有按键,那config.keyboard == Configuration.KEYBOARD_NOKEYS应该是true
如果设备有按键的时候允许弹全键盘,那settings secure show_ime_with_hard_keyboard 这个值不应该设置为0.
所以是我们设备的系统配置有问题。改上面的任一个条件都可。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容