介绍
这篇文章主要讲解了Android自定义对话框的用法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
Android开发过程中,常常会遇到一些需求场景——在界面上弹出一个弹框,对用户进行提醒并让用户进行某些选择性的操作,
如退出登录时的弹窗,让用户选择“退出“还是“取消”等操作。
Android系统提供了对话框类,以及对话框的子类,常见如AlertDialog来实现此类功能。
一般情况下,利用Android提供的对话框及其子类能够满足多数此类需求,然而,其不足之处体现在:
1。基于Android提供的对话框及其子类样式单一,风格上与应用本身风格可能不太协调;
2。对话框弹窗在布局和功能上有所限制,有时不一定能满足实际的业务需求。
本文将通过在对话框基础上构建自定义的对话框弹窗,以最常见的确认弹框为例。
本样式相对比较简单:上面有一个弹框标题(提示语)、下面左右分别是“确认”和“取消”按钮,当用户点击”确认“按钮时,弹框执行
相应的确认逻辑,当点击”取消”按钮时,执行相应的取消逻辑。
首先,自定义弹框样式:
& lt;及# 63;xml version=?.0”;编码=皍tf-8", # 63;比; http://schemas.android.com/apk/res/android" & lt; LinearLayout xmlns: android=?; android: layout_width=癿atch_parent" android: layout_height=皐rap_content" android:背景=癅drawable/dialog_bg" android:取向=皏ertical"比; & lt; TextView android: id=癅 + id/title" android: layout_width=皐rap_content" android: layout_height=皐rap_content" android: layout_gravity=癱enter" android: paddingTop=?4 dp" 输入textColor=癮ndroid: @color/login_hint" android: textSize=癅dimen/text_size_18"/比; LinearLayout & lt; android: layout_width=癿atch_parent" android: layout_height=皐rap_content" android: layout_marginBottom=?4 dp" android: layout_marginLeft=?0 dp" android: layout_marginRight=?0 dp" android: layout_marginTop=?0 dp"比; & lt; TextView android: id=癅 + id/confirm" android: layout_width=皐rap_content" android: layout_height=皐rap_content" android: layout_marginRight=?0 dp" android: layout_weight=?”; android:背景=癅drawable/btn_confirm_selector" android:重力=癱enter" 输入textColor=癮ndroid: @color/white" android: textSize=癅dimen/text_size_16"/比; & lt; TextView android: id=癅 + id/cancel" android: layout_width=皐rap_content" android: layout_height=皐rap_content" android: layout_marginLeft=?0 dp" android: layout_weight=?”; android:背景=癅drawable/btn_cancel_selector" android:重力=癱enter" 输入textColor=癮ndroid: @color/login_hint" android: textSize=癅dimen/text_size_16"/比; & lt;/LinearLayout> & lt;/LinearLayout>
然后,通过继承对话框类构建确认弹框控件ConfirmDialog:
包com.corn.widget; 进口android.app.Dialog; 进口android.content.Context; 进口android.os.Bundle; 进口android.util.DisplayMetrics; 进口android.view.LayoutInflater; 进口android.view.View; 进口android.view.Window; 进口android.view.WindowManager; 进口android.widget.TextView; 进口com.corn.R; 公共类{ConfirmDialog扩展对话框 私人上下文语境; 私人字符串标题; 私人字符串confirmButtonText; 私人字符串cacelButtonText; 私人ClickListenerInterface ClickListenerInterface; 公共接口ClickListenerInterface { 公共空间doConfirm (); 公共空间doCancel (); } 公共ConfirmDialog (confirmButtonText上下文语境、字符串标题字符串,字符串cacelButtonText) { 超级(上下文,R.style.MyDialog); 这一点。上下文=上下文; 这一点。标题=标题; 这一点。confirmButtonText=confirmButtonText; 这一点。cacelButtonText=cacelButtonText; } @Override 保护空白>公共静态无效退出(最终上下文语境){ 最后ConfirmDialog ConfirmDialog=new ConfirmDialog(上下文,“确定要退出吗,# 63;“,“退出“,“取消“); confirmDialog.show (); confirmDialog。setClicklistener(新ConfirmDialog.ClickListenerInterface () { @Override 公共空间doConfirm () {//TODO自动生成方法存根 confirmDialog.dismiss ();//toUserHome(上下文); AppManager.getAppManager () .AppExit(上下文); } @Override 公共空间doCancel () {//TODO自动生成方法存根 confirmDialog.dismiss (); } }); }Android自定义对话框的用法