模拟ProgressBar下载进度显示

package com.example.administrator.myapplication;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private ProgressBar progress;
    private Button start;
    private TextView number;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(final Message msg) {
            super.handleMessage(msg);
            progress.setProgress(msg.arg1);
            number.setText(msg.arg1+"%");
            handler.postDelayed(updateProgress,100);
        }};
    private void initView() {
        number = findViewById(R.id.number);
        start = findViewById(R.id.start);
        progress = findViewById(R.id.progress);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handler.post(updateProgress);
            }
        });

    }

    Runnable updateProgress = new Runnable() {
        int i = 0;
        @Override
        public void run() {
            i+=1;
            Message message = handler.obtainMessage();
            message.arg1 = i;
            if(i<=100){
                handler.sendMessage(message);
            }
            if(i==100){
                handler.removeCallbacks(updateProgress);
            }
        }
    };
}




    
    
    

模拟ProgressBar下载进度显示_第1张图片

你可能感兴趣的:(Android控件,Android,Android面试)