android softkeyboard showSoftInput vs toggleSoftInput

时间:2012-12-04 02:00:56

标签: 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);

使用:

InputMethodManager imm = (InputMethodManager) getDelegate().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

8 个答案:

答案 0 :(得分:21)

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

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

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

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

答案 1 :(得分: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);
}

P.S inputSearch =(EditText)getSherlockActivity()。findViewById(R.id.inputSearch);

    // Hide Keyboard
InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);

答案 2 :(得分:2)

这个问题的确切答案为什么showSoftInput不能正常工作和toggleSoftInput呢?

您尝试显示键盘的视图是否没有焦点。因此,要解决此问题并使用方法 showSoftInput ,您必须在视图上调用以下方法:

  setFocusable(true);
  setFocusableInTouchMode(true); 

调用上述方法将确保在您单击视图时保留并捕获焦点。

答案 3 :(得分:2)

当设备具有硬(滑出式)键盘时,showSoftInput()不起作用

Android show softkeyboard with showSoftInput is not working?

答案 4 :(得分:1)

试试这个:

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

如果这不起作用,请阅读here

中的教程

答案 5 :(得分:1)

public void hideKeyboard() {
    myTextView.setFocusable(true);
    myTextView.setFocusableInTouchMode(true);
    imm.hideSoftInputFromWindow(myTextView.getWindowToken(), 0);
}

WORKS

public void hideKeyboard() {
    imm.hideSoftInputFromWindow(myTextView.getWindowToken(), 0);
}

不工作

因为我正在使用片段,所以早先处理了

imm:

在片段中声明imm

private InputMethodManager imm;

然后在片段中添加:

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    imm = (InputMethodManager)
    getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
}

他说经过3到4个小时的研究和失败!!

感谢user_CC! : - )

菲尔

答案 6 :(得分:1)

如果imm的目标视图与editText相同,则ShowSoftInput有效。 您可以imm.isActive(editText)editText.isInputMethodTarget()

进行检查

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

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

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);
                }
            }
        });
    }
});

答案 7 :(得分:0)

我知道这则帖子很老,但是对于那些从现在起寻求答案的人,以上方法均无效。当弹出“警报对话框”时,下面的代码对我有用。

proxy_pass http://127.0.0.1:8000;

显示键盘:

InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

隐藏键盘:

keyboard.toggleSoftInput(editText.getPaintFlags(), 0);