自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android Notifications通知詳解

移動開發(fā) Android
Android提供了三種通知類型方式:Toast Notifications、Status Bar Notification、Dialog Notification。現(xiàn)在分別來看看它們適用的場景與使用方法。

一、Toast Notifications

以背景改變方式,提示一些簡短的消息,消息窗口自動淡入淡出,不接受交互事件。

例如:當下載某個文件完成時,可以提示簡短的“保存成功”。

顯示效果:

創(chuàng)建彈出提示方法:

1、創(chuàng)建Toast對象,可以通過Toast提供的靜態(tài)方法makeText(Context context, String message, int duration)

context:應用上下文對象,這里可以傳遞getApplicationContext()

message:提示文本

duration:顯示時長,可以使用Toast.LENGTH_SHORT、Toast.LENGTH_LONG

  1. Context context = getApplicationContext(); 
  2.  
  3. Toast toast = Toast.makeText(context, "保存成功", Toast.LENGTH_LONG); 

2、顯示提示,調用show()方法

  1. toast.show(); 

上述兩步也可簡寫為:

  1. Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG).show(); 

這樣,最簡單的提示信息已經(jīng)完成?,F(xiàn)在來看看如何創(chuàng)建自定義外觀Toast notification。

3、自定義外觀Toast通知

3.1、定義XML資源視圖作為提示的外觀

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.               android:id="@+id/toast_layout_root" 
  4.               android:orientation="horizontal" 
  5.               android:layout_width="fill_parent" 
  6.               android:layout_height="fill_parent" 
  7.               android:padding="10dp" 
  8.               android:background="#DAAA" 
  9.               > 
  10.     <ImageView android:id="@+id/image" 
  11.                android:layout_width="wrap_content" 
  12.                android:layout_height="fill_parent" 
  13.                android:layout_marginRight="10dp" 
  14.                android:src="@drawable/icon" 
  15.                /> 
  16.     <TextView android:id="@+id/text" 
  17.               android:layout_width="wrap_content" 
  18.               android:layout_height="fill_parent" 
  19.               android:textColor="#FFF" 
  20.               /> 
  21. </LinearLayout> 

其中TextView文本組件用來顯示需要提示的文本。這里默認沒有設置文字。

3.2、解析上述XML資源視圖,并設置提示文本

  1. LayoutInflater inflater = getLayoutInflater();//XML資源布局填充對象 
  2. View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root)); 
  3.  
  4. //修改自定義布局中TextView文本,作為提示信息 
  5. TextView textView = (TextView) layout.findViewById(R.id.text); 
  6. textView.setText("自定義界面:保存成功"); 

3.3、創(chuàng)建Toast對象,并設置視圖、顯示視圖

  1. Toast toast = new Toast(getApplicationContext()); 
  2. //設置垂直居中,水平、垂直偏移值為0,表示正中間。 
  3. toast.setGravity(Gravity.CENTER_VERTICAL, 00);//設置提示框位置,三個參數(shù)分別代表:對其方式、水平偏移值、垂直偏移值。 
  4. toast.setDuration(Toast.LENGTH_LONG); 
  5. toast.setView(layout);//設置顯示的視圖 
  6. toast.show(); 

顯示效果圖:

#p#

二、Status Bar Notification

狀態(tài)欄通知。當某個應用處于后臺運行時需要提示用戶某些信息時,不可能啟動Activity。這時使用狀態(tài)欄通知就非常合適。

例如:最經(jīng)典的就是當接收到新短信時,可以在通知欄看到簡要信息。

創(chuàng)建狀態(tài)欄通知的過程:

1.取得通知管理器

  1. NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 

2.實例化通知對象

  1. /** 
  2.  * new Notification(int icon, String message, long when) 
  3.  * 參數(shù)1:通知圖標 
  4.  * 參數(shù)2:簡短提示文本 
  5.  * 參數(shù)3:何時顯示,這里使用的是時間戳 
  6.  */ 
  7. Notification notification = new Notification(R.drawable.icon, "狀態(tài)欄通知測試", System.currentTimeMillis()); 

