Android實戰教程第二篇之簡單實現兩種進度條效果_Android

來源:互聯網
上載者:User

本文執行個體實現點擊按鈕類比進度條下載進度,“下載”完成進度條消失,供大家參考,具體內容如下

代碼如下:
xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  > <TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="@string/hello"  /> <ProgressBar  android:id="@+id/firstBar"  style="?android:attr/progressBarStyleHorizontal"  android:layout_width="200dp"  android:layout_height="wrap_content"  android:visibility="gone"  /> <ProgressBar  android:id="@+id/secondBar"  style="?android:attr/progressBarStyle"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:visibility="gone"  /> <Button  android:id="@+id/myButton"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="begin"  /> </LinearLayout> 

Activity:

package ydl.progressbar;  import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar;  public class ProgressBarTest extends Activity {  /** Called when the activity is first created. */  //聲明變數  private ProgressBar firstBar =null;  private ProgressBar secondBar = null;  private Button myButton = null;  private int i = 0 ;  @Override  public void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.main);   //根據控制項的ID來取得代表控制項的對象   firstBar = (ProgressBar)findViewById(R.id.firstBar);   secondBar = (ProgressBar)findViewById(R.id.secondBar);   myButton = (Button)findViewById(R.id.myButton);   myButton.setOnClickListener(new ButtonListener());  }  class ButtonListener implements OnClickListener{      @Override   public void onClick(View v) {    if(i == 0)    {     //設定進度條處於可見的狀態     firstBar.setVisibility(View.VISIBLE);     firstBar.setMax(150);//手動設定最大值,預設是100     secondBar.setVisibility(View.VISIBLE);    }    else if ( i < firstBar.getMax()){     //設定主進度條的當前值     firstBar.setProgress(i);     //設定第二進度條的當前值     firstBar.setSecondaryProgress(i + 10);     //因為預設的進度條無法顯示進行的狀態     //secondBar.setProgress(i);         }    else{     //設定進度條處於不可見狀態     firstBar.setVisibility(View.GONE);     secondBar.setVisibility(View.GONE);    }    i = i + 10 ;   }     }   }

 以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.