当前位置: 移动技术网 > 移动技术>移动开发>Android > Android自定义对话框Dialog

Android自定义对话框Dialog

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文简单介绍自定义对话框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();
      }
    });
  }

  /*获取输入的ip值*/
  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();方法就可以将对话框显示出来。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网