如何通过触摸窗户外关闭弹出窗口?

时间:2014-11-03 05:16:15

标签: android android-activity popupwindow dismiss

我想在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();
    }

4 个答案:

答案 0 :(得分:0)

尝试在PopupWindow上设置setBackgroundDrawable,如果你触摸它,它应该关闭窗口。

例如

;

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

答案 1 :(得分: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);

    }



  });    

答案 2 :(得分:0)

您可以在onCreate()中使用它:

this.setFinishOnTouchOutside(true);

答案 3 :(得分:0)

设置这两个属性:

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

可替换地:

myPopupWindow.setFocusable(true);