Android编程自定义Dialog的方法分析

发布时间 - 2026-01-10 23:26:06    点击率:

本文实例讲述了Android编程自定义Dialog的方法。分享给大家供大家参考,具体如下:

功能:

android 提供给我们的只有2种Dialog 即 AlertDialog & ProgressDialog 但是 Dialog 有其自身的特点:1. 不是 Activity 2. 开销比 Activity 小得多

鉴于以上的优点 我们就有定制自己Dialog 的需求

原理:

1. android 系统提供了一个class: Dialog. 而且你可以把自己的工作放在"protected void onCreate(Bundle savedInstanceState)" 在里面先调用系统的"super.onCreate(savedInstanceState)" 其就会保证调用这个method.

2. 至于 Dialog 界面的定制 可以写一个xml 文件 然后 在 "void onCreate(Bundle)" 通过 "setContentView()" 来使之生效

3. Dialog 使用问题: 1. 弹出:show() 2. 取消:dismiss()

代码:

1. 创建一个 Dialog:

public class CustomDialog extends Dialog {
  public CustomDialog(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
  }
  protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_dialog);
    setTitle("Custom Dialog");
    TextView text = (TextView)findViewById(R.id.text);
    text.setText("Hello, this is a custom dialog!");
    ImageView image = (ImageView)findViewById(R.id.image);
    image.setImageResource(R.drawable.sepurple);
    Button buttonYes = (Button) findViewById(R.id.button_yes);
    buttonYes.setHeight(5);
    buttonYes.setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v) {
        // TODO Auto-generated method stub
        dismiss();
      }
    });
    Button buttonNo = (Button) findViewById(R.id.button_no);
    buttonNo.setSingleLine(true);
    buttonNo.setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v) {
        // TODO Auto-generated method stub
        dismiss();
      }
    });
  }
  //called when this dialog is dismissed
  protected void onStop() {
    Log.d("TAG","+++++++++++++++++++++++++++");
  }
}

2. Dialog 的使用:

public class CustomDialogUsage extends Activity {
  CustomDialog cd;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    cd = new CustomDialog(this);
    Button buttonYes = (Button) findViewById(R.id.main_button);
    buttonYes.setOnClickListener(new OnClickListener(){
      public void onClick(View v) {
        // TODO Auto-generated method stub
        cd.show();
      }
    });
  }
}

3. Dialog 的界面定制:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dp">
  <ImageView android:id="@+id/image" android:layout_width="wrap_content"
  android:layout_height="fill_parent" android:layout_marginRight="10dp"
  />
  <LinearLayout android:orientation="vertical"
  android:layout_width="fill_parent" android:layout_height="wrap_content"
  android:padding="5px">
    <TextView android:id="@+id/text" android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#FFF" />
    <LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5px">
      <Button android:id="@+id/button_yes"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text=" Yes " android:gravity="center"
      />
      <Button android:id="@+id/button_no"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text=" No " android:gravity="center"
      />
    </LinearLayout>
  </LinearLayout>
</LinearLayout>

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。


# Android  # 自定义  # Dialog  # Android自定义dialog可选择展示年月日时间选择栏  # Android中用Builder模式自定义Dialog的方法  # Android自定义Dialog实现文字动态加载效果  # Android 自定义Dialog 实例  # Android UI设计系列之自定义Dialog实现各种风格的对话框效果(7)  # Android中制作自定义dialog对话框的实例分享  # Android自定义dialog简单实现方法  # Android编程经典代码集锦(复制  # 粘贴  # 浏览器调用  # Toast显示  # 自定义Dialog等)  # Android编程中自定义dialog用法实例  # Android 去掉自定义dialog的白色边框的简单方法  # Android 自定义dialog的实现代码  # 自己的  # 进阶  # 就会  # 放在  # 相关内容  # 你可以  # 就有  # 给我们  # 感兴趣  # 得多  # 给大家  # 弹出  # 在里面  # 使之  # 更多关于  # 解决方法  # 所述  # 创建一个  # 程序设计 


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


相关推荐: 如何在阿里云通过域名搭建网站?  PythonWeb开发入门教程_Flask快速构建Web应用  ChatGPT 4.0官网入口地址 ChatGPT在线体验官网  如何在建站主机中优化服务器配置?  JavaScript如何实现类型判断_typeof和instanceof有什么区别  Laravel的契約(Contracts)是什么_深入理解Laravel Contracts与依赖倒置  Laravel如何实现多级无限分类_Laravel递归模型关联与树状数据输出【方法】  奇安信“盘古石”团队突破 iOS 26.1 提权  Laravel模型事件有哪些_Laravel Model Event生命周期详解  jQuery 常见小例汇总  网站建设保证美观性,需要考虑的几点问题!  如何快速搭建支持数据库操作的智能建站平台?  php json中文编码为null的解决办法  Laravel如何使用API Resources格式化JSON响应_Laravel数据资源封装与格式化输出  Laravel Pest测试框架怎么用_从PHPUnit转向Pest的Laravel测试教程  Laravel事件监听器怎么写_Laravel Event和Listener使用教程  如何在Tomcat中配置并部署网站项目?  如何在Windows 2008云服务器安全搭建网站?  Python结构化数据采集_字段抽取解析【教程】  bootstrap日历插件datetimepicker使用方法  Laravel如何为API编写文档_Laravel API文档生成与维护方法  专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设?  深圳防火门网站制作公司,深圳中天明防火门怎么编码?  香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化  韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南  如何用AWS免费套餐快速搭建高效网站?  使用豆包 AI 辅助进行简单网页 HTML 结构设计  宙斯浏览器文件分类查看教程 快速筛选视频文档与图片方法  如何快速辨别茅台真假?关键步骤解析  Laravel怎么实现前端Toast弹窗提示_Laravel Session闪存数据Flash传递给前端【方法】  Laravel模型关联查询教程_Laravel Eloquent一对多关联写法  企业在线网站设计制作流程,想建设一个属于自己的企业网站,该如何去做?  如何用景安虚拟主机手机版绑定域名建站?  黑客如何利用漏洞与弱口令入侵网站服务器?  微信小程序 wx.uploadFile无法上传解决办法  谷歌Google入口永久地址_Google搜索引擎官网首页永久入口  Laravel如何与Pusher实现实时通信?(WebSocket示例)  Windows Hello人脸识别突然无法使用  javascript日期怎么处理_如何格式化输出  关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)  Laravel如何使用Seeder填充数据_Laravel模型工厂Factory批量生成测试数据【方法】  公司门户网站制作流程,华为官网怎么做?  如何用搬瓦工VPS快速搭建个人网站?  百度浏览器ai对话怎么关 百度浏览器ai聊天窗口隐藏  Laravel如何实现邮箱地址验证功能_Laravel邮件验证流程与配置  Laravel控制器是什么_Laravel MVC架构中Controller的作用与实践  大型企业网站制作流程,做网站需要注册公司吗?  香港服务器部署网站为何提示未备案?  如何在建站之星网店版论坛获取技术支持?  Win11怎么关闭专注助手 Win11关闭免打扰模式设置【操作】