如何关闭 Android 中的弹出窗口?
How to dismiss a popup in Android?
我有一个弹出窗口 window,它会在单击 activity 中的按钮时弹出。该对话框覆盖整个屏幕,仅包含三个按钮。如果单击按钮,则它们会相应地起作用。但是,如果用户触摸弹出窗口中按钮以外的任何地方,我希望弹出窗口消失。我尝试了以下代码。
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(layout, Gravity.FILL, 0, 0);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
popupWindow.dismiss();
return true;
}
return false;
}
});
但是没有用。我还设置了这个:
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
我的弹出式布局如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="10px">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="10dp">
<ImageButton
android:id="@+id/voiceCall"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center|center_horizontal"
android:background="@drawable/round_button"
android:padding="15dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_call_black_24dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Voice Call"
android:textColor="#ffffff"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
如果我单击此弹出窗口中除这两个按钮以外的任何位置,我希望将其关闭。
什么都不适合我。谁能建议我该怎么做?此外,我希望在单击设备后退按钮时弹出窗口消失。
只需使用 popupWindow.dismiss();
为什么使用 setTouchInterceptor
等等..
你的意思是:
popupWindow.setCancelable(true);
或
popupWindow.setCanceledOnTouchOutside(true);
取决于构建器级别或对话级别
@更新2:
// set a listener to listen your popup window click event
popupWindow.getContentView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
如果点击后退按钮,检查是否显示:
@Override
public void onBackPressed() {
if(popupWindow.isShowing()) {
popupWindow.dismiss();
return;
}
super.onBackPressed();
}
如果 popupWindow 是 AlertDialog 的对象,您可以使用
dialog.setCanceledOnTouchOutside(true);
在出现弹出窗口的 activity 中尝试此代码段:
@Override
public void onBackPressed() {
super.onBackPressed();
popupWindow.setOutsideTouchable(true);
popupWindow.dismiss();
}
您可以为存在两个按钮的线性布局编写一组 OnClick 侦听器。像这样...
linearlayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
答案在这里-
下面这样写就可以了:
popupWindow.setOutsideTouchable(true);
popupWindow.setCancelable(true);
它会很好用!
如果回答有帮助,请投票。
首先将布局的宽度和高度设置为wrap_content,以便您可以查看剩余的屏幕。
然后你可以启用 setCancelable() 属性为真
popupWindow.setCancelable(真);
或者你可以使用
popupWindow.dismiss();
在您要覆盖的 onBackPress() 方法中
例如:-
@Override
public void onBackPressed() {
super.onBackPressed();
popupWindow.dismiss();
}
我有一个弹出窗口 window,它会在单击 activity 中的按钮时弹出。该对话框覆盖整个屏幕,仅包含三个按钮。如果单击按钮,则它们会相应地起作用。但是,如果用户触摸弹出窗口中按钮以外的任何地方,我希望弹出窗口消失。我尝试了以下代码。
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(layout, Gravity.FILL, 0, 0);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
popupWindow.dismiss();
return true;
}
return false;
}
});
但是没有用。我还设置了这个:
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
我的弹出式布局如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="10px">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="10dp">
<ImageButton
android:id="@+id/voiceCall"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center|center_horizontal"
android:background="@drawable/round_button"
android:padding="15dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_call_black_24dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Voice Call"
android:textColor="#ffffff"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
如果我单击此弹出窗口中除这两个按钮以外的任何位置,我希望将其关闭。 什么都不适合我。谁能建议我该怎么做?此外,我希望在单击设备后退按钮时弹出窗口消失。
只需使用 popupWindow.dismiss();
为什么使用 setTouchInterceptor
等等..
你的意思是:
popupWindow.setCancelable(true);
或
popupWindow.setCanceledOnTouchOutside(true);
取决于构建器级别或对话级别
@更新2:
// set a listener to listen your popup window click event
popupWindow.getContentView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
如果点击后退按钮,检查是否显示:
@Override
public void onBackPressed() {
if(popupWindow.isShowing()) {
popupWindow.dismiss();
return;
}
super.onBackPressed();
}
如果 popupWindow 是 AlertDialog 的对象,您可以使用
dialog.setCanceledOnTouchOutside(true);
在出现弹出窗口的 activity 中尝试此代码段:
@Override
public void onBackPressed() {
super.onBackPressed();
popupWindow.setOutsideTouchable(true);
popupWindow.dismiss();
}
您可以为存在两个按钮的线性布局编写一组 OnClick 侦听器。像这样...
linearlayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
答案在这里-
下面这样写就可以了:
popupWindow.setOutsideTouchable(true);
popupWindow.setCancelable(true);
它会很好用! 如果回答有帮助,请投票。
首先将布局的宽度和高度设置为wrap_content,以便您可以查看剩余的屏幕。 然后你可以启用 setCancelable() 属性为真 popupWindow.setCancelable(真);
或者你可以使用 popupWindow.dismiss();
在您要覆盖的 onBackPress() 方法中 例如:-
@Override
public void onBackPressed() {
super.onBackPressed();
popupWindow.dismiss();
}