2017-10-13 116 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(); 
    } 
}