[英]Android - Disable parent view click events when popup window is viewable
I've spent a large amount of time trying to solve this issue both by myself and searching through here to find a solution, none of which have worked for me. 我花了很多时间试图自己解决这个问题,并在这里搜索以找到解决方案,但没有一个对我有用。
My current scenario is when a popup window appears I want to disable all clickable events on the foreground view which is underneath the popup window. 我当前的情况是,当出现弹出窗口时,我想禁用弹出窗口下方的前景视图上的所有可单击事件。
if (Shared.InspectionData.JobViewModel.RAMS_Id == null || Shared.InspectionData.JobViewModel.RAMS_Id.equals("")) {
// Disable foreground view here
LoadRAMSPopup();
}
private void LoadRAMSPopup() {
mainLayout.getForeground().setAlpha(150);
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View ramsView = layoutInflater.inflate(R.layout.popup_rams, null);
final PopupWindow popupRAMS = new PopupWindow(
ramsView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
if (Build.VERSION.SDK_INT >= 21) {
popupRAMS.setElevation(5.0f);
}
findViewById(R.id.mainLayout).post(new Runnable() {
@Override
public void run() {
popupRAMS.showAtLocation(findViewById(R.id.mainLayout), Gravity.CENTER, 0, 0);
popupRAMS.setOutsideTouchable(false);
popupRAMS.update();
Button btnGenerate = (Button) ramsView.findViewById(R.id.btnGenerate);
btnGenerate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), CreateRAMSActivity.class);
startActivity(intent);
popupRAMS.dismiss();
mainLayout.getForeground().setAlpha(0);
}
});
}
});
}
Hitchhiking off of Akshay Mukadam Disabling all child views inside the layout . 搭便车Akshay Mukadam 禁用布局中的所有儿童视图 。 I've tweaked it slightly to include enabling my views.
我对其进行了微调,以包括启用我的视图。
public static void disableEnableViews(ViewGroup layout) {
layout.setEnabled(false);
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
if (child instanceof ViewGroup) {
disableEnableViews((ViewGroup) child);
} else {
if(child.isEnabled()){
child.setEnabled(false);
} else {
child.setEnabled(true);
}
}
}
}
Simply give your top view an id, reference it, and then put into the method. 只需给您的顶视图提供一个ID,对其进行引用,然后将其放入方法中即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.