iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Android如何自定义Dialog
  • 156
分享到

Android如何自定义Dialog

2023-07-02 06:07:50 156人浏览 八月长安
摘要

本篇内容主要讲解“Android如何自定义Dialog”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android如何自定义Dialog”吧!接下来看代码:public class&n

本篇内容主要讲解“Android如何自定义Dialog”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android如何自定义Dialog”吧!

Android如何自定义Dialog

Android如何自定义Dialog

接下来看代码:

public class MyDialog extends Dialog {    private Button yes;//确定按钮    private Button no;//取消按钮    private TextView titleTv;//消息标题文本    private TextView messageTv;//消息提示文本    private String titleStr;//从外界设置的title文本    private String messageStr;//从外界设置的消息文本    private View view;    //确定文本和取消文本的显示内容    private String yesStr, noStr;    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器        public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {        if (str != null) {            noStr = str;        }        this.noOnclickListener = onNoOnclickListener;    }        public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {        if (str != null) {            yesStr = str;        }        this.yesOnclickListener = onYesOnclickListener;    }    public MyDialog(Context context) {        super(context, R.style.MyDialog);    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.mydialog);        //按空白处不能取消动画        setCanceledOnTouchOutside(false);        //初始化界面控件        initView();        //初始化界面数据        initData();        //初始化界面控件的事件        initEvent();    }        private void initEvent() {        //设置确定按钮被点击后,向外界提供监听        yes.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (yesOnclickListener != null) {                    yesOnclickListener.onYesClick();                }            }        });        //设置取消按钮被点击后,向外界提供监听        no.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (noOnclickListener != null) {                    noOnclickListener.onNoClick();                }            }        });    }        private void initData() {        //如果用户自定了title和message        if (titleStr != null) {            titleTv.setText(titleStr);        }        if (messageStr != null) {            messageTv.setText(messageStr);        }        //如果设置按钮的文字        if (yesStr != null) {            yes.setText(yesStr);        }        if (noStr != null) {            no.setVisibility(View.VISIBLE);            view.setVisibility(View.VISIBLE);            no.setText(noStr);        } else {            no.setVisibility(View.GoNE);            view.setVisibility(View.GONE);        }    }        private void initView() {        yes = (Button) findViewById(R.id.yes);        no = (Button) findViewById(R.id.no);        titleTv = (TextView) findViewById(R.id.title);        messageTv = (TextView) findViewById(R.id.message);        view = findViewById(R.id.view_dialog);    }        public void setTitle(String title) {        titleStr = title;    }        public void setMessage(String message) {        messageStr = message;    }        public interface onYesOnclickListener {        public void onYesClick();    }    public interface onNoOnclickListener {        public void onNoClick();    }}

Xml文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="Http://schemas.android.com/apk/res/android"                android:layout_width="match_parent"                android:layout_height="match_parent"               >    <LinearLayout        android:layout_width="260dp"        android:layout_height="wrap_content"        android:background="@drawable/mydialog_shape"        android:orientation="vertical"        android:layout_centerVertical="true"        android:layout_centerHorizontal="true">        <TextView            android:id="@+id/title"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:layout_margin="15dp"            android:gravity="center"            android:text="消息提示"            android:textColor="#333333"            android:textSize="16sp" />        <TextView            android:id="@+id/message"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="20dp"            android:layout_marginRight="20dp"            android:layout_gravity="center"            android:textColor="#666666"            android:text="提示消息" />        <View            android:layout_width="match_parent"            android:layout_height="1px"            android:layout_marginTop="15dp"            android:background="#E4E4E4" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="40dp"            android:orientation="horizontal">            <Button                android:id="@+id/no"                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_marginLeft="10dp"                android:layout_weight="1"                android:background="@null"                android:gravity="center"                android:singleLine="true"                android:text="No"                android:textColor="#7D7D7D"                android:visibility="gone"                android:textSize="16sp" />            <View                android:id="@+id/view_dialog"                android:layout_width="1px"                android:layout_height="match_parent"                android:background="#E4E4E4" />            <Button                android:id="@+id/yes"                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_marginRight="10dp"                android:layout_weight="1"                android:background="@null"                android:gravity="center"                android:singleLine="true"                android:text="Yes"                android:textColor="#38ADFF"                android:textSize="16sp" />        </LinearLayout>    </LinearLayout></RelativeLayout>

调用的方法:

  myDialog = new MyDialog(activity);                            myDialog.setTitle("提示");                            myDialog.setMessage("您输入的关键字未查询到信息,请重新输入");                            myDialog.setYesOnclickListener("确定", new MyDialog.onYesOnclickListener() {                                @Override                                public void onYesClick() {                                    myDialog.dismiss();                                }                            });                            myDialog.show();

这是一个调用只含有确认键的方法,通过mydialog中传的str来进行判断,gone或者vs:

   if (noStr != null) {            no.setVisibility(View.VISIBLE);            view.setVisibility(View.VISIBLE);            no.setText(noStr);        } else {            no.setVisibility(View.GONE);            view.setVisibility(View.GONE);        }

调用俩个按键:

    myDialog = new MyDialog(activity);                    myDialog.setTitle("指纹关闭确认");                    myDialog.setMessage("确认关闭指纹登陆?");                    myDialog.setYesOnclickListener("确定", new MyDialog.onYesOnclickListener() {                        @Override                        public void onYesClick() {                            SharedPreferenceUtil.setInfoToShared("Z", "N");                            myDialog.dismiss();                        }                    });                    myDialog.setNoOnclickListener("取消", new MyDialog.onNoOnclickListener() {                        @Override                        public void onNoClick() {                            cbFingerprint.setChecked(true);                            myDialog.dismiss();                        }                    });                    myDialog.show();

到此,相信大家对“Android如何自定义Dialog”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

--结束END--

本文标题: Android如何自定义Dialog

本文链接: https://www.lsjlt.com/news/340014.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android如何自定义Dialog
    本篇内容主要讲解“Android如何自定义Dialog”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android如何自定义Dialog”吧!接下来看代码:public class&n...
    99+
    2023-07-02
  • 如何在Android中自定义Dialog
    本篇文章为大家展示了如何在Android中自定义Dialog,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。功能:android 提供给我们的只有2种Dialog 即 AlertDialog &...
    99+
    2023-05-31
    android roi dialog
  • Android 自定义Dialog 实例
    开发中经常需要请求网络获取数据,我们在请求网络到得到数据时当中需要等待一些时间,为了增加用户体验,我们一般会用一个Dialog来提示用户我们在加载网络数据。 今天我们来实现如下...
    99+
    2022-06-06
    自定义dialog dialog Android
  • Android自定义对话框Dialog
    本文简单介绍自定义对话框Dialog的使用,代码和结构都非常简单,目的是能够快速使用自定义对话框,在本文中不具体讲解对话框的高级使用。 实现步骤 首先需要自己在我们的.xml文...
    99+
    2022-06-06
    dialog Android
  • Android自定义Dialog框样式
    本文实例为大家分享了Android自定义Dialog框样式的具体代码,供大家参考,具体内容如下 首先定义dialog的布局文件,buy_goods_dialog.xml如下: &...
    99+
    2022-11-12
  • android dialog自定义实例详解
    本人工作有一个月多了。对于android很多东西,都有了新的了解或者说真正的掌握。为了让更多的像我这样的小白少走弯路,所以我会坚持将我在工作中遇到的一些比较令我印象深刻的知识点...
    99+
    2022-06-06
    dialog Android
  • Android自定义弹框Dialog效果
    本文实例为大家分享了Android自定义弹框Dialog效果的具体代码,供大家参考,具体内容如下 1.dialog_delete.xml <xml version=...
    99+
    2022-11-13
  • Android 自定义View 之 Dialog弹窗
    Dialog弹窗 前言正文一、弹窗视图帮助类二、弹窗控制类三、监听接口四、样式五、简易弹窗六、常规使用七、简易使用八、源码 前言   在日常开发中用到弹窗是比较多的,常用于提示作用,比如错误操作提示,余额不足提示,退出登录提...
    99+
    2023-08-18
    自定义Dialog 简易提示弹窗 EasyDialog
  • Android如何实现自定义Dialog的大小
    小编给大家分享一下Android如何实现自定义Dialog的大小,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!具体如下:Android应用开发中,无论是出于功能还...
    99+
    2023-05-30
    android dialog
  • Android自定义Dialog的方法实例
    因为公司需要不同样式的dialog,既有取消和确认键 又需要只有确认键的 并且系统自带dialog样式太丑 所以就自己做了个 接下来看代码: public class M...
    99+
    2022-06-08
    方法 dialog Android
  • Android 自定义dialog的实现代码
    Android 自定义dialog的实现代码 搜索相关关键字网上一大堆实现,但是看完总觉得缺胳膊少腿,绕了不少弯路,终于弄好了自定义dialog。把自己整合的完整代码发上来。 ...
    99+
    2022-06-06
    自定义dialog dialog Android
  • Android自定义弹出框dialog效果
    项目要用到弹出框,还要和苹果的样式一样(Android真是没地位),所以就自己定义了一个,不是很像(主要是没图),但是也还可以。废话不多说了,直接上代码先看布局文件<?xml version="1.0" encoding="u...
    99+
    2023-05-31
    android 弹出框 dialog
  • Android自定义dialog简单实现方法
    本文实例讲述了Android自定义dialog简单实现方法。分享给大家供大家参考,具体如下: @Override protected void onCreate(Bundl...
    99+
    2022-06-06
    方法 dialog Android
  • Android自定义个性化的Dialog示例
    本文实例讲述了Android自定义个性化的Dialog。分享给大家供大家参考,具体如下: Dialog: mDialog = new Dialog(this, R.style...
    99+
    2022-06-06
    dialog Android
  • android自定义dialog的方法是什么
    Android中自定义Dialog的方法有以下几种:1. 创建一个继承自Dialog类的自定义对话框:- 创建一个新的类,继承自Di...
    99+
    2023-09-21
    android
  • Android怎么自定义弹框Dialog效果
    今天小编给大家分享一下Android怎么自定义弹框Dialog效果的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。具体效果如下...
    99+
    2023-06-30
  • 怎么在Android应用中自定义dialog
    这篇文章将为大家详细讲解有关怎么在Android应用中自定义dialog,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。Android 自定义dialog要点:设置自定义dialog的布局文件...
    99+
    2023-05-31
    android dialog roi
  • Android中Dialog如何自定义上下文花式菜单
    这篇文章主要为大家展示了“Android中Dialog如何自定义上下文花式菜单”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Android中Dialog如何自定义上下文花式菜单”这篇文章吧。先自...
    99+
    2023-05-30
    android dialog
  • Android自定义样式圆角dialog对话框
    本文实例为大家分享了Android创建自定义样式圆角dialog对话框的具体代码,供大家参考,具体内容如下 效果如上,圆角对话框,标题和正文都可以自己设定 做法: 1.在res文件...
    99+
    2022-11-12
  • Android编程自定义Dialog的方法分析
    本文实例讲述了Android编程自定义Dialog的方法。分享给大家供大家参考,具体如下: 功能: android 提供给我们的只有2种Dialog 即 AlertDialog...
    99+
    2022-06-06
    方法 自定义dialog dialog Android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作