【ARK UI】HarmonyOS Java语言实现警报弹窗Alerter

 1.集成:在项目级build.gradle中添加

allprojects{
    repositories{
        mavenCentral()}
}

在entry目录下的build.gradle中添加

implementation 'io.openharmony.tpc.thirdlib:Alerter:1.0.0'

 

2.示例代码
在entry\src\main\resources\base\layout\ability_main.xml中添加

<Button
     ohos:background_element="#FFFF0000"
     ohos:text_color="#FFFFFFFF"
     ohos:id="$+id:btn_alert1"
     ohos:text="警报"
     ohos:height="50vp"
     ohos:left_margin="30vp"
     ohos:right_margin="30vp"
     ohos:top_margin="10vp"
     ohos:text_size="18fp"
     ohos:width="match_parent"/>

cke_2243.gif

在entry\src\main\java\com\example\alerterdemo\slice\MainAbilitySlice.java的onStart()中添加

Button btnOnClick = (Button) findComponentById(ResourceTable.Id_btn_alert1);
btnOnClick.setClickedListener(new Component.ClickedListener() {
    @Override
    public void onClick(Component component) {

        Alerter.create(MainAbilitySlice.this, componentContainer)
                .setEnableIconAnim(true)
                .setTitle("Alert Title")
                .setText("Alert text...")
                .setDuration(10000)
                .setBackgroundColorInt(0xffF99143)
                .setOnClickListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        new ToastDialog(getContext()).setText("CLICK").show();
                    }
                })
                .show();
    }
});

 

3.运行效果

%E5%8F%8D%E5%8F%8D%E5%A4%8D%E5%A4%8D.gif

欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

posted @ 2022-08-10 08:40  华为开发者论坛  阅读(98)  评论(0编辑  收藏  举报