Android簡易實戰教程--第二十二話《簡單的水平進度條》

來源:互聯網
上載者:User

標籤:

本打算寫一篇自訂水平進度條,但是出於過度和文章的完整性,今天先來一篇簡單的進度條。

需求:按鈕點擊,實現水平進度條載入進度。而且,進度條設定兩個進度(到下邊的例子裡就能知道什麼是“第二進度條”了)

設定檔代碼如下:

<?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="Hello, ProgressBarTest!"    /><ProgressBarandroid:id="@+id/firstBar"style="?android:attr/progressBarStyleHorizontal"android:layout_width="200dp"android:layout_height="wrap_content"android:visibility="gone"/><Buttonandroid:id="@+id/myButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="begin"/></LinearLayout>

主活動中的代碼如下:

package itydl.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 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);        myButton = (Button)findViewById(R.id.myButton);        myButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if(i == 0)    {    //設定進度條處於可見的狀態    firstBar.setVisibility(View.VISIBLE);    firstBar.setMax(150);//手動設定最大值,預設是100    }    else if ( i < firstBar.getMax()){    //設定主進度條的當前值    firstBar.setProgress(i);    //設定第二進度條的當前值    firstBar.setSecondaryProgress(i + 10);            }    else{//載入的進度完成了    //設定進度條處於不可見狀態    firstBar.setVisibility(View.GONE);    }    i = i + 10 ;    }});    }    }

進度條是系統內建的,如果想要做出其他的效果,期待下一篇載入自訂水平進度條。

期待感謝您關注本部落格點擊開啟連結   http://blog.csdn.net/qq_32059827,只需要花上您5分鐘,觀看一天一篇更新簡單好玩的android文章哦。

Android簡易實戰教程--第二十二話《簡單的水平進度條》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.