2012-10-30 45 views
0

在我的應用程序已經寫了的onkeydown方法來處理我的菜單和後退按鈕,它正常工作時,在其所在的活動工作正常,但是當我移動的任何其他活動,然後再返回到本次活動再次,這種方法不起作用。的Android的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); 
    } 

感謝

+0

「則此方法不起作用」這是什麼意思是什麼呢?該方法未被調用?它不符合你的期望?它崩潰? 「不工作」是沒有問題的一個很好的說明。 – Simon

+1

現在完成了。我得到了解決 –

+0

請你給出的解決方案... – baco

回答