ProgressBar_test.class
package com.example.administrator.ten_9;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView; /**
* Created by Administrator on 2015/10/9 0009.
*/
public class ProgressBar_test extends Activity {
private ProgressBar progressBar;
private my my;
private TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progressbar);
progressBar = (ProgressBar) findViewById(R.id.pb);
textView = (TextView) findViewById(R.id.textjindu);
my = new my();
my.execute();
} @Override
protected void onPause() {
super.onPause();
//生命周期绑定
//AsyncTask不为空而且是在Running的
if (my != null && my.getStatus() == AsyncTask.Status.RUNNING){
//cancel方法只是将对应的AsyncTask标记为cancel状态,并不是真正的取消线程执行。
my.cancel(true);
}
} class my extends AsyncTask<Void,Integer,Void>{ @Override
protected Void doInBackground(Void... params) {
//模拟进度更新
for(int i = 0; i <= 100 ; i++){
//判断是否被标记为 cancel 是 就break出来
if(isCancelled()){
break;
}
publishProgress(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} return null;
} @Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.d("xys", String.valueOf(values[0]));
//判断是否被标记为 cancel 是 就直接返回
if(isCancelled()){
return;
}
//获取更新进度
textView.setText(values[0]+"");
progressBar.setProgress(values[0]);
}
}
}

  

progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="16dp"
android:layout_width="match_parent" android:layout_height="match_parent"> <ProgressBar
android:layout_centerInParent="true"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pb" />
<TextView
android:id="@+id/textjindu"
android:gravity="center_horizontal"
android:textSize="20sp"
android:text="0"
android:layout_below="@id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </RelativeLayout>

  

MainActivity.class
public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); MyAsyncTask myAsyncTask = new MyAsyncTask();
//start 异步处理myAsyncTask
myAsyncTask.execute();
} public void LoadImage(View view){
startActivity(new Intent(this,ImageTest.class)); }
public void LoadProgress(View view){
startActivity(new Intent(this,ProgressBar_test.class));
}
}

  mainactivity.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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button
android:id="@+id/bt1"
android:text="跳转"
android:onClick="LoadImage"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:layout_below="@id/bt1"
android:id="@+id/bt2"
android:text="跳转2"
android:onClick="LoadImage"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </RelativeLayout>

  

最新文章

  1. Shell编程和Vim操作
  2. 在virtualbox中安装CentOS-7
  3. ansible非root用户批量修改root密码
  4. T-SQL 常用语句
  5. git的一些指令
  6. C语言的位运算
  7. [原创]SSIS-WMI 数据读取器任务:监控物理磁盘空间
  8. HDU 4893 线段树
  9. ORACLE CLIENT客户端安装步骤详解
  10. Eclipse 卸载插件
  11. 史上最全的iOS各种设备信息获取总结
  12. Zxing 的集成 ---- Maven 对应 Gradle 的写法
  13. 网络基础tcp/ip协议四
  14. Ubuntu 16.04安装httpd
  15. linux驱动(续)
  16. redis 安装配置 [转]
  17. ORA-01917: user or role &#39;PDB_DBA&#39; does not exist
  18. socket阻塞与非阻塞,同步与异步、I/O模型
  19. hdu3974 Assign the task【线段树】
  20. 【微信小程序】——wxss引用外部CSS文件及iconfont

热门文章

  1. Java泛型(4):泛型与匿名内部类
  2. NetCore 对Json文件的读写操作
  3. Claymore&#39;s Dua Miner挖矿教程
  4. 工具 --- Git
  5. vs2017安装过程中下载不动的一种情况
  6. css的优先级权重
  7. 2019icpc南京网络赛
  8. firewall-cmd 的简单使用 进行端口转发的使用
  9. 啃掉Hadoop系列笔记(03)-Hadoop运行模式之本地模式
  10. MySQL线程池(THREAD POOL)的原理