2011-08-22 54 views
2

我在我的應用程序中創建了一個選項菜單,所有工作都很好,但是當我在optionMenu按鈕中輸入longKey時,出現了一個inputKeyboard ..我如何禁用此輸入鍵盤以在選項菜單的longpress上出現..代碼我有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

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

葉其工作的感謝...... – AndroidDev

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

這是一個更好的答案。 – NitroG42