我发现 API 19 和 API 23/24 之间关于点击/触摸事件的行为不匹配。下面的示例代码有一个按钮来创建一个弹出窗口,并在这个新窗口中有一个按钮来关闭它。在具有 API 19 的设备上对其进行测试,它按预期工作,如果我单击弹出窗口的按钮,窗口将关闭,但是如果我单击其他任何地方,则没有任何反应。 然而,使用 API 23 或 24 运行相同的代码,如果我单击弹出窗口旁边的按钮,它也会消失,而不会执行我的事件处理程序。
谁能帮我弄清楚为什么会这样,以及我如何才能确保该行为与我在 API 19 上看到的一致? 或者,建议一些解决问题的方法?
主 Activity .java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openPopup(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup, null, false);
final PopupWindow pw = new PopupWindow(layout);
pw.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
pw.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
pw.setFocusable(true);
pw.showAtLocation(findViewById(R.id.activity_main), Gravity.CENTER, 0, 0);
Button b = (Button) layout.findViewById(R.id.popup_button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pw.dismiss();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.danielsh.games.test.MainActivity"
android:id="@+id/activity_main">
<android.support.v7.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open popup"
android:onClick="openPopup"/>
</RelativeLayout>
弹出窗口
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text"
android:textColor="@color/colorPrimaryDark"
android:textSize="24sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Close"
android:id="@+id/popup_button"/>
</LinearLayout>
最佳答案
如果您希望在关闭窗口时进行通用处理,请尝试使用此方法:https://developer.android.com/reference/android/widget/PopupWindow.OnDismissListener.html
pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
// Place your handler code here
});
如果您不希望窗口在用户触摸弹出窗口之外时关闭(注意:在 PopupWindow 中,这些默认情况下是这样的):
pw.setTouchable(true);
pw.setFocusable(false);
pw.setOutsideTouchable(false);
对此的引用:https://developer.android.com/reference/android/widget/PopupWindow.html#setOutsideTouchable(boolean)
This only makes sense for pop-ups that are touchable but not focusable, which means touches outside of the window will be delivered to the window behind.
编辑: 好的,所以我可以重现这个问题,并且我可以确认如果您删除行 pw.setFocusable(true);
您的代码将按需工作。
关于API 23/24 中的 Android OnClick 事件监听器更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38457843/