显示对话框的软键盘

问题描述 投票:0回答:6

我正在显示一个带有编辑文本视图的对话框。但是,只有当用户在编辑视图内按下时,软键盘才会打开。所以我尝试使用以下代码调用InputMethodManager。

InputMethodManager imm =
 (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(dialogField,0);

dialogField 是输入字段。但是,我到底什么时候应该这样做呢?我在对话框的 onStart() 方法中尝试过,但没有任何反应。我之前也尝试过请求dialogField的焦点,但这没有改变。

我也尝试过这个代码

dialogField.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
    public void onFocusChange (View v, boolean hasFocus)
    {
        if (hasFocus)
        {
            Main.log("here");
            dialogInput.getWindow().setSoftInputMode(
              WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            /*
                InputMethodManager mgr =
                  (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                mgr.showSoftInput(dialogField,0);
            */
        }
    }
});

在两个版本中。但没有软键盘愿意出现。 Main.log 只是一个日志,它向我显示该函数实际上被调用了。是的,它被称为。

我可以在对话框打开之前使用 SHOW_FORCED 标志获取键盘。但退出时它不会关闭。我只能在显示对话框之前执行此操作。在任何回调中它也不起作用。

java android kotlin android-softkeyboard android-sdk-2.1
6个回答
201
投票

很棒的问题,我也在尝试这样做并找到了解决方案。

使用对话框构建器类

AlertDialog.Builder
,您必须像这样调用对话框:

AlertDialog.Builder builder = new AlertDialog.Builder();
AlertDialog dialog;

builder.set...

dialog = builder.create();
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();

这对我来说效果很好。

注意:您必须

import android.view.WindowManager.LayoutParams;
以获得那里的常数值。


17
投票
 AlertDialog dialog = new AlertDialog.Builder(this).create();
    dialog.show();
    Window window = dialog.getWindow();
    window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

8
投票

科特林

这是经过测试的代码。

val dialog = AlertDialog.Builder(requireContext()).apply {
    setTitle(…)
    setView(editText)
    setPositiveButton(…)
    setNegativeButton(…)
}
val window = dialog.show().window
window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)

确保从

window
方法访问
show()
属性。从
window
方法获取
create()
为我返回
null
,因此键盘没有显示。

AlertDialog
导入
androidx.appcompat.app.AlertDialog
。 从
WindowManager
导入
android.view


3
投票

这是我的解决方案,它对于对话效果很好。

txtFeedback.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

也许您还需要将其添加到 AndroidManifest.xml 中的活动标记中,以便在对话框关闭时关闭键盘。

android:windowSoftInputMode="stateAlwaysHidden"

1
投票

Kotlin 对话框片段

重写 onStart 方法

override fun onStart() {
    super.onStart()
    dialog.window?.
    setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
    )
}

如果您想在解雇后关闭,请使用以下代码覆盖解雇方法

override fun onDismiss(dialog: DialogInterface?) {
val inputMethodManager = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_IMPLICIT_ONLY)
}

0
投票

我已经尝试并取得了成功:

editText.requestFocus();
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
© www.soinside.com 2019 - 2024. All rights reserved.