1. ToastDialog profile

  • ToastDialog is a subclass of CommonDialog and their usage is almost identical, except ToastDialog has its own features
  • The ToastDialog consists of a title, a prompt, and a select button

  • The intermediate prompt content is usually used, because the purpose of a ToastDialog dialog is to prompt messages
  • The ToastDialog dialog has its own display time, which is 2 seconds by default and then disappears automatically

2. ToastDialog case

Case: Click the button to pop up a 2 second pop-up prompt message

ability_main


      
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:orientation="vertical">

    <Text
        ohos:id="$+id:msg"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text_size="30fp"
        ohos:text_color="#FFFFFF"
        ohos:text_alignment="center"
        ohos:background_element="# 464343"
        />

</DirectionalLayout>
Copy the code

MainAbilitySlice

package com.xdr630.toastapplication.slice;

import com.xdr630.toastapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // Find the button in the interface
        Button but = (Button) findComponentById(ResourceTable.Id_but);
        // Add click events
        but.setClickedListener(this);

    }

    @Override
    public void onActive(a) {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        // a ToastDialog dialog pops up
        // this: indicates that the current popbox is displayed in the current interface
        ToastDialog td = new ToastDialog(this);
        // Set ToastDialog to display text content
        td.setText("The toast cartridge appears.");
        // Set to center
        td.setAlignment(LayoutAlignment.CENTER);
        // Set the occurrence time in milliseconds. The next step is to set the popup time to only two seconds
        td.setDuration(2000);
        // Make the popbox appeartd.show(); }}Copy the code
  • Run:

  • Click the button, you can see the pop-up message appears

  • The frame disappeared after two seconds

Matters needing attention:

  • Basic use:
ToastDialog t = new ToastDialog(this);
t.setText("What to display")
t.setAlignment(LayoutAlignment.CENTER);
t.show();
Copy the code
  • Related Settings:
ToastDialog toastDialog = new ToastDialog(this);
// Set the size
// If not written, the content is wrapped by default
toastDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT,
DirectionalLayout.LayoutConfig.MATCH_CONTENT);
// Set the duration
// If you do not write, the default is 2 seconds
toastDialog.setDuration(2000);
// Set automatic shutdown
// If you do not write, it is automatically closed
toastDialog.setAutoClosable(true);
// Set the location
// If no, center by default
toastDialog.setAlignment(LayoutAlignment.CENTER);
// Set the prompt content
toastDialog.setText("What to display");
// Let the toast show
toastDialog.show();
Copy the code

3. ToastDialog extraction utility class

ToastDialog extension

  1. You can give the ToastDialog dialog a layout
  2. To extract a ToastDialog into a utility class, you don’t need to write it when you use the popbox. You can call the utility class method directly

Case study:

  • Create a new layout file called:mytoast

  • Change the parent layout to the samematch_content

  • Just write a text in the layout file becauseToastDialogThere’s only one text prompt
  • Because the contents of the text need to be modified every time a message pops up, give the text oneidThe text content is changing, so it is set in Java code

mytoast.xml


      
<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"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text_size="30fp"
        ohos:text_color="#FFFFFF"
        ohos:text_alignment="center"
        ohos:background_element="# 464343"
        />

</DirectionalLayout>
Copy the code
  • Create a toolkit and class

ToastUtils

package com.xdr630.toastapplication.MyToastUtils;

import com.xdr630.toastapplication.ResourceTable;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

public class ToastUtils {
    public static void showDialog(Context context,String message){

        1. Load the XML file into memory
        DirectionalLayout dl = (DirectionalLayout) LayoutScatter.getInstance(context).parse(ResourceTable.Layout_mytoast, null.false);

        //2. Get the text component in the current layout object
        Text msg = (Text) dl.findComponentById(ResourceTable.Id_msg);

        //3. Set the prompt message to the text component
        msg.setText(message);

        // create a ToastDialog object
        ToastDialog td = new ToastDialog(context);
        // Set the size of the popbox, default is the contents of the package
        // Set width and height
        td.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT,DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        // Set the occurrence time
        td.setDuration(2000);
        // Set the alignment
        td.setAlignment(LayoutAlignment.CENTER);
        // Pass the XML object to ToastDialog
        td.setContentCustomComponent(dl);
        // Make the popbox appeartd.show(); }}Copy the code
  • Modify the onClick method in MainAbilitySlice to call the utility class method

package com.xdr630.toastapplication.slice;

import com.xdr630.toastapplication.MyToastUtils.ToastUtils;
import com.xdr630.toastapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // Find the button in the interface
        Button but = (Button) findComponentById(ResourceTable.Id_but);
        // Add click events
        but.setClickedListener(this);

    }

    @Override
    public void onActive(a) {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {

        ToastUtils.showDialog(this."Toast shell extraction tool class"); }}Copy the code
  • Run:

  • Click on the

  • This popbox is similar to the APP popbox we usually play, the only difference is a little big, inmytoast.xmlTo adjust the frameThe size of the.ToastUtils.javaAdjusting the framelocation

  • You can also add an offset to the popup, such as 200px for the Y-axis

  • Run, click, find the popup offset