How to Trigger the soft keyboard?

拟墨画扇 提交于 2020-01-11 13:10:14

问题


How can I trigger the software keyboard and add listeners to it's keys?


回答1:


To display the soft keyboard you might try: InputMethodManager.showSoftInput()

As for adding listeners, the best you can do is add a TextChangedListener to an EditText to listen to the changes in the EditText view that are made via the keyboard.




回答2:


Ive tried two options, but none of them worked in the emulator, as i said, i am trying to pop up soft keyboard on long-press menu:

@Override

public boolean onKeyLongPress(int keyCode, KeyEvent event)

{

            if (keyCode == KeyEvent.KEYCODE_MENU)
        {
             showSoftInput.getInputMethodList();
             showSoftInput.toggleSoftInput(showSoftInput.SHOW_FORCED, 0);

            return true;
        }
        return super.onKeyLongPress(keyCode, event);
    }

second option:

View.OnLongClickListener mLongClickListener = new View.OnLongClickListener()
    {

        @Override
        public boolean onLongClick(View v)
        {

            Configuration config = RouteMapActivity.this.getResources()
                    .getConfiguration();
            if (config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES)
            {
                InputMethodManager imm = (InputMethodManager) RouteMapActivity.this
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(mapView, InputMethodManager.SHOW_IMPLICIT); // .SHOW_FORCED);
            }
            return false;
        }

    };



回答3:


You can do this from your`s AndroidManifest.xml by adding

<activity
    android:windowSoftInputMode="stateVisible" ... >
    ...
</activity>

But note: If the user's device has an attached hardware keyboard, the soft input method does not appear. http://developer.android.com/training/keyboard-input/visibility.html



来源:https://stackoverflow.com/questions/2209131/how-to-trigger-the-soft-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!