2 zxd 72 zxd_72 于 2013.04.02 17:46 提问

长时间按 optionMenu 按钮时触发 input keyboard 出现的问题

我在程序中创建了一个optionMenu,当我长时间按optionMenu 按钮时,inputKeyboard就会出现。如何设置当长时间按optionMenu 按钮时,不让 input keyboard 出现?

   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个回答

myq_26
myq_26   2013.04.03 13:20
已采纳

我找到解决问题的方法,要在 Launcher2 应用中

@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;
}
hxn_217
hxn_217   2013.04.03 11:49

OnKeyLongPress 不能在 key menu 中执行,因为没有调用。

我的解决方法:

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);
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!