2014-11-03 133 views
-1

我想點擊popupwindow以外的地方關閉它。經過調查,我嘗試了很多案例,但都沒有爲我工作,我能從中獲得什麼幫助嗎?如何通過觸摸窗口外面來關閉popupwindow?

這是我的代碼:

package com.javacodegeeks.android.fragmentstest; 

import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.PopupWindow; 
import android.annotation.SuppressLint; 
import android.app.ActionBar.LayoutParams; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.content.Context; 
import android.content.DialogInterface; 


@SuppressLint("InflateParams") 
public class MainActivity extends Activity implements OnClickListener { 

    ImageView mButton1; 
    Context contex; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 

     mButton1 = (ImageView) findViewById(R.id.button1); 
     mButton1.setOnClickListener(new OnClickListener() { 


      public void onClick(View arg0) { 
      mButton1.setEnabled(false); 
      LayoutInflater layoutInflater 
      = (LayoutInflater)getBaseContext() 
       .getSystemService(LAYOUT_INFLATER_SERVICE); 
      View popupView = layoutInflater.inflate(R.layout.popupform, null); 
        final PopupWindow popupWindow = new PopupWindow(
         popupView, 
         LayoutParams.WRAP_CONTENT, 
          LayoutParams.WRAP_CONTENT); 

         Button btnbutton1 = (Button)popupView.findViewById(R.id.login); 
         btnbutton1.setOnClickListener(new Button.OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       popupWindow.dismiss(); 
       mButton1 .setEnabled(true); 
      } 

      }); 



       popupWindow.showAsDropDown(btnbutton1, 50, 250); 

     } 



     }); 
    } 



    @Override 
    public void onBackPressed() { 
     new AlertDialog.Builder(this) 
      .setTitle("The door is open") 
      .setMessage("Are you sure you want to leave?") 
      .setPositiveButton("Leave", new DialogInterface.OnClickListener() 
     { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       finish();  
      } 

     }) 
     .setNegativeButton("Stay", null) 
     .show(); 
    } 
+0

popupWindow.setOutsideTouchable(true); – 2014-11-03 05:19:17

+2

可能重複:http://stackoverflow.com/questions/12232724/popupwindow-dismiss-when-clicked-outside – 2014-11-03 05:23:25

回答

0

試試這個

mButton1.setOnClickListener(new OnClickListener() { 


     public void onClick(View arg0) { 
     mButton1.setEnabled(false); 

     View popupView = getLayoutInflater().inflate(R.layout.popupform, 
       null); 

     final PopupWindow popupWindow= new PopupWindow(popupView, 
       LinearLayout.LayoutParams.WRAP_CONTENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT, true); 
     popupWindow.setBackgroundDrawable(new ColorDrawable(
       android.R.color.transparent)); 
     popupWindow.setFocusable(true); 
     popupWindow.setOutsideTouchable(true); 

        Button btnbutton1 = (Button)popupView.findViewById(R.id.login); 
        btnbutton1.setOnClickListener(new Button.OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      popupWindow.dismiss(); 
      mButton1 .setEnabled(true); 
     } 

     }); 



      popupWindow.showAsDropDown(btnbutton1, 50, 250); 

    } 



    });  
+0

我應該如何使用這???它給了我錯誤的代碼。你可以把它合併到我的代碼? – 2014-11-03 06:42:35

+0

等..我會那麼做.. :) – 2014-11-03 06:45:12

+0

LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,true); 這裏仍然給出錯誤,而不是變量! – 2014-11-03 17:17:29

0

嘗試設置setBackgroundDrawable上PopupWindow如果你觸摸它的外面應該關閉該窗口。例如

;

popup.setBackgroundDrawable(new BitmapDrawable(getResources(), "")); 
0

您可以使用的onCreate這():

this.setFinishOnTouchOutside(true); 
0

設置這兩個屬性:

myPopupWindow.setBackgroundDrawable(new BitmapDrawable()); 
    myPopupWindow.setOutsideTouchable(true); 

或者:

myPopupWindow.setFocusable(true);