2011-03-09 241 views
1

我創建了info_popup.xml(簡單的說,我在彈出窗口上有textview和imagebutton)。我在我的主要活動中顯示,但我不知道如何在點擊btnExitInfo按鈕時關閉該彈出窗口。點擊監聽器關閉pw的內容是什麼?我嘗試了與GONE,但它不起作用,它仍然存在。按鈕點擊關閉彈出問題

LayoutInflater inflater = (LayoutInflater) currentActivity 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.info_popup, 
        null, false), 230, 230, true); 

      pw.showAtLocation(currentActivity.findViewById(R.id.main), 
        Gravity.CENTER, 0, 0); 
      final View popupView=inflater.inflate(R.layout.info_popup, null, false); 

      ImageButton btnExitInfo=(ImageButton)popupView.findViewById(R.id.btnExitInfo); 
      btnExitInfo.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        //popupView.setVisibility(View.GONE); 


      } 
     }); 

回答

1

嘗試使用辭退:

public void onClick(View v) { 
    pw.dismiss(); 
} 

你需要一個final修飾符添加到最終PW,所以你可以把它的onclick方法中:

final PopupWindow pw = new PopupWindow........... 
0

下面的代碼爲我工作:

在班級聲明pw: PopupWindow pw;

   LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

       final View vw = inflater.inflate(R.layout.activity_child, null, false); 

       Button close = (Button) vw.findViewById(R.id.btnClose); 
       close.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View view) { 
         if(pw.isShowing()){ 
          pw.dismiss(); 
         } 
        } 
       }); 

       Button next = (Button) vw.findViewById(R.id.btnProceed); 
       next.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View view) { 
         Intent myIntent = new Intent(view.getContext(), NextActivity.class); 
         startActivity(myIntent); 
        } 
       }); 

       pw = new PopupWindow(vw, LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT, true); 
       pw.setBackgroundDrawable(getResources().getDrawable(R.drawable.gradient_bg)); 
       pw.setOutsideTouchable(false); 
       pw.showAtLocation(sv, Gravity.CENTER, 10, 10);