You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击同意按钮跳转辅助设置后,如何关闭CardView?

解决方案

核心问题分析

当前代码仅实现了页面跳转逻辑,未在点击按钮时关闭弹窗,导致弹窗持续显示;同时缺少用户操作状态的记录,用户从设置界面返回后弹窗会再次出现。

修改后的完整代码

private void checkAccesibleDialog() {
    // 检查用户是否已同意过,避免重复弹窗
    SharedPreferences sp = getActivity().getSharedPreferences("app_prefs", Context.MODE_PRIVATE);
    boolean hasAgreed = sp.getBoolean("access_agreed", false);
    if (hasAgreed) {
        return;
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.consent_dialog, null);
    CheckBox mCheckBox= dialogView.findViewById(R.id.checkBox);
    Button mButton = dialogView.findViewById(R.id.button1);
    Button mButton2 = dialogView.findViewById(R.id.button2);
    builder.setCancelable(false);
    builder.setView(dialogView);

    // 声明dialog为final,确保点击事件中可访问
    final AlertDialog dialog = builder.create();
    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

    mButton.setOnClickListener (new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // 1. 立即关闭弹窗
            dialog.dismiss();
            // 2. 记录用户同意状态,后续不再弹出
            SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean("access_agreed", true);
            editor.apply();
            // 3. 执行页面跳转
            startActivity(new Intent("android.settings.ACCESSIBILITY_SETTINGS"));
            startActivity(new Intent(getActivity(), TransparentActivity.class));
        }
    });

    mButton2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // 关闭弹窗后跳转主界面
            dialog.dismiss();
            startActivity(new Intent(getActivity(), MainTabActivity.class));
        }
    });

    dialog.show();
    mButton.setEnabled(false);

    mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            mButton.setEnabled(isChecked);
        }
    });
}

关键修改说明

  • 关闭弹窗:在两个按钮的点击事件中添加dialog.dismiss(),确保点击操作触发后弹窗立即关闭
  • 状态持久化:通过SharedPreferences记录用户是否已完成同意操作,避免重复弹出提示
  • 代码简化:优化复选框状态监听逻辑,直接用回调参数控制按钮可用性

内容的提问来源于stack exchange,提问作者Farhath Manaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 03:55:23