首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Android ProgressDialog简单实例

Android ProgressDialog简单实例

作者头像
云未归来
发布2025-07-18 11:02:15
发布2025-07-18 11:02:15
920
举报

布局文件:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/activity_main"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context="test.future.com.test.MainActivity">     <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/tv"         android:text="开始...." />     <Button         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_below="@id/tv"         android:text="开始"         android:id="@+id/btn" /> </RelativeLayout>

主函数:

public class MainActivity extends AppCompatActivity {    private TextView tv;     private ProgressDialog pgd;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         this.setTitle("你好");         setContentView(R.layout.activity_main);         tv = (TextView)findViewById(R.id.tv);         findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {    pgd=ProgressDialog.show(MainActivity.this,"正在拼命加载","请稍后...",true,false);                 new Thread(){                     @Override                     public void run() {                         super.run();                         working();                        handler.sendEmptyMessage(0);                     }                 }.start();             }         });     }     private Handler handler=new Handler() {         @Override         public void handleMessage(Message msg) {             super.handleMessage(msg);             pgd.dismiss();             tv.setText("已完成!");         }     };     private static void  working(){         try {             Thread.sleep(4 * 1000);         } catch (Exception e) {             // TODO: handle exception         }     } }

运行结果:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-07-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档