Android百日程序:进度条对话框实现
来源:互联网 发布:python 视频 上传 编辑:程序博客网 时间:2024/04/30 02:08
显示由Activity管理的dialog。
这种dialog有多种多样,其中比较常见的是loading的时候,显示的一个loading进度条。
Android显示这样的进度条还是非常方便的,因为有现成的模块可以调用。
首先看看本程序的效果吧:
1 主界面:
2 点击这个按钮之后,显示:
进度条到了100的时候就会自动关闭,当然这里是模拟下载,真实的下载算法还需要继续完善,不过也是很简单的算法了,不算是难点。
点击Cancel或者OK按钮也可以调用函数,进行有需要的操作,这里直接显示一个Toast(简单短暂的对话框)提示。不截图了。
步骤:
新建项目之后,用以下代码做出主界面:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/dialog_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:onClick="onClick" android:text="@string/dialog_button" /> </LinearLayout>
然后是逻辑代码:
package su.dialog.dialog;import android.app.Activity;import android.app.Dialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {ProgressDialog progressDialog;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void onClick(View v) {showDialog(1);progressDialog.setProgress(0);new Thread(new Runnable() {public void run() {for (int i = 1; i <= 15; i++) {try {Thread.sleep(1000);progressDialog.incrementProgressBy((int) (100.f / 15.f));} catch (InterruptedException e) {e.printStackTrace();}}progressDialog.dismiss();}}).start();}@Overrideprotected Dialog onCreateDialog(int id) {progressDialog = new ProgressDialog(this);progressDialog.setIcon(R.drawable.ic_launcher);progressDialog.setTitle("Downloading files...");progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {Toast.makeText(getBaseContext(), "OK clicked!",Toast.LENGTH_SHORT).show();}});progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {Toast.makeText(getBaseContext(), "Cancel clicked!",Toast.LENGTH_SHORT).show();}});return progressDialog;}}
showDialog()函数已经是deprecated的函数了,可以参照:http://stackoverflow.com/questions/10285047/showdialog-deprecated-whats-the-alternative
知识点:
1 点击事件是在xml中声明的了,就是这句:android:onClick = "onClick";后面的"onClick"就是自己定义的函数,可以为任意名字,只要xml的名字和逻辑代码中的名字一致就可以驱动点击事件了。
2 模块ProgressDialog类有点复杂,其实也是一些属性的设置和函数调用罢了,理解起来不难,细心看代码就行了。
1 0
- Android百日程序:进度条对话框实现
- Android百日程序:GridView实现相册效果
- Android百日程序:嵌入Fragment
- Android百日程序: Activity的生命期
- Android百日程序:Intent传递数据
- Android百日程序:高效载入大图片
- Android百日程序:Intent传递数据
- Android百日程序:绘画程序-画手指路径
- Android ProgressDialog进度条对话框
- android进度条对话框Demo
- 【Android】对话框之进度条
- 【Android】进度条对话框ProgressDialog
- Android开发:进度条对话框
- Android--圆形进度条对话框
- Android--水平进度条对话框
- Android ProgressDialog 进度条对话框
- Android百日程序 开篇章:Intent打开网页
- Android百日程序:使用Intent回传结果
- stm32 外部中断的使用(含实例)
- java project 导出介绍
- MVC页面常见三种传值方式——ViewData,ViewBag,TempData
- 原型模式(prototype)和单例模式(默认情况下)
- ARM嵌入式系统中断向量表的动态配置
- Android百日程序:进度条对话框实现
- 集合
- 解决undefined reference to `pthread_atfork'
- Java8 新特性学习
- mysql 的delete from 子查询限制
- 在窗口中调用两块板的内容
- 栈和队列的知识
- 从头认识C—枚举类型
- arm+uClinux的嵌入式系统的开发