当前位置:   article > 正文

Android系统onKeyDown监控/拦截/监听/屏蔽返回键、菜单键和Home键_android 弹出dialog禁止调用activity onkeydown

android 弹出dialog禁止调用activity onkeydown
Android系统onKeyDown监控/拦截/监听/屏蔽返回键、菜单键和Home键

protected void dialog() {
        AlertDialog.Builder builder = new Builder(TestActivity.this);
        builder.setMessage("确定要退出吗?");
builder.setTitle("提示");
builder.setPositiveButton("确认",
new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        TestActivity.this.finish();
                    }
                });
        builder.setNegativeButton("取消",
new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.create().show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键
dialog();
          return false;
        } else if(keyCode == KeyEvent.KEYCODE_MENU) {
//        rl.setVisibility(View.VISIBLE);
Toast.makeText(TestActivity.this, "Menu", Toast.LENGTH_SHORT).show();
            return false;
        } else if(keyCode == KeyEvent.KEYCODE_HOME) {
            //由于Home键为系统键,此处不能捕获,需要重写onAttachedToWindow()
        Toast.makeText(TestActivity.this, "Home", Toast.LENGTH_SHORT).show();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
//  拦截/屏蔽系统Home键
public void onAttachedToWindow() { 
         this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);    
         super.onAttachedToWindow();
    }

分类: Android
0
0
    (请您对文章做出评价)   
« 上一篇: network: Android 网络判断(wifi、3G与其他)
» 下一篇: Android中的“再按一次返回键退出程序”实现
posted @ 2013-01-08 16:44 与时俱进 阅读( 10291) 评论( 1 编辑 收藏
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/461417
推荐阅读
相关标签
  

闽ICP备14008679号