聲明:學習的書籍《Android應用開發揭秘》,這裡記錄學習該書籍的日誌,引用的相關代碼與總結描述,沒有商業的用途,完全是自我學習的一個記錄,剛剛學習不可避免會出現很多問題,若是有錯誤還請大家多多批評。
2011-10-30周日,繼續《Android應用開發揭秘》的學習,接上一篇常用效果的學習;
一、 進度條(ProgressBar)
進度條作為背景程式處理過程中,反饋給使用者的一個很好的憑證,來顯示當前程式處理的怎麼樣,進度如何等情況。Android中一共有兩種樣式進度條:長形進度條與圓形進度條。而且有的程式也可以在標題列顯示進度條。
在我們Eclipse開發android程式中,在編輯main.xml檔案時,也提供了圖形化介面的編輯,如:
執行個體分析:通過一個開始按鈕的點擊,顯示圓形與長形進度條的進度。
關鍵源碼:
main.xml布局檔案:
<ProgressBar android:id="@+id/ProgressBar01"style="?android:attr/progressBarStyleHorizontal" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone" /> <ProgressBar android:id="@+id/ProgressBar02" android:layout_width="wrap_content" android:layout_height="wrap_content" style="?android:attr/progressBarStyleLarge" android:max="100" android:progress="50" android:secondaryProgress="70"android:visibility="gone" />
【注意】該執行個體關鍵的是對ProgressBar的控制,之前例子中已經將過若是通過Handler執行個體的sendMessage()方法進而觸發handleMessage(Message mesg)方法:
//當按鈕按下時開始執行, mButton01.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ m_ProgressBar.setVisibility(View.VISIBLE);//設定ProgressBar為可見狀態 m_ProgressBar2.setVisibility(View.VISIBLE); m_ProgressBar.setMax(100);//設定ProgressBar的最大值 m_ProgressBar.setProgress(0); //設定ProgressBar當前值 m_ProgressBar2.setProgress(0); //通過線程來改變ProgressBar的值 new Thread(new Runnable() {public void run(){for (int i = 0; i < 10; i++){try{intCounter = (i + 1) * 20;Thread.sleep(1000);if (i == 4){Message m = new Message();m.what = Activity01.GUI_STOP_NOTIFIER;Activity01.this.myMessageHandler.sendMessage(m);break;}else{Message m = new Message();m.what = Activity01.GUI_THREADING_NOTIFIER;Activity01.this.myMessageHandler.sendMessage(m);}}catch (Exception e){e.printStackTrace();}}}}).start();}});} Handler myMessageHandler = new Handler(){ public void handleMessage(Message msg){ switch (msg.what){ case Activity01.GUI_STOP_NOTIFIER://ProgressBar已經是對大值 m_ProgressBar.setVisibility(View.GONE); m_ProgressBar2.setVisibility(View.GONE); Thread.currentThread().interrupt(); break; case Activity01.GUI_THREADING_NOTIFIER: if (!Thread.currentThread().isInterrupted()){ m_ProgressBar.setProgress(intCounter);// 改變ProgressBar的當前值m_ProgressBar2.setProgress(intCounter);setProgress(intCounter*100);// 設定標題列中前景的一個進度條進度值setSecondaryProgress(intCounter*100);//設定標題列中後面的一個進度條進度值 } break; } super.handleMessage(msg); } };
執行個體效果:
二、 拖動條(SeekBar)
拖動條主要用於程式中,對一些屬性的調節,如:音效大小。在Android中實現還是比較容易,SeekBar控制項,而且只需要監聽該控制項的三個事件:
數值改變(onProgressChanged);
開始拖動(onStartTrackingTouch);
停止拖動(onStopTrackingTouch);
其控制項配置也比較簡單:
<SeekBar android:id="@+id/seek" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:progress="50"android:secondaryProgress="75" />
:
三、狀態列提示(Notification,NotificationManager)
當手機有未接電話或者簡訊息時,手機頂部狀態列就會顯示一個小表徵圖,用來顯示使用者有沒有處理的快訊。NotificationManager用來管理狀態列的資訊,而Notification用來處理這些快訊資訊。
NotificationManager對象的擷取通過gerSystenService方法,Notification對象可以設定其內容,表徵圖,標題等屬性。然後通過notify方法來執行一個Notification快訊。
執行個體分析:當使用者點擊一個按鈕,就發出一個Notification快訊,這是手機頂部狀態列顯示相應提示資訊。展開狀態列,點擊快訊資訊,跳轉到處理介面。
關鍵源碼:
//初始化NotificationManager對象 m_NotificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); //擷取四個按鈕對象 mButton1 = (Button) this.findViewById(R.id.Button01); mButton2 = (Button) this.findViewById(R.id.Button02); mButton3 = (Button) this.findViewById(R.id.Button03); mButton4 = (Button) this.findViewById(R.id.Button04); //點擊通知時轉移內容 m_Intent = new Intent(Activity01.this, Activity02.class); //主要是設定點擊通知時顯示內容的類 m_PendingIntent = PendingIntent.getActivity(Activity01.this, 0, m_Intent, 0); //構造Notification對象 m_Notification = new Notification(); mButton1.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {//設定通知在狀態列顯示的表徵圖m_Notification.icon = R.drawable.img1;//當我們點擊通知時顯示的內容m_Notification.tickerText="Button1通知內容..........";//通知時發出預設的聲音m_Notification.defaults = Notification.DEFAULT_SOUND;//設定通知顯示的參數m_Notification.setLatestEventInfo(Activity01.this, "Button1", "Button1通知", m_PendingIntent);//可以理解為執行這個通知m_NotificationManager.notify(0,m_Notification);}});
其中,notify()方法:
public void notify (int id, Notification notification)
Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.
Parameters
|
id |
An identifier for this notification unique within your application. |
|
notification |
A Notification object describing what to show the user. Must not be null. |
執行個體:
四、對話方塊中的進度條(ProgressDialog)
對話方塊中的進度條,可以設定表徵圖,內容等屬性。
執行個體分析:通過點擊兩個按鈕,顯示對話方塊中得兩種進度條。
關鍵源碼:
//設定mButton01的事件的監聽mButton01.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {m_pDialog = new ProgressDialog(Examples_04_24Activity.this);//建立ProgressDialog對象//設定進度條風格,風格為圓形,旋轉的m_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);m_pDialog.setTitle("提示");//設定ProgressDialog標題m_pDialog.setMessage("這是一個圓形進度條對話方塊");//設定ProgressDialog提示資訊m_pDialog.setIcon(R.drawable.img1);//設定ProgressDialog標題表徵圖m_pDialog.setIndeterminate(false);//設定ProgressDialog的進度條是否不明確m_pDialog.setCancelable(true);//設定PrgoressDialog是否可以按退回鍵取消m_pDialog.setButton("確定", new DialogInterface.OnClickListener() {//設定ProgressDialog的一個Buttonpublic void onClick(DialogInterface dialog, int which){dialog.cancel();}});//讓ProgressDialog顯示m_pDialog.show();}}); //設定mButton02的事件監聽mButton02.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {m_count = 0;m_pDialog = new ProgressDialog(Examples_04_24Activity.this);//建立ProgressDialog對象m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//設定進度條風格,風格為長形......m_pDialog.show();// 讓ProgressDialog顯示new Thread() {public void run(){try{while (m_count <= 100){ // 由線程來控制進度。m_pDialog.setProgress(m_count++);Thread.sleep(100); }m_pDialog.cancel();}catch (InterruptedException e){m_pDialog.cancel();}}}.start();}});
執行個體效果:
今天就執行個體效果學習結束了,關於android的學習,下面把布局學習結束後,基礎的學習就結束了。加油!