Android 显示Dialog的同时自动弹出软键盘;

需求大致就是这样的:用户点击按钮弹出Dialog,Dialog中有输入框,然后Dialog弹出后要自动弹出软键盘;(如果让用户自己手动点击输入框再弹出软键盘的话,用户体验太差了);

好的,需求大致就是这样;很简单嘛Dialog.show();后手动打开软键盘就可以了嘛;

第一次尝试,代码是这样的:

InputPwdDialog inputPwdDialog = new InputPwdDialog(context, Gravity.CENTER,R.layout.item_dialog2);
        inputPwdDialog.show();
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
//                        如果软键盘在窗口上已经显示,则隐藏,反之则显示
        inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

好神奇,软键盘竟然没有弹出来


重新尝试:没关系,这次使用强制打开软键盘的方法;

InputPwdDialog inputPwdDialog = new InputPwdDialog(context, Gravity.CENTER,R.layout.item_dialog2);
        inputPwdDialog.show();
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
//                        强制显示软键盘,view为当前的输入框对象
        inputMethodManager.showSoftInput(inputPwdDialog.findViewById(R.id.v),InputMethodManager.SHOW_FORCED);

showSoftInput();第一个参数是当前输入框的对象;但是!!!在Dialog中无效;还是没有软键盘弹出


嗯,不要气馁,仔细想一想感觉软键盘可能也是一种Dialog,两个Dialog同时弹出会不会有什么冲突;

第三次重新尝试:在Dialog显示完成之后再去弹出软键盘;

InputPwdDialog inputPwdDialog = new InputPwdDialog(context,Gravity.BOTTOM,R.layout.item_dialog);
inputPwdDialog.show();
inputPwdDialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialogInterface) {
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }
});

呵呵哒,软键盘还是没有自动弹出


那这样呢,第四次重新尝试!

InputPwdDialog inputPwdDialog = new InputPwdDialog(context,Gravity.BOTTOM,R.layout.item_dialog);
inputPwdDialog.show();
new Thread(new Runnable() {
    @Override
    public void run() {
        SystemClock.sleep(300);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        });

    }
}).start();

开启一个子线程睡眠300ms(我Dialog有个动画200ms),然后在主线程中弹出软键盘;这次软键盘弹出来了,真的在Dialog弹出后自动弹出来了


欣喜之余总感觉用这种开启线程延迟弹出不太好;

好的,我要放大招了!!!

<!--带输入框-->
<style name="inputDialog" parent="@android:style/Theme.Holo.Light.Dialog">
    <item name="android:windowFrame">@android:color/transparent</item><!-- 边框 -->
    <item name="android:windowIsFloating">true</item><!-- 是否浮现在activity之上 -->
    <item name="android:windowIsTranslucent">false</item><!-- 半透明 -->
    <item name="android:windowNoTitle">true</item><!-- 无标题 -->
    <item name="android:windowBackground">@android:color/transparent</item><!-- 自己想要的背景 -->
    <item name="android:backgroundDimEnabled">true</item><!-- 模糊 -->
    <item name="android:windowSoftInputMode">stateAlwaysVisible</item><!--显示软件盘-->
</style>

给你的Dialog自定义个样式就OK了。

网上有说使用Handler弹出软键盘的;

示例代码:

private final int BOND = 1;
private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case BOND:
                InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                break;
        }
    }

};
InputPwdDialog inputPwdDialog = new InputPwdDialog(context,Gravity.BOTTOM,R.layout.item_dialog);
 inputPwdDialog.show();
handler.sendEmptyMessageDelayed(BOND,100);
也可以使用,Dialog就是使用Handler来控制隐藏和显示的;但是!!!不加延迟发送消息时,偶尔会出现自动弹出后又自动隐藏了。加上延迟发送消息后,第一次总是不自动弹出;

总结一下:

延迟弹出软键盘和自定义Dialog样式都可以弹出软键盘;那种更好自己试试就知道了;

猜你喜欢

转载自blog.csdn.net/qq_35605213/article/details/80137292