聲明:學習的書籍《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的一個Button public 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的學習,下面把布局學習結束后,基礎的學習就結束了。加油!
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
