Android自定义对话框Dialog

所属分类: 软件编程 / Android 阅读数: 43
收藏 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网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多