当前位置:   article > 正文

3.8【HarmonyOS鸿蒙开发】组件ToastDialog

3.8【HarmonyOS鸿蒙开发】组件ToastDialog

3.8【HarmonyOS鸿蒙开发】组件ToastDialog

作者:韩茹

公司:程序咖(北京)科技有限公司

鸿蒙巴士专栏作家

ToastDialog是在窗口上方弹出的对话框,是通知操作的简单反馈。ToastDialog会在一段时间后消失,在此期间,用户还可以操作当前窗口的其他组件。

所以它的特点:

  • 不能够和用户交互,用户不能点击,触摸
  • 不影响用户的其他操作
  • 短时显示后,自动消失

一、创建一个ToastDialog

点击按钮,弹出ToastDialog。

我们现在xml布局文件中:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:orientation="vertical">

    <Button
        ohos:id="$+id:btn1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="#EEEEEE"
        ohos:layout_alignment="horizontal_center"
        ohos:text="显示一个ToastDialog"
        ohos:text_size="50px"
        ohos:top_margin="20vp"
        ohos:padding="20vp"
        />

</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

布局效果:

WX20210610-153518@2x

在Java代码中:

				//点击按钮,弹出ToastDialog
        Button btn1 = (Button) findComponentById(ResourceTable.Id_btn1);
        btn1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new ToastDialog(getContext())
                        .setText("这是一个吐司对话框")
                        .show();
            }
        });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

运行效果:

toastdialogyunxing1

二、设置ToastDialog

1、设置位置

在xml布局文件中,再添加一个按钮:

<Button
        ohos:id="$+id:btn2"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="#EEEEEE"
        ohos:layout_alignment="horizontal_center"
        ohos:text="设置ToastDialog位置"
        ohos:text_size="50px"
        ohos:top_margin="20vp"
        ohos:padding="20vp"
        />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Java中代码:

				//2.设置 ToastDialog 的位置
        Button btn2 = (Button) findComponentById(ResourceTable.Id_btn2);
        btn2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                /*
                LayoutAlignment.CENTER,居中
                LayoutAlignment.LEFT,垂直居中,左侧显示
                LayoutAlignment.RIGHT
                LayoutAlignment.BOTTOM
                等等。。
                 */
                new ToastDialog(getContext())
                        .setText("这个ToastDialog居中显示")
                        .setAlignment(LayoutAlignment.CENTER)
                        .show();
            }
        });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

运行效果:

toastdialogyunxing2

2、自定义ToastDialog的Component

首先我们再添加一个按钮,ability_main.xml布局文件中,继续添加按钮3:

<Button
        ohos:id="$+id:btn3"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="#EEEEEE"
        ohos:layout_alignment="horizontal_center"
        ohos:text="自定义ToastDialog"
        ohos:text_size="50px"
        ohos:top_margin="20vp"
        ohos:padding="20vp"
        />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在layout目录下,新建一个布局文件,layout_toast.xml:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:orientation="vertical">
    <Text
        ohos:id="$+id:msg_toast"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:layout_alignment="center"
        ohos:text_size="16fp"
        ohos:text="自定义的ToastDialog"
        ohos:padding="20vp"
        ohos:background_element="$graphic:background_toast_element"/>
</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

其中graphic下的background_toast_element.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
       ohos:shape="rectangle">
    <corners
        ohos:radius="30vp"/>
    <solid
        ohos:color="#33ff0000"/>
</shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Java中的代码:

//3.自定义ToastDialog
        Button btn3 = (Button) findComponentById(ResourceTable.Id_btn3);
        btn3.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance(MainAbilitySlice.this)
                        .parse(ResourceTable.Layout_layout_toast, null, false);
                new ToastDialog(getContext())
                        .setContentCustomComponent(toastLayout)
                        .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                        .setAlignment(LayoutAlignment.CENTER)
                        .show();
            }
        });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

运行效果:

toastdialogyunxing3

3、自定义ToastDialog带图片

首先我们再添加一个按钮,ability_main.xml布局文件中,继续添加按钮3:

<Button
        ohos:id="$+id:btn4"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="#EEEEEE"
        ohos:layout_alignment="horizontal_center"
        ohos:text="带图片的ToastDialog"
        ohos:text_size="50px"
        ohos:top_margin="20vp"
        ohos:padding="20vp"
        />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在layout目录下,新建一个布局文件,layout_toast_and_image.xml:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:orientation="horizontal">

    <Image
        ohos:width="30vp"
        ohos:height="30vp"
        ohos:scale_mode="inside"
        ohos:image_src="$media:t4"/>

    <Text
        ohos:id="$+id:msg_toast"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_toast_element"
        ohos:bottom_padding="4vp"
        ohos:layout_alignment="vertical_center"
        ohos:left_padding="16vp"
        ohos:right_padding="16vp"
        ohos:text="这个一个带图片的ToastDialog"
        ohos:text_size="16fp"
        ohos:top_padding="4vp"/>
</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

这里我们需要将一个图片t4拷贝到media目录下:

WX20210610-160901@2x

Java中的代码:

 //4.自定义ToastDialog
        Button btn4 = (Button) findComponentById(ResourceTable.Id_btn4);
        btn4.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(MainAbilitySlice.this)
                        .parse(ResourceTable.Layout_layout_toast_and_image, null, false);
                new ToastDialog(getContext())
                        .setContentCustomComponent(layout)
                        .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                        .setAlignment(LayoutAlignment.CENTER)
                        .show();
            }
        });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

运行效果:

toastdialogyunxing4

更多内容:

1、社区:鸿蒙巴士https://www.harmonybus.net/

2、公众号:HarmonyBus

3、技术交流QQ群:714518656

4、视频课:https://www.chengxuka.com

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/282527
推荐阅读
相关标签
  

闽ICP备14008679号