Androd自定义对话框Dialog视图及参数传递的实现方法

发布时间 - 2026-01-10 22:23:04    点击率:

今天给大家讲讲有关自定义对话框的相关内容,前面两篇都在在利用系统提供的函数来实现对话框,但局限性太大,当我们想自己定义视图的时候,就不能利用系统函数了,就需要我们这里的自定义对话框了,有关自定义对话框的东东,以前有写过一篇《android之Dialog相关》,写的不好,今天给大家重新写一篇

一、雏形构建

先给大家看下这小节的效果图:

自定义一个对话框,内容是四个ImageView横排;

1、Dialog布局

根据上图的对话框样式,我们看一下Dialog的布局定义(custom_dialog.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/log_in_layout" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:orientation="horizontal"> 
 <ImageView 
  android:layout_width="match_parent" 
  android:layout_height="100dip" 
  android:src="@drawable/animal1" 
  android:clickable="true" 
  android:layout_weight="1"/> 
 <ImageView 
  android:layout_width="match_parent" 
  android:layout_height="100dip" 
  android:src="@drawable/animal2" 
  android:clickable="true" 
  android:layout_weight="1"/> 
 <ImageView 
  android:layout_width="match_parent" 
  android:layout_height="100dip" 
  android:src="@drawable/animal3" 
  android:clickable="true" 
  android:layout_weight="1"/> 
 <ImageView 
  android:layout_width="match_parent" 
  android:layout_height="100dip" 
  android:src="@drawable/animal4" 
  android:clickable="true" 
  android:layout_weight="1"/> 
</LinearLayout> 

2、从Dialog派生对话框类

有关构造函数:

有三种构造函数,现在我这里使用重写了两个,这里只需要使用第一个,即传进去context即可;

有关OnCreate()

在OnCreate()时,利用LayoutInflater获取我们对话框的View,然后利用SetContentView指定为我们CustomDialog类的布局。

public class CustomDialog extends Dialog { 
 Context mContext; 
 public CustomDialog (Context context){ 
  super(context); 
  mContext = context; 
 } 
 public CustomDialog(Context context, int theme) { 
  super(context, theme); 
  mContext = context; 
 } 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  LayoutInflater inflater = (LayoutInflater) mContext 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  View layout = inflater.inflate(R.layout.custom_dialog, null); 
  this.setContentView(layout); 
 } 
} 

3、主函数(MainActivity)

在MainActivity中,我们写一个Button,当用户点击的时候弹出我们自定义的对话框实例

MainActivity的布局:(activity_main.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 tools:context=".MainActivity"> 
 <Button 
  android:id="@+id/btn_pop_dialog" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="弹出对话框"/> 
</RelativeLayout> 

代码中的处理:(MainActivity.java)

在点击Btn的时候,创建CustomDialog类的实例,并显示

public class MainActivity extends Activity { 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  Button btn = (Button)findViewById(R.id.btn_pop_dialog); 
  btn.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View view) { 
    CustomDialog dialog = new CustomDialog(MainActivity.this); 
    dialog.show(); 
   } 
  }); 
 } 
} 

这部分源码在文章最底部给出;

二、定义对话框样式

这里再回头看看上面弹出的对话框:

在布局中,我们只定义了一个水平布局,里面放了四个ImageView,即那四个小动物,那上面那一坨是哪来的呢(我用红笔圈出来的那块)?能不能去掉?这节,我们就说说有关样式的问题。

第一个问题,只所有上面那一坨,是因为我们没有指定对话框样式,系统会使用默认样式,那一坨就是标题栏。

要去掉的话,我们就需要自定义样式。

1、自定义样式

我们的样式是写在res/values文件夹下的style.xml文件中的,如图,如果没有style.xml,自已新建一个,位置如图:

这里定义的样式代码是这样的:

<style name="dialog" parent="android:Theme.Dialog"> 
 <item name="android:windowFrame">@null</item> 
 <item name="android:windowIsFloating">true</item> 
 <item name="android:windowContentOverlay">@null</item> 
 <item name="android:windowNoTitle">true</item> 
</style> 

先对这几个参数解释一下:

Android:windowFrame:界面对应的前景图片;

android:windowIsFloating:表示浮在屏幕上的,如果在这里使用了,整个layout就会在 屏幕中心,相当于浮在屏幕上,所以这个只适用于dialog

android:windowContentOverlay:表示标题栏的阴影部分的样式,使用图片或者颜色

android:windowNoTitle:标题栏是否隐藏,这就是我们上面显示的标题栏

有关样式的内容很多很杂,这里有篇文章大家可以参考下《Andriod中Style/Theme原理以及Activity界面文件选取过程浅析》,有机会给大家总结一下有关样式和主题的内容,到时再细讲,这里不是本篇的重点,就不再细讲了。

2、使用样式

