【发布时间】:2014-12-27 03:18:31
【问题描述】:
我在 Android 中遇到了弹出窗口的问题。我想要做的是当用户第一次点击按钮时,会显示弹出窗口。如果显示弹出窗口并且用户再次单击该按钮,则弹出窗口将被隐藏。这是弹出窗口的 XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llAttendeeList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000000"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="HELLO" />
</LinearLayout>
以及点击按钮的代码,它会执行这个方法:
private void openPopUp(){
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getBaseContext().getSystemService(
context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(
R.layout.event_attendee_pop, null);
llAttendeeList = (LinearLayout) popupView
.findViewById(R.id.llAttendeeList);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
{
popupWindow.dismiss();
return true;
}
return false;
}
});
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
但是,当用户第一次点击按钮时,弹出窗口确实出现了。但是当我在弹出窗口之外单击时,它不会关闭。有什么想法吗?
提前致谢。
【问题讨论】:
标签: java android popupwindow