Android onKeyDown

时间:2012-10-30 05:34:12

标签: android onkeydown

在我的应用程序中,我编写了onkeyDown方法来处理我的菜单和后退按钮,当它写入的活动工作正常时工作正常,但是当我移动任何其他活动然后再次返回此活动时这种方法不起作用。

请让我知道我的错误或可能的解决方案是什么?

@SuppressWarnings("unchecked")
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {


        if(mExpandableListView.isGroupExpanded(commonGroupPosition)&& keyCode == KeyEvent.KEYCODE_MENU){

            if(mllpopmenu.getVisibility() == View.VISIBLE){
                mllpopmenu.setVisibility(View.INVISIBLE);
            }

            //Toast.makeText(getApplicationContext(), "Chat Expanded Menu", Toast.LENGTH_SHORT).show();
            else{

                if(commonGroupPosition>=4){

                    mllpopmenu.setVisibility(View.VISIBLE);

                    mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
                            R.layout.listviewtext, R.id.tvMenuText,
                            data_Contact));

                }

                else if(commonGroupPosition==1){

                    mllpopmenu.setVisibility(View.VISIBLE);

                    mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
                            R.layout.listviewtext, R.id.tvMenuText,
                            data_child_contact));

                }



            }

            return true;
        }

        else{

            if (keyCode == KeyEvent.KEYCODE_MENU) {
                if (mllpopmenu.getVisibility() == View.VISIBLE) {
                    mllpopmenu.setVisibility(View.INVISIBLE);
                }

                else if(commonGroupPosition==0){

                    mllpopmenu.setVisibility(View.VISIBLE);
                    mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
                            R.layout.listviewtext, R.id.tvMenuText, data));

                }

                return true;

            }

        }



        if(keyCode==KeyEvent.KEYCODE_BACK){

            //Toast.makeText(UserMenuActivity.this, "Returning..", Toast.LENGTH_SHORT).show();

            if (mllpopmenu.getVisibility() == View.VISIBLE) {
                mllpopmenu.setVisibility(View.INVISIBLE);
            } else {
                if (CCMStaticVariable.CommonConnection.isConnected()) {
                    //super.onBackPressed();

                    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);      
                    alertDialog.setTitle("CCM Logout");
                    alertDialog.setMessage("Are you sure you want to logout?");
                    alertDialog.setIcon(R.drawable.ic_launcher);

                    //Ok button for Logout
                    alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                            CCMStaticVariable.CommonConnection.disconnect();

                            Intent serviceIntent = new Intent(UserMenuActivity.this,UpdaterService.class);
                            stopService(serviceIntent);

                            //CCMStaticVariable.CommonConnection.removeConnectionListener(arg0);
                            finish();

                        }
                    });

                    //Cancel button
                    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                            dialog.cancel();                        
                        }
                    });
                    alertDialog.show();

                }

            }

            return true;

        }
        return super.onKeyDown(keyCode, event);
    }

由于

1 个答案:

答案 0 :(得分:0)

我认为这可能是因为你的onKeydown()方法的返回值。如果你处理了这个事件,则返回true。如果要允许事件由下一个接收器处理,则返回false。我想如果你将返回值改为true可能会有效。

http://developer.android.com/reference/android/view/KeyEvent.Callback.html#onKeyDown(int,android.view.KeyEvent)

希望它能奏效。 :)