Android自定义对话框Dialog

所属分类: 软件编程 / Android 阅读数: 21
收藏 0 赞 0 分享

本文简单介绍自定义对话框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();方法就可以将对话框显示出来。

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

更多精彩内容其他人还在看

Android实现悬浮窗体效果

这篇文章主要为大家详细介绍了Android实现悬浮窗体效果,显示悬浮窗口,窗口可以拖动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Andriod studio 打包aar 的方法

这篇文章主要介绍了Andriod studio 打包aar的方法,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

这篇文章主要介绍了Android加载loading对话框的功能及实例代码,不退出沉浸式效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中LayoutInflater.inflater()的正确打开方式

这篇文章主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Delphi在Android下使用Java库的方法

这篇文章主要介绍了Delphi在Android下使用Java库的方法,本文以Android的USB串口通讯库为例,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多