2017-10-13 115 views
1

我的项目是下载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

使用'AsyncTask'来显示从服务器下载数据时的进度。 –

+0

在该项目中有一个filedownloader类可帮助下载文件。我不确定在哪里添加setprogress。 –

+0

使用对话框甚至TextView,跟踪进度以及更新值可以是一个解决方案 – rmjoia

回答

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(); 
    } 
}