107

アクティビティに PopupWindow があります。アクティビティとやり取りしているとき (リストをスクロールしているときなど) にも PopupWindow が表示されます。リストをスクロールできますが、PopupWindow はまだそこにあります。

私が達成したいのは、PopupWindow ではない画面でタッチ/スクロール/クリックなどをしているときに、PopupWindow を閉じたいということです。メニューの仕組みと同じです。メニューの外側をクリックすると、メニューが閉じます。

試してみましsetOutsideTouchable(true)たが、ウィンドウを閉じません。ありがとう。

4

15 に答える 15

138

窓の外を触ったら閉じるように設定setBackgroundDrawableしてみてください。PopupWindow

于 2012-09-02T01:31:43.827 に答える
132

受け入れられた回答に対するWareNinjaのコメントを除いて、提供された回答はどれもうまくいかないことがわかりました.Marcin S.のコメントもおそらく機能します。これが私のために働く部分です:

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

または:

myPopupWindow.setFocusable(true);

違いはわかりませんが、ListPopupWindow のソース コードでは、モダリティが setModal で true に設定されている場合、実際には後者を使用しているため、少なくとも Android 開発者はこれを実行可能なアプローチと見なしており、1 行しかありません。

于 2013-07-31T15:31:55.450 に答える
67

同じ問題に遭遇し、以下のコードとして修正しました。それは私にとってはうまくいきます。

    // Closes the popup window when touch outside.
    mPopupWindow.setOutsideTouchable(true);
    mPopupWindow.setFocusable(true);
    // Removes default background.
    mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

ところで、BitmapDrawable の廃止されたコンストラクターを使用しないでください。この新しい ColorDrawable(android.R.color.transparent)を使用してデフォルトの背景を置き換えてください。

楽しむ@。@

于 2015-06-03T02:22:04.920 に答える
25

遅いことはわかっていますが、ポップアップ ウィンドウにまだ問題があることに気付きました。ポップアップ ウィンドウの外側をタッチまたはクリックするか、ウィンドウ自体にタッチするだけで、ポップアップ ウィンドウを閉じることができる、完全に機能する例を作成することにしました。これを行うには、新しい PopupWindow クラスを作成し、次のコードをコピーします。

PopupWindow.class

public class PopupWindow extends android.widget.PopupWindow
{
Context ctx;
Button btnDismiss;
TextView lblText;
View popupView;

public PopupWindow(Context context)
{
    super(context);

    ctx = context;
    popupView = LayoutInflater.from(context).inflate(R.layout.popup, null);
    setContentView(popupView);

    btnDismiss = (Button)popupView.findViewById(R.id.btn_dismiss);
    lblText = (TextView)popupView.findViewById(R.id.text);

    setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    setWidth(WindowManager.LayoutParams.WRAP_CONTENT);

    // Closes the popup window when touch outside of it - when looses focus
    setOutsideTouchable(true);
    setFocusable(true);

    // Removes default black background
    setBackgroundDrawable(new BitmapDrawable());

    btnDismiss.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View v) {


         dismiss();
        }});

    // Closes the popup window when touch it
/*     this.setTouchInterceptor(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                dismiss();
            }
            return true;
        }
    }); */   
   } // End constructor

   // Attaches the view to its parent anchor-view at position x and y
   public void show(View anchor, int x, int y)
   {
      showAtLocation(anchor, Gravity.CENTER, x, y);
   }
}

次に、ポップアップ ウィンドウのレイアウトを作成します: popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout     
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="1dp"
    android:orientation="vertical"
    android:padding="10dp" >

<TextView 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"  
    android:gravity="center" 
    android:padding="5dp" 
    android:text="PopupWindow Example"
    android:textColor="#000000" 
    android:textSize="17sp" 
    android:textStyle="italic" />

<FrameLayout
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical">

    <Button
        android:id="@+id/btn_dismiss" 
        style="?android:attr/buttonStyleSmall" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Dismiss" 
        android:visibility="gone" />

    <TextView
        android:id="@+id/lbl_dismiss"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Touch outside of this box to dismiss"
        android:textColor="#ffffff"
        android:textStyle="bold" />

</FrameLayout>      

メイン アクティビティで、PopupWindow クラスのインスタンスを作成します。

final PopupWindow popupWindow = new PopupWindow(this);
popupWindow.show(findViewById(R.id.YOUR_MAIN_LAYOUT), 0, -250);

YOUR_MAIN_LAYOUT は、popupWindow がポップアップする現在のアクティビティのレイアウトです。

于 2012-09-29T19:01:16.033 に答える
5

isOutsideTouchable OR を使用isFocusableして、外側に触れたときにポップアップ ウィンドウを閉じることができます

popupWindow.isOutsideTouchable = true // dismiss popupwindow when touch outside

popupWindow.isFocusable = true // dismiss popupwindow when touch outside AND when press back button

ノート

  • 現在、テストの後、popupwindow を閉じるのに役立た setBackgroundDrawable ないことがわかりました

  • PopupWindow(PopupWindow->PopupDecorView->dispatchKeyEventおよびPopupWindow->PopupDecorView->onTouchEvent)で却下のコードを見ると、戻るボタンを押すと消え、ACTION_UP外側に触れると消えることがわかりACTION_UPます。ACTION_OUTSIDE

于 2019-07-18T02:22:09.170 に答える
5
mPopWindow.setFocusable(true);
于 2013-09-27T06:11:30.803 に答える
5
  popupWindow.setTouchable(true);
  popupWindow.setFocusable(true);
  popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);

画面上でクリック/タッチすると、PopupWindow が閉じます。showAtLocation の前に focusable を true に設定していることを確認してください。

于 2017-07-31T06:21:54.443 に答える
4

@LunaKong 提案作品は魅力のようなものです。

ただし、 mPopupWindow.setFocusable(false)を設定します。ポップアップ ウィンドウを非表示にするために必要な不要なタッチを削除します。

例: 画面にポップアップ ウィンドウが表示されていて、ボタンをクリックしようとしているとします。したがって、この場合、(mpopwindow.setFocusable(true) の場合) ボタンの最初のクリックで popupwindow が閉じます。ただし、ボタンを機能させるには、もう一度クリックする必要があります。if**(mpopwindwo.setFocusable(false)** ボタンを 1 回クリックすると、ポップアップ ウィンドウが閉じ、ボタンのクリックがトリガーされます。お役に立てば幸い です。

于 2015-09-25T10:07:07.783 に答える
3

ウィンドウの背景を透明に設定します。

PopupWindow.getBackground().setAlpha(0);

背景をレイアウトに設定した後。正常に動作します。

于 2014-02-17T10:08:44.137 に答える