3.定義通知的詳細信息、及PendIntent來設置激活的Activity

  1. //這里設置意圖處理很簡單,僅僅是當用戶觸摸詳細信息時,將會顯示MainActivity界面 
  2.         Intent notificationIntent = new Intent(this, MainActivity.class); 
  3.         PendingIntent pendingIntent = PendingIntent.getActivity(this200, notificationIntent, 0); 
  4.         notification.setLatestEventInfo(this"通知完整標題""通知內容", pendingIntent); 

4.傳遞到通知管理器,加入到通知隊列

  1. manager.notify(11, notification); 

這樣,就完成了一個簡單的狀態(tài)欄通知。

除此之外,還可以設置通知的提示方式,如震動、音樂、閃爍等。

設置提示聲音:

  1. notification.sound = Uri.parse("file:///sdcard/On Call.mp3"); 

設置震動的交替模式:

  1. notification.vibrate  = new long[]{0,100,200,300}; 

這里vibrate是一個長整型數(shù)組,用來設置震動交替時長,第一個值表示震動開始之前,第二個值表示第一次震動的時間,第三個值表示第二次震動的時間,以次類推。

#p#

三、Dialog Notification

一個對話框,用于遮擋當前界面,使得當前界面失去焦點。

通常用于鎖定屏幕,提示用戶等待等場景。例如:某個文件正在下載,出現(xiàn)提示等待。成功下載之后才能允許用戶進行其他操作。

常用Dialog類型有:Alert Dialog、ProgressDialog、Custom Dialog

1.使用AlertDialog創(chuàng)建選擇窗口、列表窗口、單選窗口、多選窗口

1.1選擇窗口

效果:

創(chuàng)建彈窗方法:

  1.     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
  2.     builder.setCancelable(false);//設置當點擊返回按鈕后,默認表示的行為。這里設置為false 
  3.     builder.setMessage("dialog彈窗標題"); 
  4.      
  5.     //設置true按鈕 
  6.     builder.setPositiveButton("Yes"new OnClickListener() { 
  7.     @Override 
  8.     public void onClick(DialogInterface dialog, int which) { 
  9.         Toast.makeText(getApplicationContext(), "您選擇了Yes", Toast.LENGTH_LONG).show(); 
  10.     } 
  11. }); 
  12.      
  13.     //設置false按鈕 
  14.     builder.setNegativeButton("No"new OnClickListener() { 
  15.     @Override 
  16.     public void onClick(DialogInterface dialog, int which) { 
  17.         dialog.cancel(); 
  18.     } 
  19. }); 
  20.      
  21.     //顯示 
  22.     builder.show(); 

1.2列表窗口

效果:

