当前位置: 首页 > article >正文

鸿蒙HarmonyOS学习笔记之基于CommonDialog实现自定义PromptDialog

文章目录

  • 效果预览
  • 一、基本概念
  • 二、接口说明
  • 三、实现
    • 1.PromptDialog 类
    • 2.Dialog整体布局dialog_prompt.xml
    • 3.按钮点击效果
      • button_confirm_dialog_ok.xml
        • button_state_natural_confirm_dialog_ok.xml
        • button_state_pressed_confirm_dialog_ok.xml
      • button_confirm_dialog_cancel.xml
        • button_state_natural_confirm_dialog_cancel.xml
        • button_state_pressed_confirm_dialog_cancel.xml
    • 4.预览
  • 四、使用

效果预览

在虚拟机上运行效果,只能通过点击取消和确定按钮关闭弹窗,在不输入内容时无法使用确定按钮。
在这里插入图片描述

一、基本概念

CommonDialog是一种在弹出框消失之前,用户无法操作其他界面内容的对话框。通常用来展示用户当前需要的或用户必须关注的信息或操作。对话框的内容通常是不同组件进行组合布局,如:文本、列表、输入框、网格、图标或图片,常用于选择或确认信息。

二、接口说明

参考官方文档

三、实现

1.PromptDialog 类

package com.tang.draw.dialog;import com.tang.draw.ResourceTable;
import com.tang.draw.utils.LogUtils;
import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.text.RichText;
import ohos.agp.text.richstyles.ImageRichStyle;
import ohos.agp.text.richstyles.RangedRichStyle;
import ohos.agp.text.richstyles.UnderlineRichStyle;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.media.image.ImageSource;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;import java.io.IOException;import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;/*********文件名: PromptDialog*创建者: 醉意丶千层梦*创建时间:2022/2/9 22:36*描述: PromptDialog********/
public class PromptDialog extends CommonDialog {private Context mContext;private Text titleText;private TextField input;private Button okBtn,cancelBtn;private DialogClickListener dialogClickListener;public PromptDialog(Context context) {super(context);this.mContext =context;initComponents();initClickedListener();}private void initClickedListener() {okBtn.setClickedListener(this::onClick);cancelBtn.setClickedListener(this::onClick);}public void onClick(Component component) {if (component==okBtn){LogUtils.info(getClass().getSimpleName()+" --- ok click");//关闭dialoghide();if(dialogClickListener != null){dialogClickListener.onOKClick(input.getText());}}else if(component==cancelBtn){LogUtils.info(getClass().getSimpleName()+" --- cancel click");//关闭dialoghide();if(dialogClickListener != null){dialogClickListener.onCancelClick();}}}private void initComponents(){Component component= LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_dialog_prompt,null,true);okBtn=component.findComponentById(ResourceTable.Id_btn_ok);cancelBtn=component.findComponentById(ResourceTable.Id_btn_cancel);input=component.findComponentById(ResourceTable.Id_field_input);titleText=component.findComponentById(ResourceTable.Id_text_title);okBtn.setEnabled(false);input.addTextObserver(new Text.TextObserver() {@Overridepublic void onTextUpdated(String s, int i, int i1, int i2) {if(s==null || s.isEmpty()){okBtn.setEnabled(false);}else {okBtn.setEnabled(true);}}});super.setContentCustomComponent(component);//居中setAlignment(LayoutAlignment.CENTER);setCornerRadius(50);//设置高度为自适应,宽度为屏幕的0.8setSize(mContext.getResourceManager().getDeviceCapability().width* mContext.getResourceManager().getDeviceCapability().screenDensity/ 160*4/5, MATCH_CONTENT);}/***按钮接口*/public interface DialogClickListener{void onOKClick(String inputData);void onCancelClick();}public void setOnDialogClickListener(DialogClickListener clickListener){dialogClickListener = clickListener;}
}

2.Dialog整体布局dialog_prompt.xml

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_content"ohos:width="match_parent"ohos:left_margin="5vp"ohos:orientation="vertical"ohos:right_margin="5vp"><DirectionalLayoutohos:height="match_content"ohos:width="match_parent"ohos:background_element="#fffffff"ohos:orientation="vertical"><Textohos:id="$+id:text_title"ohos:height="50vp"ohos:width="match_parent"ohos:text="提示"ohos:text_size="20fp"ohos:text_alignment="center"ohos:bottom_margin="2vp"></Text><TextFieldohos:id="$+id:field_input"ohos:height="50vp"ohos:width="match_parent"ohos:top_margin="20vp"ohos:bottom_margin="10vp"ohos:left_margin="20vp"ohos:right_margin="20vp"ohos:left_padding="10vp"ohos:hint="请输入"ohos:text_color="#000000"ohos:text_size="20fp"ohos:text_alignment="vertical_center"ohos:background_element="$graphic:background_field_input"></TextField><DirectionalLayoutohos:height="match_content"ohos:width="match_parent"ohos:background_element="#ffffff"ohos:top_padding="5vp"ohos:bottom_padding="5vp"ohos:bottom_margin="20vp"ohos:orientation="horizontal"ohos:top_margin="20vp"ohos:layout_alignment="center"><Buttonohos:id="$+id:btn_cancel"ohos:height="45vp"ohos:width="0vp"ohos:weight="1"ohos:left_margin="20vp"ohos:right_margin="10vp"ohos:text="取消"ohos:text_color="#D0BE76"ohos:text_size="15fp"ohos:background_element="$graphic:button_confirm_dialog_cancel"></Button><Buttonohos:id="$+id:btn_ok"ohos:height="45vp"ohos:width="0vp"ohos:weight="1"ohos:left_margin="10vp"ohos:right_margin="20vp"ohos:background_element="$graphic:button_confirm_dialog_ok"ohos:text="确定"ohos:text_color="#DCC8A1"ohos:text_size="15fp"></Button></DirectionalLayout></DirectionalLayout></DirectionalLayout>

3.按钮点击效果

component_state_empty要在最后一个,不然会没效果

button_confirm_dialog_ok.xml

<?xml version="1.0" encoding="utf-8"?>
<state-containerxmlns:ohos="http://schemas.huawei.com/res/ohos"><item ohos:state="component_state_pressed"ohos:element="$graphic:button_state_pressed_confirm_dialog_ok"></item><item ohos:state="component_state_empty"ohos:element="$graphic:button_state_natural_confirm_dialog_ok"></item>
</state-container>
button_state_natural_confirm_dialog_ok.xml
<?xml version="1.0" encoding="utf-8"?>
<shapexmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:shape="rectangle"><corners ohos:radius="2vp"></corners><solid ohos:color="$color:gray_515151"></solid><strokeohos:width="1vp"ohos:color="$color:gray_515151"/><bounds ohos:top="1vp"></bounds>
</shape>
button_state_pressed_confirm_dialog_ok.xml
<?xml version="1.0" encoding="utf-8"?>
<shapexmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:shape="rectangle"><corners ohos:radius="2vp"></corners><solid ohos:color="$color:gray_2C2C2C"></solid><strokeohos:width="1vp"ohos:color="$color:gray_2C2C2C"/><bounds ohos:top="1vp"></bounds>
</shape>

button_confirm_dialog_cancel.xml

<?xml version="1.0" encoding="utf-8"?>
<state-containerxmlns:ohos="http://schemas.huawei.com/res/ohos"><item ohos:state="component_state_pressed"ohos:element="$graphic:button_state_pressed_confirm_dialog_cancel"></item><item ohos:state="component_state_empty"ohos:element="$graphic:button_state_natural_confirm_dialog_cancel"></item>
</state-container>
button_state_natural_confirm_dialog_cancel.xml
<?xml version="1.0" encoding="utf-8"?>
<shapexmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:shape="rectangle"><corners ohos:radius="2vp"></corners><solid ohos:color="$color:white_FFFFFF"></solid><strokeohos:width="1vp"ohos:color="$color:gray_CDCDCD"/><bounds ohos:top="1vp"></bounds>
</shape>
button_state_pressed_confirm_dialog_cancel.xml
<?xml version="1.0" encoding="utf-8"?>
<shapexmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:shape="rectangle"><corners ohos:radius="2vp"></corners><solid ohos:color="$color:white_DFDFDF"></solid><strokeohos:width="1vp"ohos:color="$color:white_DFDFDF"/><bounds ohos:top="1vp"></bounds>
</shape>

4.预览

在这里插入图片描述

四、使用

