本文简单介绍自定义对话框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
# 对话框
# Dialog
# Android中自定义对话框(Dialog)的实例代码
# Android中制作自定义dialog对话框的实例分享
# Android实现自定义圆角对话框Dialog的示例代码
# Android自定义等待对话框
# Android对话框自定义标题 对话框标题美化操作
# Android自定义控件实现万能的对话框
# Android如何自定义升级对话框示例详解
# 自定义
# 自己的
# 类中
# 是在
# 我在
# 都要
# 只需
# 要把
# 我们可以
# 就行
# 两步
# 中不
# 大家多多
# 就可以
# 有一个
# 首字母
# 在我们的
# 目的是
# background
相关文章:
东莞专业网站制作公司有哪些,东莞招聘网站哪个好?
如何自定义建站之星网站的导航菜单样式?
如何获取免费开源的自助建站系统源码?
高防服务器如何保障网站安全无虞?
浅谈Javascript中的Label语句
建站之星安装提示数据库无法连接如何解决?
建站之星Pro快速搭建教程:模板选择与功能配置指南
如何在IIS服务器上快速部署高效网站?
建站之星收费标准详解:套餐费用及年费价格表一览
如何设计高效校园网站?
建站之星备案是否影响网站上线时间?
Python多线程使用规范_线程安全解析【教程】
宝盒自助建站智能生成技巧:SEO优化与关键词设置指南
建站主机是什么?如何选择适合的建站主机?
建站之星北京办公室:智能建站系统与小程序生成方案解析
网页设计与网站制作内容,怎样注册网站?
英语简历制作免费网站推荐,如何将简历翻译成英文?
义乌企业网站制作公司,请问义乌比较好的批发小商品的网站是什么?
C#如何使用XPathNavigator高效查询XML
免费制作小说封面的网站有哪些,怎么接网站批量的封面单?
微网站制作教程,不会写代码,不会编程,怎么样建自己的网站?
如何快速搭建二级域名独立网站?
公司门户网站制作公司有哪些,怎样使用wordpress制作一个企业网站?
如何用IIS7快速搭建并优化网站站点?
北京企业网站设计制作公司,北京铁路集团官方网站?
测试制作网站有哪些,测试性取向的权威测试或者网站?
建站之星伪静态规则如何正确配置?
C++用Dijkstra(迪杰斯特拉)算法求最短路径
大学网站设计制作软件有哪些,如何将网站制作成自己app?
赚钱网站制作软件,建一个网站怎样才能赚钱?是如何盈利的?
建站之星安装模板失败:服务器环境不兼容?
重庆网站制作公司哪家好,重庆中考招生办官方网站?
临沂网站制作企业,临沂第三中学官方网站?
如何在香港免费服务器上快速搭建网站?
专业网站设计制作公司,如何制作一个企业网站,建设网站的基本步骤有哪些?
全景视频制作网站有哪些,全景图怎么做成网页?
如何在宝塔面板中修改默认建站目录?
微信小程序 input输入框控件详解及实例(多种示例)
如何在橙子建站中快速调整背景颜色?
c++23 std::expected怎么用 c++优雅处理函数错误返回【详解】
如何做静态网页,sublimetext3.0制作静态网页?
网站建设制作、微信公众号,公明人民医院怎么在网上预约?
建站之星伪静态规则如何设置?
如何打造高效商业网站?建站目的决定转化率
TestNG的testng.xml配置文件怎么写
怀化网站制作公司,怀化新生儿上户网上办理流程?
免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?
建站之星导航菜单设置与功能模块配置全攻略
完全自定义免费建站平台:主题模板在线生成一站式服务
建站ABC备案流程中有哪些关键注意事项?
*请认真填写需求信息,我们会在24小时内与您取得联系。