zoukankan      html  css  js  c++  java
  • android中进度条的实现

    布局:

    <?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="com.example.administrator.progress.MainActivity">
    
        <ProgressBar
            android:id="@+id/progressBar"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </RelativeLayout>

    实现:

    package com.example.administrator.progress;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.widget.ProgressBar;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar);
            final Handler handler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    progressBar.setProgress(msg.what);
                }
            };
            final Thread thread = new Thread(){
                @Override
                public void run() {
                    super.run();
                    int index = 0;
                    int numloop = 1;
    
                    while (index < 100){
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.sendEmptyMessage(index);
                        index++;
                        if(index == 100){
                            while(index > 1){
                                try {
                                    Thread.sleep(100);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                handler.sendEmptyMessage(index);
                                index--;
                            }
                        }
                    }
                }
            };
            thread.start();
    
        }
    }

    转载:https://blog.csdn.net/wangbowj123/article/details/80454490

  • 相关阅读:
    The EF 6.x DbContextGenerator templates are not available for VS2010
    selenium定位tr及td,并获取其文本及属性
    selenium基本操作
    Git入门
    独热编码OneHotEncoder简介
    openCV中直方图均衡化算法的理解
    pyinstaller生成exe文件失败
    图像形态学运算
    对双边滤波的理解
    PyQt中对RadioButton分组
  • 原文地址:https://www.cnblogs.com/mlgm/p/9686452.html
Copyright © 2011-2022 走看看