天天看點

練習Toast和Notification 5.7.1 Toast提醒你 5.7.2 Notification提醒你 5.7.3 練習Toast和Notification(1) 5.7.3 練習Toast和Notification(2) 5.7.3 練習Toast和Notification(3) 5.7.3 練習Toast和Notification(4)

5.7.1 Toast提醒你

2011-12-30 11:34 徐娜子 電子工業出版社  我要評論(0) 字号: T |  T

綜合評級:

想讀(0)  在讀(0)  已讀(0)   品書齋鑒(0)   已有0人發表書評

練習Toast和Notification 5.7.1 Toast提醒你 5.7.2 Notification提醒你 5.7.3 練習Toast和Notification(1) 5.7.3 練習Toast和Notification(2) 5.7.3 練習Toast和Notification(3) 5.7.3 練習Toast和Notification(4)

《Android江湖》第5章系出名門,在本章内容中講解了Android中基本控件的基本使用知識。首先介紹了UI等常用布局控件的基本知識和用法,然後詳細講解了友好界面控件和清單控件的的基本知識,最後講解了Intent、Activity、Toast和Notification控件的基本知識。本節為大家介紹Toast提醒你。

AD:51CTO雲計算架構師峰會 搶票進行中!

5.7 Toast和Notification控件實作提醒

生活中有提醒,Android中也有提醒。在Android中,可以通過Toast和Notification控件來實作提醒功能。和Dialog相比,此類型提醒更加友好和溫馨,并且不會打斷使用者的目前操作。在本節内容中将詳細講解Toast和Notification控件的具體使用方法。

5.7.1 Toast提醒你

Toast是Android 中用來顯示資訊的一種機制,和Dialog不同的是,Toast沒有焦點,而且Toast 顯示的時間有限,經過一定的時間後就會自動消失。

5.7.2 Notification提醒你

2011-12-30 11:34 徐娜子 電子工業出版社  我要評論(0) 字号: T |  T

綜合評級:

想讀(0)  在讀(0)  已讀(0)   品書齋鑒(0)   已有0人發表書評

練習Toast和Notification 5.7.1 Toast提醒你 5.7.2 Notification提醒你 5.7.3 練習Toast和Notification(1) 5.7.3 練習Toast和Notification(2) 5.7.3 練習Toast和Notification(3) 5.7.3 練習Toast和Notification(4)

《Android江湖》第5章系出名門,在本章内容中講解了Android中基本控件的基本使用知識。首先介紹了UI等常用布局控件的基本知識和用法,然後詳細講解了友好界面控件和清單控件的的基本知識,最後講解了Intent、Activity、Toast和Notification控件的基本知識。本節為大家介紹Notification提醒你。

AD:51CTO雲計算架構師峰會 搶票進行中!

5.7.2 Notification提醒你

看名字就知道,Notification和提醒有關,它通常和NotificationManager同時使用。具體來說,其主要功能如下:

1.NotificationManager和Notification用來設定通知

通知的設定等操作相對比較簡單,基本的使用方式就是用建立一個Notification對象,然後設定好通知的各項參數,然後使用系統背景運作的NotificationManager服務将通知發出來。基本步驟如下:

(1)得到NotificationManager:

  1. String ns = Context.NOTIFICATION_SERVICE;  
  2. NotificationManager mNotificationManager = (NotificationManager)  
  3. getSystemService(ns); 

(2)建立一個新的Notification對象:

  1. Notification notification = new Notification();  
  2. notification.icon = R.drawable.notification_icon; 

也可以使用稍微複雜一些的方式建立Notification:

  1. int icon = R.drawable.notification_icon; //通知圖示  
  2. CharSequence tickerText = "Hello"; //狀态欄(Status Bar)顯示的通知文本提示  
  3. long when = System.currentTimeMillis(); //通知産生的時間,會在通知資訊裡顯示  
  4. Notification notification = new Notification(icon, tickerText, when); 

(3)填充Notification的各個屬性:

  1. Context context = getApplicationContext();  
  2. CharSequence contentTitle = "My notification";  
  3. CharSequence contentText = "Hello World!";  
  4. Intent notificationIntent = new Intent(this, MyClass.class);  
  5. PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  
  6. notificationIntent, 0);  
  7. notification.setLatestEventInfo(context, contentTitle, contentText,  
  8. contentIntent); 

Notification提供了如下幾種手機提示方式:

