我正在尝试制作一个弹出窗口,其中包含文本字段和信息以询问用户,但我想知道如何制作它以便用户可以通过在主要 fragment/Activity 所在的弹出窗口外部单击来关闭它是。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tabstudygroups, container, false);
listview = (ListView) rootView.findViewById(R.id.clist2);
addCourseButton = (Button) rootView.findViewById(R.id.caddcoursebutton);
// do stuff here
addCourseButton.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
if(v == addCourseButton) {
View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_layout, null);
final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
// HERE IS WHAT I THOUGHT WOULD MAKE IT BE ABLE TO ENABLE THE OUTSIDE TOUCH
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
Button btn = (Button) popupView.findViewById(R.id.button);
popupWindow.showAsDropDown(popupView, 0, 0);
}
}
}
最佳答案
将您的 PopupWindow
设置为 wrap_content 并使其可聚焦。
final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
// HERE IS WHAT I THOUGHT WOULD MAKE IT BE ABLE TO ENABLE THE OUTSIDE TOUCH
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Button btn = (Button) popupView.findViewById(R.id.button);
popupWindow.showAsDropDown(popupView, 0, 0);
关于android - 在弹出窗口外单击时,如何关闭弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49483491/