iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android自定义对话框Dialog
  • 800
分享到

Android自定义对话框Dialog

dialogAndroid 2022-06-06 01:06:12 800人浏览 八月长安
摘要

本文简单介绍自定义对话框Dialog的使用,代码和结构都非常简单,目的是能够快速使用自定义对话框,在本文中不具体讲解对话框的高级使用。 实现步骤 首先需要自己在我们的.xml文

本文简单介绍自定义对话框Dialog的使用,代码和结构都非常简单,目的是能够快速使用自定义对话框,在本文中不具体讲解对话框的高级使用。

实现步骤

首先需要自己在我们的.xml文件中自己构建布局
布局文件做好之后,我们可以在style文件下自己定义布局的样式
前两步都做好之后,我开始在写java文件

具体实现过程

1.   xml文件


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="Http://schemas.android.com/apk/res/android"
  android:layout_width="300dp"
  android:layout_height="180dp"
  android:gravity="center"
  android:orientation="vertical">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@android:color/holo_green_light">
    <TextView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center_vertical"
      android:text="IP设置"
      android:textColor="#fff"
      android:textSize="24sp" />
  </LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#fff"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="5dp">
    <EditText
      android:id="@+id/et_ip1"
      style="@style/textview14sp"
      android:layout_weight="1"
      android:inputType="phone"
      android:maxLength="3"
      android:textColor="@color/colorPrimary" />
    <EditText
      android:id="@+id/et_ip2"
      style="@style/textview14sp"
      android:layout_weight="1"
      android:inputType="phone"
      android:maxLength="3"
      android:textColor="@color/colorPrimary" />
    <EditText
      android:id="@+id/et_ip3"
      style="@style/textview14sp"
      android:layout_weight="1"
      android:inputType="phone"
      android:maxLength="3"
      android:textColor="@color/colorPrimary" />
    <EditText
      android:id="@+id/et_ip4"
      style="@style/textview14sp"
      android:layout_weight="1"
      android:inputType="phone"
      android:maxLength="3"
      android:textColor="@color/colorPrimary" />
  </LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:orientation="horizontal">
    <Button
      android:id="@+id/btn_ipok"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@android:color/holo_green_light"
      android:text="确认"
      android:textColor="#fff"
      android:textSize="30sp" />
    <View
      android:layout_width="1dp"
      android:layout_height="match_parent"
      android:background="#fff" />
    <Button
      android:id="@+id/btn_ipcancle"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@android:color/holo_green_light"
      android:text="取消"
      android:textColor="#fff"
      android:textSize="30sp" />
  </LinearLayout>
</LinearLayout>

以上是我的xml代码,里面用到了一些简单的组建,大家按自己的需求和风格制作就行。部分组件中用到了style属性,该属性我们同样是在res/value/style文件中构建.
注意:所有组件的首字母都要大写。

2.  style


<!-- 自定义对话框样式 -->
  <style name="dialog_custom" parent="android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:background">#00000000</item>
    <item name="android:windowBackground">@android:color/transparent</item>
  </style>

3.  class文件


public class IP_dialog extends Dialog {
  private Button btnOk, btnCancle;
  private EditText ip1, ip2, ip3, ip4;
  public static String ip = "";
  public IP_dialog(Context context) {
    super(context, R.style.dialog_custom);
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog);
    initView();
    initEvet();
  }
  
  private void initView() {
    btnOk = (Button) findViewById(R.id.btn_ipok);
    btnCancle = (Button) findViewById(R.id.btn_ipcancle);
    ip1 = (EditText) findViewById(R.id.et_ip1);
    ip2 = (EditText) findViewById(R.id.et_ip2);
    ip3 = (EditText) findViewById(R.id.et_ip3);
    ip4 = (EditText) findViewById(R.id.et_ip4);
  }
  
  private void initEvet() {
    btnOk.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        ip = getIP();
        Log.e("IP--->", ip);
        dismiss();
      }
    });
    btnCancle.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        dismiss();
      }
    });
  }
  
  private String getIP() {
    String ip = ip1.getText().toString().trim() + "."
        + ip2.getText().toString().trim() + "."
        + ip3.getText().toString().trim() + "."
        + ip4.getText().toString().trim();
    return ip;
  }
}

该类继承Dialog,在该类中我们需要有一个构造方法在方法里面引用我们的style文件,接下来的就是我们一般套路啦。特别提示一下我在该类中使用dismiss();来销毁对话框。在MainActivity.java中,只需要把这个类实例化一下,创建出对象,调用对象的show();方法就可以将对话框显示出来。

您可能感兴趣的文章:Android中自定义对话框(Dialog)的实例代码Android中制作自定义dialog对话框的实例分享Android实现自定义圆角对话框Dialog的示例代码Android自定义等待对话框Android对话框自定义标题 对话框标题美化操作Android自定义控件实现万能的对话框Android如何自定义升级对话框示例详解


--结束END--

本文标题: Android自定义对话框Dialog

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

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

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

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