在狀态欄(Status Bar)顯示的通知文本提示,例如:notification.tickerText = "hello";

發出提示音,例如:

  1. notification.defaults |= Notification.DEFAULT_SOUND;  
  2. notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");  
  3. notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_  
  4. URI, "6"); 

手機振動,例如:

  1. notification.defaults |= Notification.DEFAULT_VIBRATE;  
  2. long[] vibrate = {0,100,200,300};  
  3. notification.vibrate = vibrate; 

LED燈閃爍,例如:

  1. notification.defaults |= Notification.DEFAULT_LIGHTS;  
  2. notification.ledARGB = 0xff00ff00;  
  3. notification.ledOnMS = 300;  
  4. notification.ledOffMS = 1000;  
  5. notification.flags |= Notification.FLAG_SHOW_LIGHTS; 

(4)發送通知,代碼如下:

  1. private static final int ID_NOTIFICATION = 1;  
  2. mNotificationManager.notify(ID_NOTIFICATION, notification); 

2.更新通知

如果需要更新一個通知,則隻需要在設定好notification之後,再調用setLatestEventInfo,然後重新發送一次通知即可。

為了更新一個已經觸發過的Not i f icat ion,傳入相同的ID,此時可以傳入相同的Notification對象,也可以是一個全新的對象。隻要ID相同,新的Notification對象會替換狀态條圖示和擴充的狀态視窗的細節。

另外,還可以使用ID來取消Notification,通過調用NotificationManager的cancel方法,例如:

  1. notificationManager.cancel(notificationRef); 

當取消一個Notification時,會移除它的狀态條圖示以及清除在擴充的狀态視窗中的資訊。

5.7.3 練習Toast和Notification(1)

2011-12-30 11:34 徐娜子 電子工業出版社  我要評論(0) 字号: T |  T

綜合評級:

想讀(0)  在讀(0)  已讀(0)   品書齋鑒(0)   已有0人發表書評

練習Toast和Notification 5.7.1 Toast提醒你 5.7.2 Notification提醒你 5.7.3 練習Toast和Notification(1) 5.7.3 練習Toast和Notification(2) 5.7.3 練習Toast和Notification(3) 5.7.3 練習Toast和Notification(4)

《Android江湖》第5章系出名門,在本章内容中講解了Android中基本控件的基本使用知識。首先介紹了UI等常用布局控件的基本知識和用法,然後詳細講解了友好界面控件和清單控件的的基本知識,最後講解了Intent、Activity、Toast和Notification控件的基本知識。本節為練習Toast和Notification。

AD:51CTO雲計算架構師峰會 搶票進行中!

5.7.3 練習Toast和Notification(1)

練習Toast和Notification 5.7.1 Toast提醒你 5.7.2 Notification提醒你 5.7.3 練習Toast和Notification(1) 5.7.3 練習Toast和Notification(2) 5.7.3 練習Toast和Notification(3) 5.7.3 練習Toast和Notification(4)

練習12:使用Toast和Notification實作提醒功能效果的的具體使用流程源碼路徑:“第5章\widges”檔案夾

(1)建立一個Android工程檔案,然後編寫main.xml布局檔案,具體代碼如下所示:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android" 
  3. Android:orientation="vertical" Android:layout_width="fill_parent" 
  4. Android:layout_height="fill_parent"> 
  5. <Button Android:id="@+id/button1" 
  6. Android:layout_width="wrap_content" 
  7. Android:layout_height="wrap_content" Android:text="介紹Notification" 
  8. /> 
  9. <Button Android:id="@+id/button2" 
  10. Android:layout_width="wrap_content" 
  11. Android:layout_height="wrap_content" Android:text="介紹Toast" /> 
  12. </LinearLayout> 

通過上述代碼插入了兩個Button按鈕,執行後的效果如圖5-40所示。

練習Toast和Notification 5.7.1 Toast提醒你 5.7.2 Notification提醒你 5.7.3 練習Toast和Notification(1) 5.7.3 練習Toast和Notification(2) 5.7.3 練習Toast和Notification(3) 5.7.3 練習Toast和Notification(4)
圖5-40 插入兩個Button

