android softkeyboard showSoftInput vs toggleSoftInput

pro*_*ock 32 android

showSoftInput()没有为我显示键盘,但toggleSoftInput()确实如此.我看到一些其他帖子说要在使用模拟器时禁用硬键盘,但我没有使用模拟器.我在没有硬键盘的实际设备上加载我的APK.两种方法都不应该有效吗?为什么不起作用showSoftInput()?我想明确地将键盘与特定的文本字段关联起来.

不起作用:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.setText("textchange"); //i see the text field update
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
Run Code Online (Sandbox Code Playgroud)

作品:

InputMethodManager imm = (InputMethodManager) getDelegate().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
Run Code Online (Sandbox Code Playgroud)

Ber*_*ink 23

似乎键盘最初显示但被其他东西隐藏,因为以下工作(但实际上是一个肮脏的解决方法):

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.postDelayed(new Runnable()
{
    @Override
    public void run()
    {
        editText.requestFocus();
        imm.showSoftInput(editText, 0);
    }
}, 100);
Run Code Online (Sandbox Code Playgroud)

在查看logcat时,我怀疑此消息背后的原因隐藏了最初显示的键盘:

在开始输入时隐藏剪贴板对话框:由其他人完成...!

  • 当我尝试在 onCreate 中以编程方式显示键盘时,这实际上是唯一对我有用的东西。我希望我知道为什么。不管怎么说,还是要谢谢你.. (3认同)

Maz*_*ser 5

显示键盘+焦点以及是否要隐藏键盘

@Override
public void onResume () {
    super.onResume();

    inputSearch.setFocusableInTouchMode(true);
    inputSearch.requestFocus();

    // Show Keyboard
    InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(
            Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(inputSearch, InputMethodManager.SHOW_IMPLICIT);
}
Run Code Online (Sandbox Code Playgroud)

PS inputSearch =(EditText)getSherlockActivity().findViewById(R.id.inputSearch);

    // Hide Keyboard
InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)


小智 5

如果 imm 的目标视图与 editText 相同,则 ShowSoftInput 起作用。imm.isActive(editText)您可以通过或进行检查editText.isInputMethodTarget()

ToggleSoftInput只是切换imm当前目标的键盘。

imm 的目标视图是在通过 更改焦点后设置的editText.requestFocus()。我认为这两个任务之间存在一些后处理,因为一个后运行是不够的。我测试了双重帖子,它对我有用。

editText.requestFocus();
editText.post(new Runnable() {
    @Override
    public void run() {
        editText.post(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.showSoftInput(editText, 0);
                }
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)


Lau*_*ura 1

尝试这个:

public void showTheKeyboard(Context context, EditText editText){
    InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请阅读此处的教程

  • 我不得不切换。由于某种原因 showSoftInput 对我不起作用 (2认同)