开发者

How do I disable long press option menu key to bring up keyboard?

I have created an optionMenu in my app, all works fine but when I long-press in the optionMenu button an inputKeyboard appears. How can I disable this input keyboard to appear on long-press of option menu? The code I have written:

public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item)
{

    switch (item.getItemId())
    {
        case R.id.settingOpt:
            Intent intent = new Intent(this, SettingForm.class);
            this.startActivity(intent);
            break;

        case R.id.reminderOpt:
            Intent intentR = new Intent(this, ReminderForm.class);
            this.startActivity(intentR);
            break;

        case R.id.helpOpt:
            Intent intentH = new Intent(this, HelpForm.class);
    开发者_开发问答        this.startActivity(intentH);
            break;

        case R.id.shareOpt:
            Intent share = new Intent(android.content.Intent.ACTION_SEND);
            share.setType("text/plain");
            share.putExtra(Intent.EXTRA_SUBJECT, "Name of the thing to share");
            share.putExtra(Intent.EXTRA_TEXT, "www.gmail.com");
            startActivity(Intent.createChooser(share, "Title of the dialog that will show up"));
            break;

        default:

            return super.onOptionsItemSelected(item);
    }
    return true;
}


I find sources to solve the your problem, in Launcher2 application.
https://android.googlesource.com/platform/packages/apps/Launcher2/+/master/src/com/android/launcher2/Launcher.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean handled = super.onKeyDown(keyCode, event);


    // Eat the long press event so the keyboard doesn't come up.
    if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
        return true;
    }

    return handled;
}


OnKeyLongPress doesn't work with the key menu. (It is not called)
Here is the solution I've found :

private static long timer = 0;
private static final long LONG_PRESS_TIME = 200;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (DEBUG && ConfigApp.DEBUG) {
        Log.d(TAG, "onKeyDown");
    }

    if (keyCode == KeyEvent.KEYCODE_MENU) {
        if (timer == 0) {
            timer = System.currentTimeMillis();
            return false;
        }

        // Longpress
        if (System.currentTimeMillis() - timer > LONG_PRESS_TIME) {
            return true;
        } else {
            timer = System.currentTimeMillis();
            return false;
        }

    } else {
        return super.onKeyDown(keyCode, event);
    }
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (DEBUG && ConfigApp.DEBUG) {
        Log.d(TAG, "onKeyUp");
    }

    if (keyCode == KeyEvent.KEYCODE_MENU) {
        timer = 0;
    }
    return super.onKeyUp(keyCode, event);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