(2)編寫處理檔案ActivityMain.java,具體代碼如下所示:

  1. public class ActivityMain extends Activity {  
  2. OnClickListener listener1 = null;  
  3. OnClickListener listener2 = null;  
  4. Button button1;  
  5. Button button2;  
  6. @Override  
  7. public void onCreate(Bundle savedInstanceState) {  
  8. super.onCreate(savedInstanceState);  
  9. listener1 = new OnClickListener() {  
  10. public void onClick(View v) {  
  11. setTitle(“這是Notification");  
  12. Intent intent = new Intent(ActivityMain.this,  
  13. ActivityMainNotification.class);  
  14. startActivity(intent);  
  15. }  
  16. };  
  17. listener2 = new OnClickListener() {  
  18. public void onClick(View v) {  
  19. setTitle("這是Toast");  
  20. Intent intent = new Intent(ActivityMain.this,  
  21. ActivityToast.class);  
  22. startActivity(intent);  
  23. }  
  24. };  
  25. setContentView(R.layout.main);  
  26. button1 = (Button) findViewById(R.id.button1);  
  27. button1.setOnClickListener(listener1);  
  28. button2 = (Button) findViewById(R.id.button2);  
  29. button2.setOnClickListener(listener2);  
  30. }  

在上述代碼中,對兩個Button綁定了單擊監聽器OnClickListener,當單擊這兩個Button時,會跳轉到新的Activity上。

(3)編寫第一個Button的處理程式,即單擊圖5-21中的“介紹Notification”按鈕後,執行ActivityMainNotification.java,其主要代碼如下所示:

  1. public class ActivityMainNotification extends Activity {  
  2. private static int NOTIFICATIONS_ID = R.layout.activity_notification;  
  3. private NotificationManager mNotificationManager;  
  4. @Override  
  5. public void onCreate(Bundle savedInstanceState) {  
  6. super.onCreate(savedInstanceState);  
  7. setContentView(R.layout.activity_notification);  
  8. Button button;  
  9. mNotificationManager = (NotificationManager) getSystemService  
  10. (NOTIFICATION_ SERVICE);  
  11. button = (Button) findViewById(R.id.sun_1);  
  12. button.setOnClickListener(new Button.OnClickListener() {  
  13. public void onClick(View v) {  
  14. setWeather("好", "天氣", "好", R.drawable.sun);  
  15. }  
  16. });  
  17. button = (Button) findViewById(R.id.cloudy_1);  
  18. button.setOnClickListener(new Button.OnClickListener() {  
  19. public void onClick(View v) {  
  20. setWeather("還行", "天氣", "還行", R.drawable.cloudy);  
  21. }  
  22. });  
  23. button = (Button) findViewById(R.id.rain_1);  
  24. button.setOnClickListener(new Button.OnClickListener() {  
  25. public void onClick(View v) {  
  26. setWeather("不好", "天氣", "不好", R.drawable.rain);  
  27. }  
  28. });  
  29. button = (Button) findViewById(R.id.defaultSound);  
  30. button.setOnClickListener(new Button.OnClickListener() {  
  31. public void onClick(View v) {  
  32. setDefault(Notification.DEFAULT_SOUND);  
  33. }  
  34. });  
  35. button = (Button) findViewById(R.id.defaultVibrate);  
  36. button.setOnClickListener(new Button.OnClickListener() {  
  37. public void onClick(View v) {  
  38. setDefault(Notification.DEFAULT_VIBRATE);  
  39. }  
  40. });  
  41. button = (Button) findViewById(R.id.defaultAll);  
  42. button.setOnClickListener(new Button.OnClickListener() {  
  43. public void onClick(View v) {  
  44. setDefault(Notification.DEFAULT_ALL);  
  45. }  
  46. });  
  47. button = (Button) findViewById(R.id.clear);  
  48. button.setOnClickListener(new Button.OnClickListener() {  
  49. public void onClick(View v) {  
  50. mNotificationManager.cancel(NOTIFICATIONS_ID);  
  51. }  
  52. });  
  53. }  
  54. private void setWeather(String tickerText, String title, String content,  
  55. int drawable) {  
  56. Notification notification = new Notification(drawable, tickerText,  
  57. System.currentTimeMillis());  
  58. PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  
  59. new Intent(this, ActivityMain.class), 0);  
  60. notification.setLatestEventInfo(this, title, content, contentIntent);  
  61. mNotificationManager.notify(NOTIFICATIONS_ID, notification);  
  62. }  
  63. private void setDefault(int defaults) {  
  64. PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  
  65. new Intent(this, ActivityMain.class), 0);  
  66. String title = "天氣預報";  
  67. String content = "晴";  
  68. final Notification notification = new Notification(R.drawable.sun,  
  69. content, System.currentTimeMillis());  
  70. notification.setLatestEventInfo(this, title, content, contentIntent);  
  71. notification.defaults = defaults;  
  72. mNotificationManager.notify(NOTIFICATIONS_ID, notification);  
  73. }  

5.7.3 練習Toast和Notification(2)

2011-12-30 11:34 徐娜子 電子工業出版社  我要評論(0) 字号: T |  T

綜合評級:

想讀(0)  在讀(0)  已讀(0)   品書齋鑒(0)   已有0人發表書評

練習Toast和Notification 5.7.1 Toast提醒你 5.7.2 Notification提醒你 5.7.3 練習Toast和Notification(1) 5.7.3 練習Toast和Notification(2) 5.7.3 練習Toast和Notification(3) 5.7.3 練習Toast和Notification(4)

《Android江湖》第5章系出名門,在本章内容中講解了Android中基本控件的基本使用知識。首先介紹了UI等常用布局控件的基本知識和用法,然後詳細講解了友好界面控件和清單控件的的基本知識,最後講解了Intent、Activity、Toast和Notification控件的基本知識。本節為練習Toast和Notification。

AD:51CTO雲計算架構師峰會 搶票進行中!

5.7.3 練習Toast和Notification(2)

因為所有的Notification都是通過NotificationManager來管理的,是以應該首先得到NotificationManager執行個體,以便管理這個Activity中的跳轉服務。擷取NotificationManager執行個體的代碼如下所示:

mNotificationManager=(NotificationManager)getSystemService(NOTIFICATION_

SERVICE);

函數setWeather是ActivityMainNotification中的一個重要函數,它執行個體化了一個Notification,并将這個Notification顯示出來。在下面的代碼中:

Notification notification = new Notification(drawable, tickerText,

System.currentTimeMillis());

包含了3個參數,具體說明如下:

第1個:要顯示的圖檔的ID;

第2個:顯示的文本文字;

第3個: N o t i f i c a t i o n 顯示的時間, 一般是立即顯示, 時間就是S y s t e m .currentTimeMillis()。

函數setDefault也是ActivityMainNotification.java中的一個重要函數,在此函數中初始化了一個Notification,在設定Notification時使用了其預設值的形式,即:

notification.defaults = defaults;

另外,在上述程式中還用到了以下幾種表現形式:

Notification.DEFAULT_VIBRATE:表示目前的Notification

顯示出來時手機會發出震動;

Notification.DEFAULT_SOUND:表示目前的Notification顯示出來時手機會伴随音樂;

Notification.DEFAULT_ALL:表示目前的Notification顯示出來時手機既會震動,也會伴随音樂。

這樣當單擊第一個Button後會執行上述處理程式,來到對應的新界面,新界面的布局檔案是由activity_notification.xml實作的,其主要代碼如下所示:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <ScrollView xmlns:Android="http://schemas.Android.com/apk/res/Android" 
  3. Android:layout_width="fill_parent" 
  4. Android:layout_height="fill_parent"> 
  5. <LinearLayout 
  6. Android:orientation="vertical" 
  7. Android:layout_width="fill_parent" 
  8. Android:layout_height="wrap_content"> 
  9. <LinearLayout 
  10. Android:orientation="vertical" 
  11. Android:layout_width="fill_parent" 
  12. Android:layout_height="wrap_content"> 
  13. <Button 
  14. Android:id="@+id/sun_1" 
  15. Android:layout_width="wrap_content" 
  16. Android:layout_height="wrap_content" 
  17. Android:text="适合" /> 
  18. <Button 
  19. Android:id="@+id/cloudy_1" 
  20. Android:layout_width="wrap_content" 
  21. Android:layout_height="wrap_content" 
  22. Android:text="一般" /> 
  23. <Button 
  24. Android:id="@+id/rain_1" 
  25. Android:layout_width="wrap_content" 
  26. Android:layout_height="wrap_content" 
  27. Android:text="一點也不适合" /> 
  28. </LinearLayout> 
  29. <TextView 
  30. Android:layout_width="wrap_content" 
  31. Android:layout_height="wrap_content" 
  32. Android:layout_marginTop="20dip" 
  33. Android:text="進階notification" /> 
  34. <LinearLayout 
  35. Android:orientation="vertical" 
  36. Android:layout_width="fill_parent" 
  37. Android:layout_height="wrap_content"> 
  38. <Button 
  39. Android:id="@+id/defaultSound" 
  40. Android:layout_width="wrap_content" 
  41. Android:layout_height="wrap_content" 
  42. Android:text="有聲音的提示" /> 
  43. <Button 
  44. Android:id="@+id/defaultVibrate" 
  45. Android:layout_width="wrap_content" 
  46. Android:layout_height="wrap_content" 
  47. Android:text="振動的提示" /> 
  48. <Button 
  49. Android:id="@+id/defaultAll" 
  50. Android:layout_width="wrap_content" 
  51. Android:layout_height="wrap_content" 
  52. Android:text="聲音+振動的提示" /> 
  53. </LinearLayout> 
  54. <Button Android:id="@+id/clear" 
  55. Android:layout_width="wrap_content" 
  56. Android:layout_height="wrap_content" 
  57. Android:layout_marginTop="20dip" 
  58. Android:text="清除提示" /> 
  59. </LinearLayout> 
  60. </ScrollView> 

5.7.3 練習Toast和Notification(3)

2011-12-30 13:44 徐娜子 電子工業出版社  我要評論(0) 字号: T |  T

綜合評級:

想讀(0)  在讀(0)  已讀(0)   品書齋鑒(0)   已有0人發表書評

練習Toast和Notification 5.7.1 Toast提醒你 5.7.2 Notification提醒你 5.7.3 練習Toast和Notification(1) 5.7.3 練習Toast和Notification(2) 5.7.3 練習Toast和Notification(3) 5.7.3 練習Toast和Notification(4)

《Android江湖》第5章系出名門,在本章内容中講解了Android中基本控件的基本使用知識。首先介紹了UI等常用布局控件的基本知識和用法,然後詳細講解了友好界面控件和清單控件的的基本知識,最後講解了Intent、Activity、Toast和Notification控件的基本知識。本節為大家介紹練習Toast和Notification。

AD:51CTO雲計算架構師峰會 搶票進行中!

5.7.3 練習Toast和Notification(3)

程式執行後新界面的效果如圖5-41所示。

當單擊圖5-41所示的新界面中的Button後,會根據上述處理檔案而實作某種效果,例如,單擊“有聲音的提示”按鈕後,會發出聲音。

(4)編寫第二個Button的處理程式,即單擊圖5-41中的“振動的提示”按鈕後,執行ActivityToast.java,其主要代碼如下所示:

練習Toast和Notification 5.7.1 Toast提醒你 5.7.2 Notification提醒你 5.7.3 練習Toast和Notification(1) 5.7.3 練習Toast和Notification(2) 5.7.3 練習Toast和Notification(3) 5.7.3 練習Toast和Notification(4)
圖5-41 運作效果
  1. public class ActivityToast extends Activity {  
  2. OnClickListener listener1 = null;  
  3. OnClickListener listener2 = null;  
  4. Button button1;  
  5. Button button2;  
  6. private static int NOTIFICATIONS_ID = R.layout.activity_toast;  
  7. @Override  
  8. public void onCreate(Bundle savedInstanceState) {  
  9. super.onCreate(savedInstanceState);  
  10. listener1 = new OnClickListener() {  
  11. public void onClick(View v) {  
  12. setTitle("短時提醒");  
  13. showToast(Toast.LENGTH_SHORT);  
  14. }  
  15. };  
  16. listener2 = new OnClickListener() {  
  17. public void onClick(View v) {  
  18. setTitle("長時提醒");  
  19. showToast(Toast.LENGTH_LONG);  
  20. showNotification();  
  21. }  
  22. };  
  23. setContentView(R.layout.activity_toast);  
  24. button1 = (Button) findViewById(R.id.button1);  
  25. button1.setOnClickListener(listener1);  
  26. button2 = (Button) findViewById(R.id.button2);  
  27. button2.setOnClickListener(listener2);  
  28. }  
  29. protected void showToast(int type) {  
  30. View view = inflateView(R.layout.toast);  
  31. TextView tv = (TextView) view.findViewById(R.id.content);  
  32. tv.setText("歡迎來到濟南,");  
  33. /*執行個體化Toast*/  
  34. Toast toast = new Toast(this);  
  35. toast.setView(view);  
  36. toast.setDuration(type);  
  37. toast.show();  
  38. }  
  39. private View inflateView(int resource) {  
  40. LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_  
  41. INFLATER_SERVICE);  
  42. return vi.inflate(resource, null);  
  43. }  
  44. protected void showNotification() {  
  45. NotificationManager notificationManager = (NotificationManager)  
  46. getSystemService(NOTIFICATION_SERVICE);  
  47. CharSequence title = "省會";  
  48. CharSequence contents = "齊魯大地";  
  49. PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  
  50. new Intent(this, ActivityMain.class), 0);  
  51. Notification notification = new Notification(R.drawable.default_icon,  
  52. title, System.currentTimeMillis());  
  53. notification.setLatestEventInfo(this, title, contents, contentIntent);  
  54. // 100ms延遲後振動250ms,停止100ms後振動500ms  
  55. notification.vibrate = new long[] { 100, 250, 100, 500 };  
  56. notificationManager.notify(NOTIFICATIONS_ID, notification);  
  57. }  

