2

我想禁用菜单按钮。我阅读了很多文章,但没有任何效果。
我什至无法检测到菜单按钮按下事件。覆盖 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();
}

}

4

1 回答 1

6

这对我有用:

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

返回 true 以防止此事件被进一步传播,或返回 false 以指示您尚未处理此事件并且应该继续传播它。

@Override
 public boolean onPrepareOptionsMenu (Menu menu) {
     return false;
 }
于 2013-01-24T12:49:35.023 回答