2013-01-24 64 views
1

我想禁用菜單按鈕。我讀了很多文章,沒有任何作品。
我甚至無法檢測菜單按鈕按下的事件。重寫方法如onKeyDown(),onKeyUp(),onPrepareOptionsMenu(),onCreateOptionsMenu()上的 沒有 工作。我真的很絕望,請幫助。檢測菜單按鈕事件

public class MainActivity extends Activity{ 
TranslateAnimation animateTopLayer; 
Button btnOk; 
ImageView ivLockMechanism; 
ViewGroup rlTopLayer; 
FramesSequenceAnimation anim; 

@Override 
public void onAttachedToWindow() { 
    getWindow().addFlags(WindowManager.LayoutParams. FLAG_DISMISS_KEYGUARD); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Intent lockServiceIntent = new Intent(getApplicationContext(),LockService.class); 
    startService(lockServiceIntent); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.test); 
    rlTopLayer = (ViewGroup) findViewById(R.id.rlTopLayer); 
    ivLockMechanism = (ImageView) findViewById(R.id.ivLockMechanism); 
    btnOk = (Button) findViewById(R.id.btnOk); 
    btnOk.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      rlTopLayer.startAnimation(animateTopLayer); 
     } 
    }); 
    Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.pg_0); 
    ivLockMechanism.setImageBitmap(b); 
    anim = AnimationsContainer.getInstance().unlockMechanismAnimation(ivLockMechanism); 
    animateTopLayer = new TranslateAnimation(0, 500,0, 0); 
    animateTopLayer.setDuration(1000); 
    animateTopLayer.setFillAfter(true); 
    animateTopLayer.setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      anim.start(); 
     } 
    }); 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ( Integer.valueOf(android.os.Build.VERSION.SDK) < 7 
      && keyCode == KeyEvent.KEYCODE_BACK 
      && event.getRepeatCount() == 0) { 
     onBackPressed(); 
    } 

    if (keyCode == KeyEvent.KEYCODE_MENU){ 
     return false; 
    } 

    return super.onKeyDown(keyCode, event); 
} 

@Override 
public void onBackPressed() { 
    return; 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    finish(); 
} 

}

+0

看到http://stackoverflow.com/questions/6875924/disable-menu-button –

+0

正如我所說的,我讀了很多討論過這裏包括一個,但這並沒有幫助我。 – prowebphoneapp

+0

如果你不需要MENU按鈕來做任何事情,只需不要定義一個菜單。沒有什麼東西需要被「禁用」。 – CommonsWare

回答

6

這個工作對我來說:

public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_MENU) { 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

返回TRUE以防止該事件被進一步傳播,或 假以表明你還沒有處理這一事件,並應該繼續傳播 。

@Override 
public boolean onPrepareOptionsMenu (Menu menu) { 
    return false; 
} 
+0

當我按下菜單按鈕時,甚至不會輸入此方法。 – prowebphoneapp

+0

嘗試調試你的應用程序,調用的方法是什麼?如果代碼沒有問題 – ObAt

+0

否,則不調用方法。 – prowebphoneapp