现在的位置: 首页 > 操作系统 > 正文

Android中通过线程实现更新ProgressDialog(对话进度条)

2020年02月12日 操作系统 ⁄ 共 1580字 ⁄ 字号 评论关闭

作为开发者我们需要经常站在用户角度考虑问题,比如在应用商城下载软件时,当用户点击下载按钮,则会有下载进度提示页面出现,现在我们通过线程休眠的方式模拟下载进度更新的演示,如图(这里为了截图方便设置对话进度条位于屏幕上方):

layout界面代码(仅部署一个按钮):

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下载"//真正项目时建议将文本资源统一定义配置在res下的strings.xml中 android:onClick="begin"/></LinearLayout>

Java代码实现(通过线程实现模拟下载进度更新)

public class ProgressBarDemo extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.progressbar); } public void begin(View v) { //实例化进度条对话框(ProgressDialog) final ProgressDialog pd = new ProgressDialog(this); pd.setTitle("请稍等"); //设置对话进度条样式为水平 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置提示信息 pd.setMessage("正在玩命下载中......"); //设置对话进度条显示在屏幕顶部(方便截图) pd.getWindow().setGravity(Gravity.TOP); pd.setMax(100); pd.show();//调用show方法显示进度条对话框 //使用匿名内部类实现线程并启动 new Thread(new Runnable() { int initial = 0;//初始下载进度 @Override public void run() { while(initial<pd.getMax()){//设置循环条件 pd.setProgress(initial+=40);//设置每次完成40 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } pd.dismiss();//进度完成时对话框消失 } }).start(); }}

本文永久更新链接地址:http://www.xuebuyuan.com/Linux/2016-12/138650.htm

以上就上有关Android中通过线程实现更新ProgressDialog(对话进度条)的相关介绍,要了解更多ProgressDialog进度条,ProgressDialog,Android中通过线程实现更新ProgressDialog(对话进度条),编程,Linux编程,Linux Shell,Android,Android教程,JAVA,C语言,Python,HTML5内容请登录学步园。

抱歉!评论已关闭.