views:

1041

answers:

2

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

+1  A: 

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.

mbaird
Ive tried to do something like that: on pressing long menu button while i`am in map view, to show me the soft keyboard.. but as i press menu, the application crush:this is whhat i write in my proggy:
rayman
InputMethodManager showSoftInput; public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_MENU: showSoftInput.showSoftInput(mapView, 0); break; case KeyEvent.KEYCODE_1: //zoom in mc.zoomIn(); break; case KeyEvent.KEYCODE_2: //zoom out mc.zoomOut(); break; case KeyEvent.KEYCODE_4: // scroll left mc.scrollBy(-4, 0); break; case KeyEvent.KEYCODE_5: // scroll right mc.scrollBy(4, 0); break; } return super.onKeyDown(keyCode, event); } what's wrong? Idan.
rayman
add this to the first line:InputMethodManager showSoftInput=(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
rayman
oh my eyes... Please don't add that much code in comments. Edit your original question to add that code. Also, long menu button press should always cause the soft keyboard to display. If it's causing your app to crash then provide the stack trace... in your original question.
mbaird
Ohh, i am sorry for that, ill note it for next time(about the code adding).about what u said with the long menu press.. ive tried it.. and it just didnt work.. the crush wwas with something else. and now it's fixed.but if i just press long menu.. it doesnt show up.. not in emulator, and not on real phone. ive tried it while i`am on mapview activity. why it doesnt show up?Thanks,Idan.
rayman
A: 

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

};
rayman