0

在我的应用程序中,我编写了 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);
    }

谢谢

4

1 回答 1

0

我认为这可能是因为您的 onKeydown() 方法的返回值。如果您处理了该事件,则返回 true。如果您想允许下一个接收者处理事件,请返回 false。我想如果您将返回值更改为true,它可能会起作用。

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

希望它可以工作。:)

于 2014-01-12T07:30:40.260 回答