2014-11-03 74 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);