如何禁用长按选项菜单键以调出键盘?

时间:2011-08-22 06:48:36

标签: android

我在我的应用程序中创建了一个optionMenu,一切正常,但是当我在optionMenu按钮中长按一个inputKeyboard时,我怎么能禁用这个输入键盘出现在长按的选项菜单上...我有的代码wrritten是......

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

2 个答案:

答案 0 :(得分:13)

我在Launcher2应用程序中找到解决问题的来源。
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;
}

答案 1 :(得分:2)

OnKeyLongPress无法使用键盘菜单。 (没有被称为)
这是我找到的解决方案:

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