- 浏览: 68196 次
- 性别:
- 来自: Mercury
-
最新评论
Dialog是Android常用的对话框控件。AlertDialog对话框是Dialog的子类,它提供一个图标,一个标题,一个文本和3个按钮。以前用过的DatePickerDialog和TimePickerDialog是Dialog的间接子类。ProgressDialog是AlertDialog类的一个扩展,可以为一个未定义进度的任务显示一个旋转轮形状的进度动画,或者为一个指定进度的任务显示一个进度条。
一、AlertDialog使用方法
1.创建对话框
setIcon:创建图标;setTitle:设置标题;setMessage:设置内容;setPositiveButton:响应Yes/Ok的点击,setNeutralButton:响应中立行为的点击,setNegativeButton:响应No/Cancel的点击。都需要传进去一个OnClickListener的对象。
dialog = new AlertDialog.Builder(this) .setIcon(android.R.drawable.btn_star).setTitle("这是一个警告对话框") .setMessage("确定要退出吗?") .setPositiveButton("是", dbl) .setNegativeButton("否", dbl) .setNeutralButton("取消", dbl).create();
2.准备显示对话框,这是一个可选的回调函数。 如果你想在每一次对话框被打开时改变它的任何属性,你可以定义这个方法。这个方法在每次打开对话框时被调用,而onCreateDialog(int) 仅在对话框第一次打开时被调用。如果你不定义onPrepareDialog(),那么这个对话框将保持和上次打开时一样。这个方法也被传递以对话框的ID,和在onCreateDialog()中创建的对话框对象。即对话框对象每次运行之后创建一次,不会每次调用都创建,所以只能在onPrepareDialog中更改。
// 每次显示对话框之前会被调用 @Override public void onPrepareDialog(int id, Dialog dialog){ Toast.makeText(this, "onPrepareDialog方法被调用", Toast.LENGTH_LONG).show(); super.onPrepareDialog(id, dialog); }
3. 添加对话框中按钮点击事件的监听器,点击取消按钮Dialog.BUTTON_NEUTRAL不会调用OnCancelListener
// 对话框按钮点击事件监听器 class DialogButtonListener implements DialogInterface.OnClickListener{ public void onClick(DialogInterface dialog, int which) { switch (which) { case Dialog.BUTTON_NEGATIVE: Toast.makeText(AlertDialogActivity.this, "不退出", Toast.LENGTH_LONG).show(); break; case Dialog.BUTTON_NEUTRAL: Toast.makeText(AlertDialogActivity.this, "取消", Toast.LENGTH_LONG).show(); break; case Dialog.BUTTON_POSITIVE: Toast.makeText(AlertDialogActivity.this, "退出", Toast.LENGTH_LONG).show(); break; } } }
4.显示对话框
showDialog(DIALOG_ID);
同样也可以消除对话框和移除对话框,移除对话框会同时调用注册的OnCancelListener,即删除对话框对象,而消除对话框则不会删除dialog对象,下次调用的时候还是这个对象:
//消除对话框 dismissDialog(DIALOG_ID); //移除对话框 removeDialog(DIALOG_ID);
完整代码:
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/showdialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="弹出对话框" /> <Button android:id="@+id/deletedialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关闭对话框" /> <Button android:id="@+id/removedialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="移除对话框" /> </LinearLayout>
AlertDialogActivity.java
package com.android.activity; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class AlertDialogActivity extends Activity { private Button showDialog = null; private Button deleteDialog = null; private Button removeDialog = null; private Dialog dialog = null; DialogButtonListener dbl = null; private static final int DIALOG_ID = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); showDialog = (Button)findViewById(R.id.showdialog); deleteDialog = (Button)findViewById(R.id.deletedialog); removeDialog = (Button)findViewById(R.id.removedialog); dbl = new DialogButtonListener(); showDialog.setOnClickListener(new ShowButtonListener()); deleteDialog.setOnClickListener(new DeleteButtonListener()); removeDialog.setOnClickListener(new RemoveButtonListener()); } // 创建会话框时调用 @Override public Dialog onCreateDialog(int id) { Toast.makeText(this, "onCreateDialog方法被调用", Toast.LENGTH_LONG).show(); dialog = new AlertDialog.Builder(this) .setIcon(android.R.drawable.btn_star) .setTitle("这是一个警告对话框!") .setMessage("确定要退出吗?") .setPositiveButton("是", dbl) .setNegativeButton("否", dbl) .setNeutralButton("取消", dbl).create(); return dialog; } // 每次显示对话框之前会被调用 @Override public void onPrepareDialog(int id, Dialog dialog){ Toast.makeText(this, "onPrepareDialog方法被调用", Toast.LENGTH_LONG).show(); super.onPrepareDialog(id, dialog); } // 对话框按钮点击事件监听器 class DialogButtonListener implements DialogInterface.OnClickListener{ public void onClick(DialogInterface dialog, int which) { switch (which) { case Dialog.BUTTON_NEGATIVE: Toast.makeText(AlertDialogActivity.this, "不退出",Toast.LENGTH_LONG).show(); break; case Dialog.BUTTON_NEUTRAL: Toast.makeText(AlertDialogActivity.this, "取消",Toast.LENGTH_LONG).show(); break; case Dialog.BUTTON_POSITIVE: Toast.makeText(AlertDialogActivity.this, "退出",Toast.LENGTH_LONG).show(); break; } } } //弹出对话框按钮监听器 class ShowButtonListener implements OnClickListener{ public void onClick(View v) { //显示对话框 showDialog(DIALOG_ID); } } //关闭对话框按钮监听器 class DeleteButtonListener implements OnClickListener{ public void onClick(View v) { //关闭对话框 dismissDialog(DIALOG_ID); } } //移除对话框按钮监听器 class RemoveButtonListener implements OnClickListener{ public void onClick(View v) { //移除对话框 ,会同时调用注册的OnCancelListener() removeDialog(DIALOG_ID); } } }
运行结果:
二、ProgressDialog使用方法
Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:text="圆形进度对话框" android:id="@+id/spinnerprogress" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:text="长方形进度对话框" android:id="@+id/horizonalprogress" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
ProgressDialogActivity.java
package com.android.activity; import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ProgressDialogActivity extends Activity { private Button spinnerProgress = null; private Button horizonalProgress = null; private ProgressDialog progressDialog = null; private ProgressDialog progressDialog2 = null; int count = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); spinnerProgress = (Button)findViewById(R.id.spinnerprogress); horizonalProgress = (Button)findViewById(R.id.horizonalprogress); spinnerProgress.setOnClickListener(new SpinnerProgress()); horizonalProgress.setOnClickListener(new HorizonalProgress()); } class SpinnerProgress implements OnClickListener{ public void onClick(View view) { //创建ProgressDialog对象 progressDialog = new ProgressDialog( ProgressDialogActivity.this); //设置进度条风格,风格为圆形,旋转的 progressDialog.setProgressStyle( ProgressDialog.STYLE_SPINNER); //设置ProgressDialog 标题 progressDialog.setTitle("下载"); //设置ProgressDialog 提示信息 progressDialog.setMessage("这是一个圆形进度条对话框"); //设置ProgressDialog 标题图标 progressDialog.setIcon(android.R.drawable.btn_star); //设置ProgressDialog 的进度条是否不明确 progressDialog.setIndeterminate(false); //设置ProgressDialog 是否可以按退回按键取消 progressDialog.setCancelable(true); //设置取消按钮 progressDialog.setButton("取消", new ProgressDialogButtonListener()); // 让ProgressDialog显示 progressDialog.show(); } } class ProgressDialogButtonListener implements DialogInterface.OnClickListener{ public void onClick(DialogInterface dialog, int which) { //点击"取消按钮"取消对话框 dialog.cancel(); } } class HorizonalProgress implements OnClickListener{ public void onClick(View view) { progressDialog2 = new ProgressDialog( ProgressDialogActivity.this); //设置进度条风格,风格长方形的 progressDialog2.setProgressStyle( ProgressDialog.STYLE_HORIZONTAL); progressDialog2.setTitle("下载"); progressDialog2.setMessage("这是一个长方形进度条对话框"); progressDialog2.setIcon(android.R.drawable.btn_star); progressDialog2.setIndeterminate(false); progressDialog2.setCancelable(true); progressDialog2.setButton("取消", new ProgressDialogButtonListener()); progressDialog2.show(); Thread t = new Thread(new ProgressThread()); t.start(); } } class ProgressThread implements Runnable{ public void run() { try{ while (count <= 100){ // 由线程来控制进度。 progressDialog2.setProgress(count++); Thread.sleep(100); } progressDialog2.setMessage("下载完成"); }catch (InterruptedException e){ progressDialog2.cancel(); } } } }
运行结果:
发表评论
文章已被作者锁定,不允许评论。
-
Android39_Clock和TimePicker
2011-11-14 00:08 2376一、AnalogClock和DigitalClock ... -
Android38_ImageView和Gallery
2011-11-14 00:07 3634一、ImageView使用方法 ImageVi ... -
Android37_JSON数据解析
2011-11-08 00:14 2368一、JSON介绍 JSON(JavaSc ... -
Android36_Animations使用(四)
2011-11-08 00:14 3433一、LayoutAnimationsContrlller ... -
Android35_Animations使用(三)
2011-11-08 00:13 2663一、AnimationSet的具体使用方法 ... -
Android34_Animations使用(二)
2011-11-08 00:12 1970在代码中使用Animations可以很方便的调试、运行 ... -
Android33_Animations使用(一)
2011-11-08 00:12 2297一、Animations介绍 Anima ... -
Android31_AppWidget使用(二)
2011-11-05 00:09 2522一、PendingIntent介绍 PendingIn ... -
Android30_AppWidget使用(一)
2011-11-05 00:08 2280一、App Widget定义 App ... -
Android32_Notification用法
2011-11-05 00:09 1896Android系统的状态栏(Status Bar)中有一 ... -
Android29_SeekBar和RatingBar
2011-11-02 23:21 2134一、使用SeekBar步骤: SeekB ... -
Android28_ExpandableListActivity
2011-11-02 23:21 1503ExpandableListActivity就是可扩展的 ... -
Android27_AutoCompleteTextView
2011-11-02 23:21 1107一、创建AutoCompleteTextView ... -
Android26_DatePicker
2011-11-02 23:20 1794一、DatePicker和DatePickerDialo ... -
Android25_Spinner使用方法
2011-11-02 23:20 2832一、创建Spinner的步骤 1.在布局 ... -
Android24_Service初步
2011-10-18 22:27 1021一、Service概念 ... -
Android23_Socket编程
2011-10-18 22:19 1528一、什么是Socket Socket是基 ... -
Android22_WIFI网络操作
2011-10-18 22:12 1711一、什么是WIFI WIFI就是一种无线 ... -
Android21_广播机制(二)
2011-10-18 22:00 1026一、注册BroadcastReceiver的方法 ... -
Android20_广播机制(一)
2011-10-18 21:48 1090一、Android广播机制介绍 Android:操作系统 ...
相关推荐
项目资源包含:可运行源码+数据集+文档 python + numpy, pandas, matplotlib, pyecharts, wordcloud 适用人群:学习不同技术领域的小白或进阶学习者;可作为课程设计、大作业、工程实训或初期项目立项。 数据来源:数据集taxis.csv从网络下载 数据清洗:异常值与缺失值的处理:有一些数据distance(乘车距离)为零而且上下车地点为空,还有些一些数据的payment(支付方式)为空。 数据预处理:将列名更改成中文 标准化与归一化: 数据分析: 数据可视化:
TypeScript 入门教程
人脸识别项目实战
本资源汇总了 历届全国电子设计竞赛(电赛)真题+模拟题,涵盖 电路设计、嵌入式系统、信号处理、自动控制等核心考点,并提供详细解析及综合测评,帮助参赛者高效备赛、查漏补缺、提升实战能力。 适用人群: 适合 准备参加电子设计竞赛的大学生、电赛爱好者、电子信息类相关专业的学生,以及希望提高电子设计和电路分析能力的工程师。 能学到什么: 电赛考察重点:熟悉往届竞赛的命题方向及考核重点。 电路设计与仿真:提升模拟电路、数字电路、单片机等核心技能。 问题分析与解决能力:通过综合测评找到薄弱点并针对性提升。 实战经验:掌握竞赛策略,提高应试效率和设计能力。 阅读建议: 建议先 通读真题,了解题型与解题思路,然后 结合模拟题实战演练,查找不足并通过测评强化练习,逐步提升竞赛能力。
2024人工智能如何塑造未来产业:AI对各行业组织带来的的变革研究研究报告.pdf
人脸识别项目源码实战
给大家分享一套课程——Vulkan原理与实战课程
c语言学习
海豚鲸鱼数据集 5435张图 正确识别率可达92.6% 可识别:海豚 虎鲸 蜥蜴 海豹 鲨鱼 龟 支持yolov8格式标注
答谢中书书教学设计.docx
人脸识别项目源码实战
c语言学习
人脸识别项目源码实战
人脸识别项目实战
本美发门店管理系统有管理员和用户两个角色。用户功能有项目预定管理,产品购买管理,会员充值管理,余额查询管理。管理员功能有个人中心,用户管理,美容项目管理,项目类型管理,项目预定管理,产品库存管理,产品购买管理,产品入库管理,会员卡管理,会员充值管理,余额查询管理,产品类型管理,系统管理等。因而具有一定的实用性。 本站是一个B/S模式系统,采用SSM框架,MYSQL数据库设计开发,充分保证系统的稳定性。系统具有界面清晰、操作简单,功能齐全的特点,使得美发门店管理系统管理工作系统化、规范化。本系统的使用使管理人员从繁重的工作中解脱出来,实现无纸化办公,能够有效的提高美发门店管理系统管理效率。 关键词:美发门店管理系统;SSM框架;MYSQL数据库;Spring Boot 1系统概述 1 1.1 研究背景 1 1.2研究目的 1 1.3系统设计思想 1 2相关技术 2 2.1 MYSQL数据库 2 2.2 B/S结构 3 2.3 Spring Boot框架简介 4 3系统分析 4 3.1可行性分析 4 3.1.1技术可行性 4 3.1.2经济可行性 5 3.1.3操作可行性 5 3.2系
内容概要:本文档介绍了基于SSA-CNN-GRU麻雀算法优化卷积门控循环单元数据分类预测的详细项目实例,重点讲述了该项目的背景、目标、挑战与解决方案、技术特点、应用领域等方面的内容。文档详细记录了从项目启动、数据预处理、算法设计(SSA优化CNN-GRU模型)、构建与评估模型到实现美观的GUI界面整个过程,并讨论了防止过拟合的技术如正则化、早停和超参数优化。另外还涵盖了项目扩展的可能性、部署和应用策略、需要注意的地方以及未来改进的方向。全文强调了模型的泛化能力和计算效率,展示了该混合算法模型在实际应用中的优越性能。 适合人群:具备一定的Python编程经验及机器学习基础知识的研究人员和技术人员;对深度学习、智能优化算法及实际应用感兴趣的学者和从业者;寻求提升数据分析和预测准确性的金融分析师、数据科学家等相关专业人士。 使用场景及目标:本文档非常适合用作学习和参考资料,以掌握如何将SSA、CNN与GRU三种先进技术结合起来进行复杂的分类和预测问题求解。具体应用场景包括但不限于以下几个方面:金融领域——股票价格预测;医疗保健领域——辅助诊断;工业制造——预防性维护;智能家居——个性化服务;以及其他涉及到时序数据分析和多模态数据处理的场合。文档既包含了理论知识又提供了完整的源代码示例,可以帮助读者理解算法原理并通过实践中加深对其的认识。 其他说明:该项目不仅仅是关于算法的设计实现,更是有关于系统的整体架构规划以及工程上的考量,比如环境准备(确保环境洁净、必要包的安装等)、数据准备、GPU配置支持等等。同时文中给出了详细的代码片段,方便开发者理解和复现实验成果。值得注意的是,虽然文中提供了一套通用解决方案,但在真实场景下还需要针对性的调整参数或修改网络结构来达到最好的性能效果。此外,对于追求更高的预测精度或解决更大规模的问题,作者建议进一步探索深度强化学习等高级技术和多任务学习策略,并且考虑使用增量学习让模型能够适应新数据而不必重新训练整个模型。最后提到安全性和隐私保护也是项目实施过程中的重要因素,要妥善保管用户的敏感信息并且做到合法合规地收集和使用数据。
人脸识别项目实战
人脸识别项目实战
水下垃圾检测数据集,基于voc和yolo标注的两种格式,共23,056个文件,已经划分了训练集和验证集、测试集。并且提供了真实水下的视频数据,可以用作视频推理
(参考GUI)MATLAB车辆检测.zip