下载Word文档
猜你喜欢
  • Android自定义对话框Dialog
    本文简单介绍自定义对话框Dialog的使用,代码和结构都非常简单,目的是能够快速使用自定义对话框,在本文中不具体讲解对话框的高级使用。 实现步骤 首先需要自己在我们的.xml文...
    99+
    2022-06-06
    dialog Android
  • Android自定义样式圆角dialog对话框
    本文实例为大家分享了Android创建自定义样式圆角dialog对话框的具体代码,供大家参考,具体内容如下 效果如上,圆角对话框,标题和正文都可以自己设定 做法: 1.在res文件...
    99+
    2022-11-12
  • Android自定义对话框Dialog的简单实现
    本文着重研究了自定义对话框,通过一下步骤即可清晰的理解原理,通过更改界面设置和style类型,可以应用在各种各样适合自己的App中。首先来看一下效果图:首先是activity的界面点击了上述图片的按钮后,弹出对话框:点击对话框的确定按钮:点...
    99+
    2023-05-30
    android 对话框 dialog
  • Android中自定义对话框(Dialog)的实例代码
    1.修改系统默认的Dialog样式(风格、主题)2.自定义Dialog布局文件3.可以自己封装一个类,继承自Dialog或者直接使用Dialog类来实现,为了方便以后重复使用,...
    99+
    2022-06-06
    自定义 dialog Android
  • Android怎么自定义样式圆角dialog对话框
    这篇文章主要介绍“Android怎么自定义样式圆角dialog对话框”,在日常操作中,相信很多人在Android怎么自定义样式圆角dialog对话框问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android...
    99+
    2023-06-25
  • Android中制作自定义dialog对话框的实例分享
    自定义dialog基础版 很多时候,我们在使用android sdk提供的alerdialog的时候,会因为你的系统的不同而产生不同的效果,就好比如你刷的是MIUI的系统,弹出...
    99+
    2022-06-06
    自定义dialog dialog Android
  • android自定义AlertDialog对话框
    前面一篇文章//www.jb51.net/article/103036.htm介绍了alertDialog的四种简单使用,但是有些时候为了让整个app的风格统一,或者说前面的四...
    99+
    2022-06-06
    alertdialog Android
  • Android 自定义对话框 showSetPwdDialog
    样式如下所示: 布局: layout   dialog_set_pwd.xml <?xml version="." encoding="utf-"&...
    99+
    2022-06-06
    自定义 Android
  • Android实现自定义圆角对话框Dialog的示例代码
    前言: 项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框。   对话框包括:1、圆角       2、app图标 , 提示文本,关闭对话框的"确定"...
    99+
    2022-06-06
    自定义 示例 dialog Android
  • Android自定义等待对话框
    最近,看了好多的APP的等待对话框,发现自己的太lower,于是就研究了一番,最后经过苦心努力,实现一个。 自定义一个LoadingIndicatorView(extend...
    99+
    2022-06-06
    Android
  • android如何自定义对话框
    这篇文章给大家分享的是有关android如何自定义对话框的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。实现效果  定义dialog.xml (res/layout/dialog.xml)<...
    99+
    2023-06-22
  • C/C++ Qt如何自定义Dialog对话框组件
    这篇文章主要介绍“C/C++ Qt如何自定义Dialog对话框组件”,在日常操作中,相信很多人在C/C++ Qt如何自定义Dialog对话框组件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答...
    99+
    2023-06-21
  • Android常用的AlertDialog对话框及自定义对话框
    常用的Dialog有确认对话框,单选按钮对话框,多选按钮对话框,复选按钮对话框另外还有自定义的对话框 AlertDialog的常用方法 setTitle:为对话框设置标题 se...
    99+
    2022-06-06
    自定义 alertdialog Android
  • Android自定义Dialog框样式
    本文实例为大家分享了Android自定义Dialog框样式的具体代码,供大家参考,具体内容如下 首先定义dialog的布局文件,buy_goods_dialog.xml如下: &...
    99+
    2022-11-12
  • Android Dialog对话框详解
    废话不多说了,直接给大家贴代码了。 布局文件xml: <LinearLayout xmlns:android="http://schemas.android.com/a...
    99+
    2022-06-06
    dialog Android
  • Android 对话框sweet-alert-dialog
    android原生的dialog太生硬了,之前看到了这个效果非常不错但是没有用过,今天给别人推荐使用,他遇到了问题,导入后错误非常多,也没有库工程。于是自己认真看了一下,这是个...
    99+
    2022-06-06
    dialog alert Android
  • android自定义带箭头对话框
    本文实例为大家分享了android自定义带箭头对话框的具体代码,供大家参考,具体内容如下 import android.content.Context; import andr...
    99+
    2022-11-11
  • Android studio自定义对话框效果
    本文实例为大家分享了Android studio自定义对话框效果的具体代码,供大家参考,具体内容如下 实现步骤: 第一步:自定义.xml布局文件 <?xml ver...
    99+
    2022-11-12
  • Android中如何自定义对话框
    本文小编为大家详细介绍“Android中如何自定义对话框”,内容详细,步骤清晰,细节处理妥当,希望这篇“Android中如何自定义对话框”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。本文测试的harbor的版本是...
    99+
    2023-06-29
  • Android自定义弹框Dialog效果
    本文实例为大家分享了Android自定义弹框Dialog效果的具体代码,供大家参考,具体内容如下 1.dialog_delete.xml <xml version=...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作