方法一:

这里有两种方法来使用样式,主要还是利用构造函数,还记得我们上面说过,对话框的两个构造函数:

public class CustomDialog extends Dialog { 
 Context mContext; 
 public CustomDialog(Context context) { 
  super(context); 
  mContext = context; 
 } 
 public CustomDialog(Context context, int theme) { 
  super(context, theme); 
  mContext = context; 
 } 
 ………… 
} 

看第二个就是我们的Theme样式,所以第一种使用样式的方法是在构造时直接将样式传进来,如果这样使用,那我们在构造对话框时应该是这样的“:

public class MainActivity extends Activity { 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  Button btn = (Button)findViewById(R.id.btn_pop_dialog); 
  btn.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View view) { 
    CustomDialog dialog = new CustomDialog(MainActivity.this,R.style.dialog); 
    dialog.show(); 
   } 
  }); 
 } 
} 

即在新建CustomDialog实例时,第二个参数传进来我们上面定义的样式。

方法二:

第二种方法,就是我们在构造时,不让别人传样式,只让传进来Context,但在内部,我们利用Super(context,theme)来指定样式,代码如下:

public class CustomDialog extends Dialog { 
 Context mContext; 
 public CustomDialog(Context context) { 
  super(context,R.style.dialog); 
  mContext = context; 
 } 
 ………… 
} 

这种情况一般用在我们封装代码时,不让其它人在外部改变我们的样式时使用的。

无论使用哪种方法,我们就已经指定我我们的对话框样式,现在的效果是这样的:

看到了没,上面的标题栏没了,下面再看看有关参数传递的问题

三、参数传递

这里涉及到两个问题:传进去和传出来;

传进去:下面有两个按钮,当用户点第一个按钮时,在对话框中显示"From btn 1",如果用户点击第二个按钮,在对话框中显示“From btn 2”

传出来:这四个图片都是可以点击的,当用户点击任何一个图片,把它的ID传出来,并设置到我们的MainActivity中;

这里为了好理解,更改了对话框的布局,将水平布局改成了垂直布局。并且在MainActiviy下面加了一个ImageView.

1、传进去

往对话框里传参数,一般就是利用构造函数来完成的,很简单,即在对话框的构造函数上加上我们自己想要传的参数,这里我们需要额外传一个字符串,来表示从哪个BTN来的。

所以对话框的构造函数就变成了这样:

public class CustomDialog extends Dialog{ 
 private Context mContext; 
 private String mStr; 
 public CustomDialog(Context context, String str, int theme) { 
  super(context, theme); 
  mContext = context; 
  mStr = str; 
 } 
 ………… 
}

在使用的时候,也很简单:

Button btn2 = (Button)findViewById(R.id.btn_pop_dialog_2); 
btn2.setOnClickListener(new View.OnClickListener() { 
 @Override 
 public void onClick(View view) { 
  CustomDialog dialog = new CustomDialog(MainActivity.this,"From btn 2",R.style.dialog); 
  dialog.show(); 
 } 
}); 

直接利用构造函数传参即可

2、传出来

利用构造函数传参数大家都知道,但要怎么把用户的操作信息传出来就不是那么简单了,这里就要利用回调来实现了!
回调函数的使用主要依照下面这些步骤:

在对话框中:

public class CustomDialog extends Dialog { 
 // 利用interface来构造一个回调函数 
 public interface ICustomDialogEventListener { 
  public void customDialogEvent(int valueYouWantToSendBackToTheActivity); 
 } 
 private ICustomDialogEventListener onCustomDialogEventListener; 
 // 在构造函数中,设置进去回调函数 
 public CustomDialog(Context context, 
      ICustomDialogEventListener onCustomDialogEventListener) { 
  super(context); 
  this.onCustomDialogEventListener = onCustomDialogEventListener; 
 } 
 //当你想把值传回去的时候,调用回调函数将值设置进去 
 @Override 
 public void onCreate(Bundle savedInstanceState) 
 { 
  Button btnOk = (Button) findViewById(R.id.customDialogButton); 
  btnOk.setOnClickListener( new Button.OnClickListener() 
  { 
   public void onClick(View v) { 
    onCustomDialogEventListener.customDialogEvent(valueYouWantToSendBackToTheActivity); 
    dismiss(); 
   } 
  }); 
 } 
} 

在构造对话框时:

final CustomDialog dialog = new CustomDialog(this, new ICustomDialogEventListener() { 
 public void customDialogEvent(int value) { 
  //在这里就获取到了从对话框传回来的值 
 } 
}); 

大致使用流程就是这样的,下面就把我们上面的效果利用回调函数实现出来;
首先在对话框中新建一个回调函数(接口),并且在对话框的构造方法中把回调函数传进来:

public class CustomDialog extends Dialog implements View.OnClickListener { 
 //增加一个回调函数,用以从外部接收返回值 
 public interface ICustomDialogEventListener { 
  public void customDialogEvent(int id); 
 } 
 private ICustomDialogEventListener mCustomDialogEventListener; 
 private Context mContext; 
 private String mStr; 
 //把回调函数传进来 
 public CustomDialog(Context context, String str, ICustomDialogEventListener listener, int theme) { 
  super(context, theme); 
  mContext = context; 
  mStr = str; 
  mCustomDialogEventListener = listener; 
 } 
 ………… 
} 

然后在OnCreate() 函数中设定ImageView的点击事件,我们将CustomDialog类继承View.OnClickListener接口,对点击事件统一处理:

private void bindImageClickEvent(View layout){ 
 ImageView img1 = (ImageView)layout.findViewById(R.id.dialog_image1); 
 ImageView img2 = (ImageView)layout.findViewById(R.id.dialog_image2); 
 ImageView img3 = (ImageView)layout.findViewById(R.id.dialog_image3); 
 ImageView img4 = (ImageView)layout.findViewById(R.id.dialog_image4); 
 img1.setOnClickListener(this); 
 img2.setOnClickListener(this); 
 img3.setOnClickListener(this); 
 img4.setOnClickListener(this); 
} 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 LayoutInflater inflater = (LayoutInflater) mContext 
   .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
 View layout = inflater.inflate(R.layout.custom_dialog, null); 
 TextView tv = (TextView)layout.findViewById(R.id.dialog_text); 
 tv.setText(mStr); 
 bindImageClickEvent(layout);//绑定ImageView点击事件 
 this.setContentView(layout); 
} 

在OnCreate()中,首先将传进来的String字符串设置到对话框的TextView中;然后绑定各个ImageView的点击事件

然后 就是在OnClick中的处理:

主要是根据用户当前点击的ImageView的ID,把这个ImageView所用的图片的DrawableID返回给MainActivity,代码如下:

public void onClick(View view) { 
 int id = view.getId(); 
 int drawableID = -1; 
 switch (id){ 
  case R.id.dialog_image1: 
   drawableID = R.drawable.animal1; 
   break; 
  case R.id.dialog_image2: 
   drawableID = R.drawable.animal2; 
   break; 
  case R.id.dialog_image3: 
   drawableID = R.drawable.animal3; 
   break; 
  case R.id.dialog_image4: 
   drawableID = R.drawable.animal4; 
   break; 
 } 
 if (drawableID != -1) { 
  mCustomDialogEventListener.customDialogEvent(drawableID); 
 } 
 dismiss(); 
} 

这样就把对话框的构造过程讲完了,完整的对话框代码是这样的:

public class CustomDialog extends Dialog implements View.OnClickListener{ 
 //增加一个回调函数,用以从外部接收返回值 
 public interface ICustomDialogEventListener { 
  public void customDialogEvent(int id); 
 } 
 private ICustomDialogEventListener mCustomDialogEventListener; 
 private Context mContext; 
 private String mStr; 
 public CustomDialog(Context context) { 
  super(context); 
  mContext = context; 
 } 
 public CustomDialog(Context context, String str,ICustomDialogEventListener listener,int theme) { 
  super(context, theme); 
  mContext = context; 
  mStr = str; 
  mCustomDialogEventListener = listener; 
 } 
 private void bindImageClickEvent(View layout){ 
  ImageView img1 = (ImageView)layout.findViewById(R.id.dialog_image1); 
  ImageView img2 = (ImageView)layout.findViewById(R.id.dialog_image2); 
  ImageView img3 = (ImageView)layout.findViewById(R.id.dialog_image3); 
  ImageView img4 = (ImageView)layout.findViewById(R.id.dialog_image4); 
  img1.setOnClickListener(this); 
  img2.setOnClickListener(this); 
  img3.setOnClickListener(this); 
  img4.setOnClickListener(this); 
 } 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  LayoutInflater inflater = (LayoutInflater) mContext 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  View layout = inflater.inflate(R.layout.custom_dialog, null); 
  TextView tv = (TextView)layout.findViewById(R.id.dialog_text); 
  tv.setText(mStr); 
  bindImageClickEvent(layout); 
  this.setContentView(layout); 
 } 
 @Override 
 public void onClick(View view) { 
  int id = view.getId(); 
  int drawableID = -1; 
  switch (id){ 
   case R.id.dialog_image1: 
    drawableID = R.drawable.animal1; 
    break; 
   case R.id.dialog_image2: 
    drawableID = R.drawable.animal2; 
    break; 
   case R.id.dialog_image3: 
    drawableID = R.drawable.animal3; 
    break; 
   case R.id.dialog_image4: 
    drawableID = R.drawable.animal4; 
    break; 
  } 
  if (drawableID != -1) { 
   mCustomDialogEventListener.customDialogEvent(drawableID); 
  } 
  dismiss(); 
 } 
} 

