Android UI組件進階(1)——帶進度條的按鈕

來源:互聯網
上載者:User

Android UI組件進階(1)——帶進度條的按鈕

Android UI組件進階(1)——帶進度條的按鈕

 

 

本節引言:

這個系列是繼Android UI組件執行個體大全後的進階系列,在該系列中我們將進一步的學習

Android UI組件,建議閱讀本系列前線學習下UI組件執行個體大全系列,掌握基本組件的使用;

當然你也可以直接學習本系列!好了,廢話不多說,直接開始第一節吧!本節要示範的是:

帶進度條的按鈕!相信大家在360手機小幫手到看到這個東東吧:

本節要實現的就是下方這個點擊後顯示進度的按鈕

 

 

:

 

 

 

必備基礎:

1.進度條的一些屬性:

background:設定背景圖片

max:設定進度條的最大值

progress:設定進度條的值

style=?android:attr/progressBarStyleHorizontal :定義進度條為水平方向的風格

progressDrawable:當我們不想使用系統預設的Progress時可以自己定義一個,這個資源檔就是

用來調用我們自己定義的Progress表徵圖的一般是在drawable下建立一個.xml件使用layer-list來組

織這些表徵圖.

 

2.Handler的相關方法:

對UI的操作要麼在主線程中進行,要麼在handler中進行,切忌別在新線程中直接操作UI組件,會報異常的!

handleMessage(msg):使用Handle需要重寫的主要方法,使用msg.what判斷標識碼,執行對應操作

sendEmptyMessage(0x123):發送一個空資訊給handle,標識碼為0x123

sendEmptyMessageDelayed(0x321,500);發送一個空資訊給handler,標識碼為0x321,延遲500毫秒後發送

 


 

工程解析:

 

 

相關代碼:

MainActivity.java

 

package com.jay.uidemo.progressbuttondemo;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.Button;import android.widget.ProgressBar;import android.widget.TextView;public class MainActivity extends Activity {int i=0;ProgressBar progressBar=null;Button downLoadBtn=null;Handler handler=new Handler(){public void handleMessage(Message msg) {switch (msg.what) {case 0x123:downLoadBtn.setClickable(false);i+=20;progressBar.setProgress(i);if(i!=100){handler.sendEmptyMessageDelayed(0x123,500);downLoadBtn.setText(i+%);}else if(i==100){downLoadBtn.setText(下載完成);handler.sendEmptyMessageDelayed(0x321,500);}break;case 0x321:downLoadBtn.setText(開啟);downLoadBtn.setClickable(true);downLoadBtn.setBackgroundResource(R.drawable.aa_button_after);handler.sendEmptyMessageDelayed(0x110,1000);break;case 0x110:progressBar.setProgress(0);downLoadBtn.setBackgroundResource(R.drawable.btn_selector);downLoadBtn.setText(下載);default:break;}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); TextView tx = (TextView) findViewById(R.id.texttitle);  progressBar=(ProgressBar) findViewById(R.id.progressBar);  downLoadBtn=(Button) findViewById(R.id.downLoadBtn);  downLoadBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) { i= 0;handler.sendEmptyMessage(0x123);}});}}


 

drawable中的btn_selctor.xml

 

        


 

drawable中的progress_selctor.xml

 

                

activity_main.xml:

 

 

             


 

 

 

 

 

聯繫我們

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