 PromptDialog promptDialog=new PromptDialog(this);promptDialog.setOnDialogClickListener(new PromptDialog.DialogClickListener() {@Overridepublic void onOKClick(String inputData) {addBtn.setText(inputData);}@Overridepublic void onCancelClick() {}});
promptDialog.show();
http://www.lryc.cn/news/2419368.html

相关文章:

  • 蓝桥杯专题之日期篇
  • 基金01、03文件,92、94文件、52、53、59等文件是指什么?
  • 【正点原子MP157连载】第二十八章 A7和M4联合调试-摘自【正点原子】STM32MP1 M4裸机CubeIDE开发指南
  • [病毒分析]熊猫烧香(上)初始分析
  • 【python】sqlalchemy
  • VC++6.0 MSDN下载地址
  • DotNetTextBox V3.0 所见即所得编辑器控件Ver3.3.3 Free(免费版)
  • 高质量C++/C编程指南(林锐)
  • 四、六级考试的标准分计算方法-“710分转换表”
  • 移花接木!轻松搞定暴风影音2关联暴风影音1图标
  • 简单了解一下博弈论
  • Label换行
  • 内网信息收集与上传下载
  • 几个免费的国外php空间
  • 苹果最新系统ios7_手机资讯:你绝对不知道的iOS7隐藏功能
  • Scratch软件编程等级考试一级——20220320
  • Kali-登录暴力破解器工具-medusa使用
  • 基于flex的三层架构特效之效果介绍
  • 免费发布一个网站(保姆级图文教程)
  • Sqlserver 之 SequenceNumber(序列)
  • 转PDP-11时代 美国的大跃进运动
  • 老毛桃winpe优盘启动系统个性修改全攻略.(全)
  • WEP/WPA/WPA2/WPA3初识
  • 中华黑豹计算机病毒,关于中华黑豹病毒...-爱毒霸交流论坛
  • mplayer在CLI下收听网络广播和网络电视
  • Terminal Service License Server
  • 中兴V880 ROOT、刷Recovery刷机教程全解
  • 【历史上的今天】11 月 8 日:为开源献身的互联网之子;卷积神经网络 LeNet-5 问世;特斯拉发明遥控器
  • [SceneKit专题]14-Motion-Control运动控制
  • 织梦CMS企业官网模版源码 淋浴卫浴产品官网源码 卫浴行业模版源码 家居卫浴设计