gpt4 book ai didi

android - 如何关闭 Android 中的弹出窗口?

转载 作者:太空狗 更新时间:2023-10-29 16:12:23 26 4
gpt4 key购买 nike

我有一个弹出窗口,当单击 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>

enter image description here

如果我单击此弹出窗口中除这两个按钮以外的任何地方,我希望它被关闭。什么都不适合我。谁能建议我该怎么做?另外,如果单击设备后退按钮,我希望弹出窗口消失。

最佳答案

您可以为存在两个按钮的线性布局编写一组 OnClick 监听器。像这样...

linearlayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});

关于android - 如何关闭 Android 中的弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42619382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com