android设计 实现窗体启动时,显示一个水平进度条,当进行完成后,隐藏该进度条,并显示一张图 片。
16lz
2021-01-23
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ProgressBar pb = (ProgressBar) findViewById(R.id.pb1); final TextView tv = (TextView) findViewById(R.id.tv); final ImageView iv = (ImageView) findViewById(R.id.iv); pb.setMax(100); pb.setProgress(0); new Thread() { public void run() { // 模拟耗时任务 int index = 0; while (index++ < 200) { try { Thread.sleep((int) (Math.random() * 200)); } catch (InterruptedException e) { e.printStackTrace(); } // 修改值 // 做了UI的处理,所以直接可以再子线程中修改UI pb.setProgress(index); // 需要使用UI线程 runOnUiThread(new Runnable() { @Override public void run() { tv.setText(pb.getProgress() + "/" + pb.getMax()); } }); } runOnUiThread(new Runnable() { public void run() { pb.setVisibility(ProgressBar.GONE);//设置ProgressBar隐藏 tv.setVisibility(TextView.INVISIBLE);//设置TextView隐藏 iv.setVisibility(TextView.VISIBLE);//显示图片 } }); }; }.start(); }}
xml的设计如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.xykj.id05_10_11work7.MainActivity" > <ProgressBar android:id="@+id/pb1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@id/pb1" android:layout_below="@id/pb1" android:text="0/100" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/img01" android:id="@+id/iv" android:visibility="invisible" />RelativeLayout>
需要注意的是开启线程的时候要符合android系统的规定
更多相关文章
- 跑马灯结合TextSwitcher实现系统公告栏
- Android 通过按钮弹出系统菜单(通过Button显示菜单)转
- 打开android 系统联系人
- android的系统属性设置和应用
- Android Build系统常见变量说明
- Android检测系统中是否存在某进程
- android中系统日期时间的获取
- 系统关闭GPRS数据流量和 GPS的方法
- Android(4.4)音频系统之mediaserver服务启动