5.7.3 練習Toast和Notification(4)

2011-12-30 13:44 徐娜子 電子工業出版社  我要評論(0) 字号: T |  T

綜合評級:

想讀(0)  在讀(0)  已讀(0)   品書齋鑒(0)   已有0人發表書評

練習Toast和Notification 5.7.1 Toast提醒你 5.7.2 Notification提醒你 5.7.3 練習Toast和Notification(1) 5.7.3 練習Toast和Notification(2) 5.7.3 練習Toast和Notification(3) 5.7.3 練習Toast和Notification(4)

《Android江湖》第5章系出名門,在本章内容中講解了Android中基本控件的基本使用知識。首先介紹了UI等常用布局控件的基本知識和用法,然後詳細講解了友好界面控件和清單控件的的基本知識,最後講解了Intent、Activity、Toast和Notification控件的基本知識。本節為大家介紹練習Toast和Notification。

AD:51CTO雲計算架構師峰會 搶票進行中!

5.7.3 練習Toast和Notification(4)

上述代碼是通過Toast實作的,它不需要用NotificationManager來管理,上述代碼的處理流程如下:

執行個體化Toast,每個Toast和一個View相關;

設定Toast的長短,通過“showToast(Toast.LENGTH_SHORT);”來設定Toast短時間顯示,通過“showToast(Toast.LENGTH_LONG);”來設定Toast長時間顯示;

