2014-02-20 143 views
10

我想通過單擊彈出窗口外或後退按鈕來關閉彈出窗口,但是當單擊後退按鈕時我的應用程序退出的,而不是退出應用程序我想關閉彈出窗口。通過後退按鈕關閉彈出窗口

這裏是我的代碼,

ivmainmenu.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      LayoutInflater layoutInflater 
      = (LayoutInflater)getBaseContext() 
       .getSystemService(LAYOUT_INFLATER_SERVICE); 
      View popupView = layoutInflater.inflate(R.layout.popupwindow, null); 
      final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT); 
       popupWindow.showAsDropDown(ivmainmenu, 0,14); 
       popupView.setPadding(0, 0, 0, 10); 
       popupWindow.showAsDropDown(ivmainmenu); 

       popupWindow.setBackgroundDrawable(new BitmapDrawable()); 
       popupWindow.setOutsideTouchable(false); 

       TextView tvpopupwork = (TextView)popupView.findViewById(R.id.tvpopupwork); 
       TextView tvpopupabout = (TextView)popupView.findViewById(R.id.tvpopupabout); 
       TextView tvpopupservices = (TextView)popupView.findViewById(R.id.tvpopupservices); 
       TextView tvpopupcontact = (TextView)popupView.findViewById(R.id.tvpopupcontact); 

       Typeface typeFace2 = Typeface.createFromAsset(getAssets(),"fonts/arboriaboldregular.ttf"); 
       tvpopupwork.setTypeface(typeFace2); 
       tvpopupabout.setTypeface(typeFace2); 
       tvpopupservices.setTypeface(typeFace2); 
       tvpopupcontact.setTypeface(typeFace2); 

       tvpopupwork.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         Intent intent = new Intent(Home.this,Ourwork.class); 
         intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
         startActivity(intent); 
        } 
       }); 

       tvpopupabout.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         Intent intent = new Intent(Home.this,Aboutus.class); 
         intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
         startActivity(intent); 
        } 
       }); 

       tvpopupservices.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 

         Intent intent = new Intent(Home.this,Services.class); 
         intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
         startActivity(intent); 
        } 
       }); 

       tvpopupcontact.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 

         Intent intent = new Intent(Home.this,Contact.class); 
         intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
         startActivity(intent); 
        } 
       }); 
       ivmainmenu.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        popupWindow.dismiss(); 
       } 
      } 
     }); 

它給我我想要的結果,但是當我關閉它不會再次打開,我想再次打開它的菜單所以我應該怎麼辦? 謝謝。

回答

14

更換

popupWindow.setOutsideTouchable(false); 

與此

popupWindow.setOutsideTouchable(true); 
popupWindow.setFocusable(true); 
+0

我不得不將背景設置爲一個非空繪製,至少不上的是Android 4.4.2 –

+1

工作'popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));' –

0

試試這樣:實施onBackPressed()並添加

if(popup!=null) { 
    popup.dismiss(); 
    popup=null; 
} 

並設置PopWindow下面:

popup.setOutsideTouchable(true); 
+0

ACTION_OUTSIDE ???那有什麼用? – akky777

+0

@ akky777當觸摸在活動之外時,您將獲得ACTION_OUTSIDE。但這隻適用於我的要求 –

0

您可以在您的代碼中覆蓋onBackPressed()回調並檢查彈出窗口是否已經顯示(然後解除它),否則您會調用super來獲得正常行爲。

8

維護全球基準爲PopUpWindow,並覆蓋onBackPressed() ...

@Override 
public void onBackPressed() { 
    if (popupWindow != null && popupWindow.isShowing()) { 
     popupWindow.dismiss(); 
    } else { 
     super.onBackPressed(); 
    } 
} 

通過在同一Button解僱......

ivmainmenu.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      if(popupWindow != null && popupWindow.isShowing()) { 
       popupWindow.dismiss(); 
       popupWindow = null; 
      } else { 
       // show pop up now 
      } 
     } 
    }); 
+0

是否有解決方案通過同一個按鈕關閉彈出窗口? – akky777

+0

@ akky777看到編輯答案... –

+2

它不起作用 – akky777

1

試試這個..

使用PopupWindow popupWindow作爲全球變量

使用popup.setOutsideTouchable(true);

@Override 
public void onBackPressed() { 
    if (popupWindow != null) { 
     if (popupWindow.isShowing()) { 
      popupWindow.dismiss(); 
     } 
    } else { 
     finish(); 
    } 
} 
+0

@GopalRao感謝您的編輯。 – Hariharan

+1

可能更安全地使用'if(popupWindow!= null && popupWindow.isShowing())' – tar

2

請寫信onBackPressed(),並具有下列代碼

if(popup!=null){ 
    //dismiss the popup 
    popup.dismiss(); 
    //make popup null again 
    popup=null; 
} 
+0

@Gopal Rao很多thx gopal ji +1對你 –

+0

不客氣。無論如何!不要爲編輯加註:-) ... –

3
popupWindow.setBackgroundDrawable (new BitmapDrawable()); 
popupWindow.setOutsideTouchable(true); 
;)