package com.example.android_rogressbar;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ProgressBar pb_progress_bar;
private TextView tv_main_text;
private ImageView iv_main_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//根据ID获取控件
pb_progress_bar = (ProgressBar) findViewById(R.id.pb_progress_bar);
tv_main_text = (TextView) findViewById(R.id.tv_main_text);
}
//下载的方法
public void download(View view){
//启动线程
new MyThread().start();
}
Handler handler=new Handler(){
//接收消息,用于更新UI界面
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int i=msg.what;
tv_main_text.setText(i+"");
}
};
class MyThread extends Thread{
@Override
public void run() {
super.run();
for (int i = 0; i <= 100; i++) {
pb_progress_bar.setProgress(i);
//在子线程中发送消息
handler.sendEmptyMessage(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android_rogressbar.MainActivity">
<!--style:设置进度条的样式,这里为直线进度条-->
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pb_progress_bar"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_main_text"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载"
android:onClick="download"
/>
</LinearLayout>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有