創(chuàng)建方法:

  1.     final String[] list = new String[]{"item1""item2""item3"}; 
  2.      
  3.     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
  4.     builder.setTitle("dialog list彈窗標題"); 
  5.     /** 
  6.      * setItems(CharSequence[] items, OnClickListener listener) 
  7.      * items:接收字符串數(shù)組,作為下拉列表選項 
  8.      * listener:監(jiān)聽選中事件 
  9.      */ 
  10.     builder.setItems(list, new OnClickListener() { 
  11.  
  12. @Override 
  13. /** 
  14.  * dialog:表示當前彈窗對象 
  15.  * which:表示當前選中項的對應list數(shù)組的序號 
  16.  */ 
  17. public void onClick(DialogInterface dialog, int which) { 
  18.     Toast.makeText(getApplicationContext(), "您選擇了:"+list[which], Toast.LENGTH_LONG).show(); 
  19. ); 
  20.      
  21.     builder.show(); 

1.3單選列表彈窗

效果:

創(chuàng)建方法:

  1.     final String[] list = new String[]{"item1""item2""item3"}; 
  2.      
  3.     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
  4.     builder.setTitle("dialog list_single彈窗標題"); 
  5.     /** 
  6.      * setSingleChoiceItems(CharSequence[] items, int checkedItem, OnClickListener listener) 
  7.      * items:下拉列表字符串數(shù)組 
  8.      * checkedItem:默認選中的數(shù)組序號,-1表示沒有默認選中項 
  9.      * listener:監(jiān)聽選中事件,注意!,單選、多選彈窗,當選擇某個項時,默認是不會關閉彈窗的。需要手動關閉。 
  10.      */ 
  11.     builder.setSingleChoiceItems(list, -1new OnClickListener() { 
  12.      
  13.     @Override 
  14.     public void onClick(DialogInterface dialog, int which) { 
  15.         Toast.makeText(getApplicationContext(), list[which], Toast.LENGTH_SHORT).show(); 
  16.         dialog.cancel(); 
  17.         //這里,當用戶選中某個項時,提示選中文字,并關閉彈窗 
  18.     } 
  19. }); 
  20.      
  21.     builder.show(); 

1.4多選列表彈窗

效果:

創(chuàng)建方法:

  1.     final String[] list = new String[]{"item1""item2""item3"}; 
  2.      
  3.     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
  4.     builder.setTitle("dialog list_mutil彈窗標題"); 
  5.     /** 
  6.      * setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, OnMultiChoiceClickListener listener) 
  7.      * items:下拉列表字符串數(shù)組 
  8.      * checkedItems:boolean數(shù)組,如果需要默認被選中,可以傳遞。null表示沒有默認選中項 
  9.      * listener:監(jiān)聽選中事件,注意!,單選、多選彈窗,當選擇某個項時,默認是不會關閉彈窗的。需要手動關閉。 
  10.      */ 
  11. builder.setMultiChoiceItems(list, nullnew OnMultiChoiceClickListener() { 
  12.      
  13.     @Override 
  14.     public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
  15.         Toast.makeText(getApplicationContext(), list[which], Toast.LENGTH_SHORT).show(); 
  16.         dialog.cancel(); 
  17.     } 
  18. }); 
  19.  
  20. builder.show(); 

2、自定義彈窗

如果需要自定義彈窗外觀,那么可以使用自定義彈窗。

下面一個自定義彈窗效果,并看看是如何實現(xiàn)的。

  1. AlertDialog.Builder builder; 
  2. LayoutInflater inflater = getLayoutInflater(); 
  3. View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.layout_root)); 
  4.  
  5. TextView text = (TextView) layout.findViewById(R.id.text); 
  6. text.setText("這是自定義彈窗"); 
  7.  
  8. builder = new AlertDialog.Builder(getApplicationContext()); 
  9. builder.setView(layout); 
  10. builder.show(); 

實際上自定義彈窗,就是使用自定義界面并覆蓋原有視圖內容。

責任編輯:徐川 來源: csdn
相關推薦

2010-01-25 17:14:44

Android應用程序

2010-08-01 15:16:41

Android

2010-02-23 14:48:38

WCF事件通知

2012-06-20 09:58:23

Android 4.0通知系統(tǒng)

2012-06-14 10:08:18

2023-11-19 23:19:09

2014-07-28 10:09:30

Android

2011-05-27 15:02:15

Android ListView

2013-11-14 10:42:48

MTPAndroid

2014-07-24 09:11:34

2017-01-11 19:05:45

AndroidAndroid Loa詳解

2013-11-14 16:50:08

2011-08-19 10:55:16

Cocoa通知委托

2013-12-25 09:34:26

Android SDKAndroid組件

2021-09-07 08:49:35

Android

2010-07-13 09:02:19

Widget開發(fā)

2011-09-09 20:14:58

Android Wid

2013-01-10 14:39:28

Android開發(fā)Content Pro組件

2024-03-12 10:05:04

應用程序推送通知

2013-01-05 09:21:55

Ubuntu for UbuntuAndroid
點贊
收藏

51CTO技術棧公眾號