美文网首页
自定义弹窗普通写法

自定义弹窗普通写法

作者: 游私塾白书生 | 来源:发表于2022-05-23 10:37 被阅读0次

1.自定义弹窗普通写法

先写一个简单的页面

代码

Button but=(Button)findComponentById(ResourceTable.Id_bu1);

but.setClickedListener(new Component.ClickedListener() {

@Override

    public void onClick(Component component) {

//  自定义弹窗  对象  并获取上下文

        CommonDialog cd=new CommonDialog(getContext());

        //设置标题

        cd.setTitleText("温馨提示:");

        //设置内容

        cd.setContentText("你确定要退出吗?");

        //设置按钮

        cd.setButton(0, "确定", new IDialog.ClickedListener() {

@Override

            public void onClick(IDialog iDialog, int i) {

System.exit(0);  //关闭虚拟机

            }

});

        cd.setButton(1, "取消", new IDialog.ClickedListener() {

@Override

            public void onClick(IDialog iDialog, int i) {

cd.destroy();//销毁弹窗

            }

});

        cd.show();  // 自定义弹窗出现

    }

});

2.页面内弹窗写法

// 1.创建一个自定义弹窗 对象 并获取上下文

                CommonDialog cd=new CommonDialog(getContext());

                // 2. 设置弹窗圆角的弧度

                cd.setCornerRadius(15);

                //3.获取到当前页面的id页面

                DirectionalLayout dl=(DirectionalLayout) LayoutScatter.getInstance(getContext())

.parse(ResourceTable.Layout_msgdialog,null,false);

//                4.获取布局对象的组件

                Text msg=(Text)findComponentById(ResourceTable.Id_mess);

//                        修海内容

                msg.setText("确定要退出吗?");

//                5.获取2个按钮的组件  取消

                Button but1=(Button)findComponentById(ResourceTable.Id_cecm);

//                    确定

                Button but2=(Button)findComponentById(ResourceTable.Id_sure);

                but1.setClickedListener(new Component.ClickedListener() {

@Override

                    public void onClick(Component component) {

cd.destroy();  //  弹窗销毁

                    }

});

                but2.setClickedListener(new Component.ClickedListener() {

@Override

                    public void onClick(Component component) {

System.exit(0);  //退软件

                    }

});

//                弹窗和xml建立联系

        cd.setContentCustomComponent(dl);

        cd.show();// 弹窗出现

3.封装一个弹窗的写法

写页面设置id

代码

//自己设计的封装工具类

public class MyC {

public static void showDialog(Context context,String mymsg,String te){

//  1.创建一个自定义弹窗  对象  并获取上下文

        CommonDialog cd=new CommonDialog(context);

        // 2. 设置弹窗圆角的弧度

        cd.setCornerRadius(15);

        //3.获取到当前页面的id页面

        DirectionalLayout dl=(DirectionalLayout) LayoutScatter.getInstance(context)

.parse(ResourceTable.Layout_msgdialog,null,false);

//                4.获取布局对象的组件  内容

          Text msg=dl.findComponentById(ResourceTable.Id_mess);

//        标题取出来

        Text title=(Text)dl.findComponentById(ResourceTable.Id_tite);

//                        修改内容

        msg.setText(mymsg);

        title.setText(te);

//                5.获取2个按钮的组件  取消

        Button but1=(Button)dl.findComponentById(ResourceTable.Id_cecm);

//                    确定

        Button but2=(Button)dl.findComponentById(ResourceTable.Id_sure);

        but1.setClickedListener(new Component.ClickedListener() {

@Override

            public void onClick(Component component) {

cd.destroy();  //  弹窗销毁

            }

});

        but2.setClickedListener(new Component.ClickedListener() {

@Override

            public void onClick(Component component) {

System.exit(0);  //退软件

            }

});

//                弹窗和xml建立联系

        cd.setContentCustomComponent(dl);

//                设置弹窗的长宽

        cd.setSize(800,DirectionalLayout.LayoutConfig.MATCH_CONTENT);

        cd.show();// 弹窗出现

    }

相关文章

网友评论

      本文标题:自定义弹窗普通写法

      本文链接:https://www.haomeiwen.com/subject/xhezurtx.html