【问题标题】:Turn off click activity behind DialogFragment?关闭 DialogFragment 后面的点击活动?
【发布时间】:2019-04-29 07:48:58
【问题描述】:

当我打开一个对话框片段时,我无法取消后台按钮的点击事件。我正在尝试通过另一个片段中的按钮调用对话框片段。

public class DialogPaymentSuccessFragment extends DialogFragment {
private View root_view;
class rootViewClick implements OnClickListener {
    rootViewClick () {
    }

    public void onClick(View view) {
        DialogPaymentSuccessFragment.this.dismiss();
    }
}

public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
    this.root_view = layoutInflater.inflate(R.layout.dialog_payment_success, viewGroup, false);
    ((FloatingActionButton) this.root_view.findViewById(R.id.fab)).setOnClickListener(new rootViewClick());
    return this.root_view;
}

@NonNull
public Dialog onCreateDialog(Bundle bundle) {
    bundle = super.onCreateDialog(bundle);
    bundle.requestWindowFeature(1);
    return bundle;
}

public void onDestroyView() {
    super.onDestroyView();
}

}

【问题讨论】:

  • 这看起来像反编译代码..
  • 当您的活动包含 CollapsingToolbarLayout 时您是否面临此问题
  • 在xml中设置dialog可点击的根项为false。在dialog_payment_success.xml根项中添加android:clickable="false"
  • 发布对话框片段的 xml 代码

标签: android dialogfragment


【解决方案1】:

您可以在 onCreateDialog 方法中设置可取消的 false

喜欢这个

this.setCancelable(false)

【讨论】:

  • 所以你的对话框出现了?然后在后面点击它消失?
  • 不,它没有,我仍然可以点击背景上的按钮
  • 所以你应该尝试以新的方式创建一个对话框!你想创建什么类型的对话框
【解决方案2】:

如果你定义了rootLayout,你可以禁用布局中的所有视图。你也可以在打开dialogfragment之前应用activity。

public static void enableDisableView(View view, boolean enabled) {
        view.setEnabled(enabled);
        if ( view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup)view;

            for ( int idx = 0 ; idx < group.getChildCount() ; idx++ ) {
                enableDisableView(group.getChildAt(idx), enabled);
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    相关资源
    最近更新 更多