聲明:學(xué)習(xí)的書籍《Android應(yīng)用開發(fā)揭秘》,這里記錄學(xué)習(xí)該書籍的日志,引用的相關(guān)代碼與總結(jié)描述,沒有商業(yè)的用途,完全是自我學(xué)習(xí)的一個記錄,剛剛學(xué)習(xí)不可避免會出現(xiàn)很多問題,若是有錯誤還請大家多多批評。
2011-10-30周日,繼續(xù)《Android應(yīng)用開發(fā)揭秘》的學(xué)習(xí),接上一篇常用效果的學(xué)習(xí);
一、 進度條(ProgressBar)
進度條作為后臺程序處理過程中,反饋給使用者的一個很好的憑證,來顯示當(dāng)前程序處理的怎么樣,進度如何等情況。Android中一共有兩種樣式進度條:長形進度條與圓形進度條。而且有的程序也可以在標(biāo)題欄顯示進度條。
在我們Eclipse開發(fā)android程序中,在編輯main.xml文件時,也提供了圖形化界面的編輯,如下圖:
實例分析:通過一個開始按鈕的點擊,顯示圓形與長形進度條的進度。
關(guān)鍵源碼:
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" />
【注意】該實例關(guān)鍵的是對ProgressBar的控制,之前例子中已經(jīng)將過若是通過Handler實例的sendMessage()方法進而觸發(fā)handleMessage(Message mesg)方法:
//當(dāng)按鈕按下時開始執(zhí)行, mButton01.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ m_ProgressBar.setVisibility(View.VISIBLE);//設(shè)置ProgressBar為可見狀態(tài) m_ProgressBar2.setVisibility(View.VISIBLE); m_ProgressBar.setMax(100); //設(shè)置ProgressBar的最大值 m_ProgressBar.setProgress(0); //設(shè)置ProgressBar當(dāng)前值 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已經(jīng)是對大值 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的當(dāng)前值 m_ProgressBar2.setProgress(intCounter); setProgress(intCounter*100); // 設(shè)置標(biāo)題欄中前景的一個進度條進度值 setSecondaryProgress(intCounter*100);//設(shè)置標(biāo)題欄中后面的一個進度條進度值 } break; } super.handleMessage(msg); } };
實例效果:
二、 拖動條(SeekBar)
拖動條主要用于程序中,對一些屬性的調(diào)節(jié),如:音效大小。在Android中實現(xiàn)還是比較容易,SeekBar控件,而且只需要監(jiān)聽該控件的三個事件:
數(shù)值改變(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" />
效果圖:
三、狀態(tài)欄提示(Notification,NotificationManager)
當(dāng)手機有未接電話或者短信息時,手機頂部狀態(tài)欄就會顯示一個小圖標(biāo),用來顯示用戶有沒有處理的快訊。NotificationManager用來管理狀態(tài)欄的信息,而Notification用來處理這些快訊信息。
NotificationManager對象的獲取通過gerSystenService方法,Notification對象可以設(shè)置其內(nèi)容,圖標(biāo),標(biāo)題等屬性。然后通過notify方法來執(zhí)行一個Notification快訊。
實例分析:當(dāng)用戶點擊一個按鈕,就發(fā)出一個Notification快訊,這是手機頂部狀態(tài)欄顯示相應(yīng)提示信息。展開狀態(tài)欄,點擊快訊信息,跳轉(zhuǎn)到處理界面。
關(guān)鍵源碼:
//初始化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);
//點擊通知時轉(zhuǎn)移內(nèi)容
m_Intent = new Intent(Activity01.this, Activity02.class);
//主要是設(shè)置點擊通知時顯示內(nèi)容的類
m_PendingIntent = PendingIntent.getActivity(Activity01.this, 0, m_Intent, 0);
//構(gòu)造Notification對象
m_Notification = new Notification();
mButton1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//設(shè)置通知在狀態(tài)欄顯示的圖標(biāo)
m_Notification.icon = R.drawable.img1;
//當(dāng)我們點擊通知時顯示的內(nèi)容
m_Notification.tickerText="Button1通知內(nèi)容..........";
//通知時發(fā)出默認(rèn)的聲音
m_Notification.defaults = Notification.DEFAULT_SOUND;
//設(shè)置通知顯示的參數(shù)
m_Notification.setLatestEventInfo(Activity01.this, "Button1", "Button1通知", m_PendingIntent);
//可以理解為執(zhí)行這個通知
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)
對話框中的進度條,可以設(shè)置圖標(biāo),內(nèi)容等屬性。
實例分析:通過點擊兩個按鈕,顯示對話框中得兩種進度條。
關(guān)鍵源碼:
//設(shè)置mButton01的事件的監(jiān)聽 mButton01.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { m_pDialog = new ProgressDialog(Examples_04_24Activity.this);//創(chuàng)建ProgressDialog對象 //設(shè)置進度條風(fēng)格,風(fēng)格為圓形,旋轉(zhuǎn)的 m_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); m_pDialog.setTitle("提示"); //設(shè)置ProgressDialog標(biāo)題 m_pDialog.setMessage("這是一個圓形進度條對話框"); //設(shè)置ProgressDialog提示信息 m_pDialog.setIcon(R.drawable.img1); //設(shè)置ProgressDialog標(biāo)題圖標(biāo) m_pDialog.setIndeterminate(false); //設(shè)置ProgressDialog的進度條是否不明確 m_pDialog.setCancelable(true); //設(shè)置PrgoressDialog是否可以按退回鍵取消 m_pDialog.setButton("確定", new DialogInterface.OnClickListener() {//設(shè)置ProgressDialog的一個Button public void onClick(DialogInterface dialog, int which){ dialog.cancel(); } }); //讓ProgressDialog顯示 m_pDialog.show(); } }); //設(shè)置mButton02的事件監(jiān)聽 mButton02.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { m_count = 0; m_pDialog = new ProgressDialog(Examples_04_24Activity.this);//創(chuàng)建ProgressDialog對象 m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//設(shè)置進度條風(fēng)格,風(fēng)格為長形 ...... 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(); } });
實例效果:
今天就實例效果學(xué)習(xí)結(jié)束了,關(guān)于android的學(xué)習(xí),下面把布局學(xué)習(xí)結(jié)束后,基礎(chǔ)的學(xué)習(xí)就結(jié)束了。加油!
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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