Harmony鸿蒙实现隐私政策自定义弹窗
这个跟之前的,类AlertDialog的CommonDialog是一样的,不过这个是可以自定义布局的
效果如下:
在这里插入图片描述
一、新建一个PrivacyDialog

public class PrivacyDialog extends CommonDialog {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");
    private Component customComponent;
    private Context context;
    private Component tv_term_tips;
    private Component tv_privacy_tips;
    private Component btn_enter;
    private Component btn_exit;

    private ClickListener clickListener;

    public PrivacyDialog(Context abilityContext) {
        super(abilityContext);
        this.context = abilityContext;
        initComponents();
        setAlignment(TextAlignment.CENTER);
        setSize(850, 1200);
        setTransparent(true);
    }

    private void initComponents() {
        customComponent = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_dialog_privacy, null, true);
        tv_term_tips = customComponent.findComponentById(ResourceTable.Id_tv_term_tips);
        tv_privacy_tips = customComponent.findComponentById(ResourceTable.Id_tv_privacy_tips);
        btn_enter = customComponent.findComponentById(ResourceTable.Id_btn_enter);
        btn_exit = customComponent.findComponentById(ResourceTable.Id_btn_exit);
        super.setContentCustomComponent(customComponent);
        confirm();
    }
    private void confirm() {
        tv_term_tips.setClickedListener(component -> {
            if (clickListener != null) {
                clickListener.onTermListener("点了协议");
            }
            HiLog.debug(LABEL,"点了协议");
        });
        tv_privacy_tips.setClickedListener(component -> {
            if (clickListener != null) {
                clickListener.onPrivacyListener("点了隐私");
            }
            HiLog.debug(LABEL,"点了隐私");
        });
        btn_enter.setClickedListener(component -> {
            if (clickListener != null) {
                clickListener.onEnterListener("点了同意");
            }
            HiLog.debug(LABEL,"点了同意");
        });
        btn_exit.setClickedListener(component -> {
            if (clickListener != null) {
                clickListener.onExitListener("点了退出");
            }
            HiLog.debug(LABEL,"点了退出");
        });
    }

    public void setOnClickListener(ClickListener term) {
        clickListener = term;
    }


    //拦截返回键
    @Override
    public boolean deliverKeyboardCase(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEY_BACK) {//拦截系统返回事件
            return true;
        }
        return super.deliverKeyboardCase(event);
    }
}

二、新建ClickListener

public interface ClickListener {
    void onTermListener(String string);
    void onPrivacyListener(String string);
    void onEnterListener(String string);
    void onExitListener(String string);
}

三、config配置跳转

"actions": [
              "action.system.home",
              "ability.intent.ACTION_PRIVACY_TERM"
            ]

四、添加路由

addActionRoute("ability.intent.ACTION_PRIVACY_TERM", PrivacyTermAbilitySlice.class.getName());

五、实现

public static void showprivate(AbilitySlice abilitySlice){
        PrivacyDialog privacyDialog = new PrivacyDialog(abilitySlice);
        privacyDialog.setOnClickListener(new ClickListener() {
            @Override
            public void onTermListener(String string) {
            //这里之前用present,这个弹窗不会隐藏,论坛问了要用这个startAbility
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withAction("ability.intent.ACTION_PRIVACY_TERM")
                        .build();
                intent.setParam("type","term");
                intent.setOperation(operation);
                abilitySlice.startAbility(intent);
            }

            @Override
            public void onPrivacyListener(String string) {

                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withAction("ability.intent.ACTION_PRIVACY_TERM")
                        .build();
                intent.setParam("type","privacy");
                intent.setOperation(operation);
                abilitySlice.startAbility(intent);
            }

            @Override
            public void onEnterListener(String string) {
                SpUtils.put(abilitySlice,true);
                privacyDialog.destroy();
            }

            @Override
            public void onExitListener(String string) {
                SpUtils.put(abilitySlice,false);
                abilitySlice.terminate();
            }
        });
        privacyDialog.siteRemovable(true);
        privacyDialog.setSwipeToDismiss(true);
        privacyDialog.show();
    }

六、调用

CheckUtils.showprivate(MainAbilitySlice.this);

如果写得不规范,有错误请指出,希望能帮到有需要的同学

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