如何添加进程按钮以显示按钮上的下载进度?

时间:2017-10-13 08:40:04

标签: android pdf download android-progressbar fileinputstream

我的项目是下载PDF然后查看它。 https://stackoverflow.com/a/24748227/8769785

我想在下载按钮上显示下载进度![“下载”按钮从提供的URL下载,“查看”按钮打开文件]

enter image description here

http://blog.rhesoft.com/2015/12/29/tutorial-android-process-button-animated-buttons-on-android-studio/

我想在下载文件时(“进度”)在“下载按钮”中添加“提交处理按钮”动画。

我的问题是我不知道如何在下载按钮上获取动画(进度)。

在项目中有一个“Filedownloader”类 我已经尝试了主要活动的点击监听器,但我仍然无法获得按钮上的进度。我无法从网址获得进展 我是Android编程的新手。

代码就像这样

//获取按钮视图         SubmitProcessButton btnSubmit =(SubmitProcessButton)findViewById(R.id.btnSubmit);

    //start with progress = 0
    btnSubmit.setProgress(0);

    //to test the animations, when we touch the button it will start counting
    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SubmitProcessButton btn = (SubmitProcessButton) view;
            // we add 25 in the button progress each click
            if(btn.getProgress() < 100){
                btn.setProgress(btn.getProgress() + 25);
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

使用ProgressBar,您可以显示下载进度。

public class MainActivity extends ActionBarActivity {
Button b1;
private ProgressDialog progress;

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  b1 = (Button) findViewById(R.id.button2);
}

public void download(View view){
  progress=new ProgressDialog(this);
  progress.setMessage("Downloading Music");
  progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  progress.setIndeterminate(true);
  progress.setProgress(0);
  progress.show();

  final int totalProgressTime = 100;
  final Thread t = new Thread() {
     @Override
     public void run() {
        int jumpTime = 0;

        while(jumpTime < totalProgressTime) {
           try {
              sleep(200);
              jumpTime += 5;
              progress.setProgress(jumpTime);
           } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
           }
        }
     }
  };
  t.start();
  }
 }