下面就是构造对话框的过程了:

在MainAcitivity中,当点击一个按钮时,new一个ICustomDialogEventListener实例来接收传过来的值;

Button btn = (Button)findViewById(R.id.btn_pop_dialog_1); 
btn.setOnClickListener(new View.OnClickListener() { 
 @Override 
 public void onClick(View view) { 
  CustomDialog dialog = new CustomDialog(MainActivity.this,"From btn 1",new CustomDialog.ICustomDialogEventListener() { 
   @Override 
   public void customDialogEvent(int id) { 
    ImageView imageView = (ImageView)findViewById(R.id.main_image); 
    imageView.setImageDrawable(getResources().getDrawable(id)); 
   } 
  },R.style.dialog); 
  dialog.show(); 
 } 
}); 

在我们收到传过来的DrawableID时,把它设置gc主页面ImageVIew里显示出来,这就完成了我们上面的功能。

好了,本文就到这里了,有关对话框的东东基本上都讲完了,一般还是我们自定义对话框的时间比较多,所以这里讲的啰嗦了一点;


# android对话框dialog  # Android UI设计系列之自定义Dialog实现各种风格的对话框效果(7)  # Android中制作自定义dialog对话框的实例分享  # Android中AlertDialog各种对话框的用法实例详解  # Android中使用DialogFragment编写对话框的实例教程  # Android 自定义对话框 showSetPwdDialog  # 对话框  # 回调  # 自定义  # 标题栏  # 第一个  # 是这样  # 第二个  # 给大家  # 弹出  # 那一  # 在这里  # 对话框中  # 就把  # 如图  # 绑定  # 即在  # 新建一个  # 返回值  # 都是  # 是在 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: google浏览器怎么清理缓存_谷歌浏览器清除缓存加速详细步骤  Laravel如何升级到最新的版本_Laravel版本升级流程与兼容性处理  湖南网站制作公司,湖南上善若水科技有限公司做什么的?  uc浏览器二维码扫描入口_uc浏览器扫码功能使用地址  网站制作价目表怎么做,珍爱网婚介费用多少?  Laravel如何配置和使用缓存?(Redis代码示例)  Laravel Eloquent:优雅地将关联模型字段扁平化到主模型中  中国移动官方网站首页入口 中国移动官网网页登录  Laravel项目怎么部署到Linux_Laravel Nginx配置详解  Swift中swift中的switch 语句  如何选择PHP开源工具快速搭建网站?  怎么用AI帮你为初创公司进行市场定位分析?  高防服务器:AI智能防御DDoS攻击与数据安全保障  网站建设整体流程解析,建站其实很容易!  成都品牌网站制作公司,成都营业执照年报网上怎么办理?  ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】  jQuery中的100个技巧汇总  如何确认建站备案号应放置的具体位置?  LinuxShell函数封装方法_脚本复用设计思路【教程】  linux写shell需要注意的问题(必看)  北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?  Python3.6正式版新特性预览  CSS3怎么给轮播图加过渡动画_transition加transform实现【技巧】  Laravel如何获取当前用户信息_Laravel Auth门面获取用户ID  如何打造高效商业网站?建站目的决定转化率  如何在建站宝盒中设置产品搜索功能?  如何制作新型网站程序文件,新型止水鱼鳞网要拆除吗?  Firefox Developer Edition开发者版本入口  Laravel观察者模式如何使用_Laravel Model Observer配置  如何在景安云服务器上绑定域名并配置虚拟主机?  微博html5版本怎么弄发超话_超话进入入口及发帖格式要求【教程】  小米17系列还有一款新机?主打6.9英寸大直屏和旗舰级影像  Laravel如何实现API资源集合?(Resource Collection教程)  如何为不同团队 ID 动态生成多个非值班状态按钮  Laravel如何处理跨站请求伪造(CSRF)保护_Laravel表单安全机制与令牌校验  手机怎么制作网站教程步骤,手机怎么做自己的网页链接?  HTML 中动态设置元素 name 属性的正确语法详解  再谈Python中的字符串与字符编码(推荐)  广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?  网站制作大概多少钱一个,做一个平台网站大概多少钱?  如何在七牛云存储上搭建网站并设置自定义域名?  如何在云主机快速搭建网站站点?  使用Dockerfile构建java web环境  高防服务器租用指南:配置选择与快速部署攻略  java获取注册ip实例  Laravel如何使用Gate和Policy进行授权?(权限控制)  Laravel如何使用Socialite实现第三方登录?(微信/GitHub示例)  Laravel如何实现数据导出到CSV文件_Laravel原生流式输出大数据量CSV【方案】  Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)  Win11任务栏卡死怎么办 Windows11任务栏无反应解决方法【教程】