通過函數showToast來顯示Toast,當單擊“長時顯示”按鈕後,程式會長時間顯示提醒,并用Notification在狀态欄中提示使用者;當單擊“短時顯示”按鈕後,程式會短時間顯示提醒。

這樣單擊第二個Button後會執行上述處理程式,來到對應的新界面,新界面的布局檔案是由activity_toast.xml實作的,其主要代碼如下所示:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android" 
  3. Android:orientation="vertical" Android:layout_width="fill_parent" 
  4. Android:layout_height="fill_parent"> 
  5. <Button Android:id="@+id/button1" 
  6. Android:layout_width="wrap_content" 
  7. Android:layout_height="wrap_content" Android:text="短時Toast" /> 
  8. <Button Android:id="@+id/button2" 
  9. Android:layout_width="wrap_content" 
  10. Android:layout_height="wrap_content" Android:text="長時Toast" /> 
  11. </LinearLayout> 

程式執行後新界面的效果如圖5-42所示。

練習Toast和Notification 5.7.1 Toast提醒你 5.7.2 Notification提醒你 5.7.3 練習Toast和Notification(1) 5.7.3 練習Toast和Notification(2) 5.7.3 練習Toast和Notification(3) 5.7.3 練習Toast和Notification(4)
圖5-42 新界面的效果

當單擊圖5-42所示的新界面中的Button後,會根據上述處理檔案而實作某種效果,例如,單擊“短時Toast”按鈕後,會短時間顯示一個提醒;當單擊“長時Toast”按鈕後,會長時間顯示一個提醒。這兩種方式的提醒界面都是相同的,具體效果如圖5-43所示。

練習Toast和Notification 5.7.1 Toast提醒你 5.7.2 Notification提醒你 5.7.3 練習Toast和Notification(1) 5.7.3 練習Toast和Notification(2) 5.7.3 練習Toast和Notification(3) 5.7.3 練習Toast和Notification(4)