android學習日記21--訊息提示之Toast和Notification

來源:互聯網
上載者:User

1、Toast  Toast譯為土司,類似切片麵包,用於彈出比較快速的及時提示資訊。當Toast被顯示時,雖然它懸浮應用程式最上方,但是並未獲得焦點。它的設計就是為了提示有用的資訊,而不打擾使用者其他動作。最使用簡單: 1       // 第一個參數:當前的上下文環境。可用getApplicationContext()或this  2          // 第二個參數:要顯示的字串。也可是R.string中字串ID  3          // 第三個參數:顯示的時間長短。Toast預設的有兩個LENGTH_LONG(長)和LENGTH_SHORT(短),也可以使用毫秒,如2000ms4          toast = Toast.makeText(getApplicationContext(), "簡單的Toast",Toast.LENGTH_LONG).show();當然你也可以設定圖片和顯示位置: 複製代碼 1             toast = Toast.makeText(getApplicationContext(), "可選位置帶圖片的Toast", 2                     Toast.LENGTH_LONG); 3             //第一個參數:設定toast在螢幕中顯示的位置。我現在的設定是置中靠頂   4             //第二個參數:相對於第一個參數設定toast位置的橫向X軸的位移量,正數向右位移,負數向左位移   5             //第三個參數:同的第二個參數道理一樣   6             //如果你設定的位移量超過了螢幕的範圍,toast將在螢幕內靠近超出的那個邊界顯示 7             toast.setGravity(Gravity.CENTER, -50, 100); 8             //獲得toast的布局  9             LinearLayout toastView = (LinearLayout) toast.getView();10             //設定圖片 11             ImageView imageCodeProject = new ImageView(getApplicationContext());12             imageCodeProject.setImageResource(R.drawable.ic_launcher);13             //添加圖片 14             toastView.addView(imageCodeProject, 0);15             toast.show();複製代碼    或者自訂Toast顯示: 複製代碼 1             //LayoutInflater這個類用來執行個體化XML檔案到其相應的視圖對象的布局 2             LayoutInflater inflater = getLayoutInflater(); 3             View layout = inflater.inflate(R.layout.custom, 4                     (ViewGroup) findViewById(R.id.llToast)); 5             ImageView image = (ImageView) layout 6                     .findViewById(R.id.tvImageToast); 7             image.setImageResource(R.drawable.ic_launcher); 8             TextView title = (TextView) layout.findViewById(R.id.tvTitleToast); 9             //設定標題10             title.setText("Attention");11             TextView text = (TextView) layout.findViewById(R.id.tvTextToast);12             //設定內容13             text.setText("完全自訂Toast");14             toast = new Toast(getApplicationContext());15             toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);16             toast.setDuration(Toast.LENGTH_LONG);17             toast.setView(layout);18             toast.show();複製代碼    Toast提示訊息也可以來自其他線程: 複製代碼 1     Handler handler = new Handler(); 2     public void showToast() { 3         handler.post(new Runnable() { 4  5             @Override 6             public void run() { 7                 Toast.makeText(getApplicationContext(), "我來自其他線程!", 8                         Toast.LENGTH_SHORT).show(); 9 10             }11         });12     }複製代碼    2、Notification  Notification位於手機狀態列。狀態列位於手機螢幕的最上層,通常顯示電池電量、訊號強度等資訊。按住狀態列往下拉就可以開啟查看系統提示資訊。   如果要添加一個Notification,需要瞭解NotificationManager和Notification a、NotificationManager(通知管理器)NotificationManager負責通知使用者事件的發生. NotificationManager有三個公用方法: 1. cancel(int id) 取消以前顯示的一個通知.假如是一個短暫的通知,試圖將隱藏,假如是一個持久的通知,將從狀態條中移走. 2. cancelAll() 取消以前顯示的所有通知. 3. notify(int id, Notification notification) 把通知持久的發送到狀態條上. b、Notification代表著一個通知Notification的屬性: audioStreamType 當聲音響起時,所用的音頻流的類型 contentIntent 當通知條目被點擊,就執行這個被設定的Intent. contentView 當通知被顯示在狀態條上的時候,同時這個被設定的視圖被顯示. defaults 指定哪個值要被設定成預設的. deleteIntent 當使用者點擊"Clear All Notifications"按鈕區刪除所有的通知的時候,這個被設定的Intent被執行. icon 狀態條所用的圖片. iconLevel 假如狀態條的圖片有幾個層級,就設定這裡. ledARGB LED燈的顏色. ledOffMS LED關閉時的閃光時間(以毫秒計算) ledOnMS LED開始時的閃光時間(以毫秒計算) number 這個通知代表事件的號碼 sound 通知的聲音 tickerText 通知被顯示在狀態條時,所顯示的資訊 vibrate 震動模式. when 通知的時間戳記. 完整設定Notification代碼: 複製代碼 1                 Intent i = new Intent(MainActivity.this, NotifiedActivity.class); 2                 // pendingIntent是一種特殊的Intent。主要的區別在於Intent的執行立刻的,而pendingIntent的執行不是立刻的 3                 PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, i, 0); 4                 //建立一個Notification對象 5                 Notification myNotification = new Notification();     6                 //Notification的表徵圖 7                 myNotification.icon=R.drawable.header;                 8                 //Notification的顯示內容 9                 myNotification.tickerText="點擊查看";            10                 //通知時發出的預設聲音11                 myNotification.defaults=Notification.DEFAULT_SOUND;12                 //設定通知顯示的參數13                 myNotification.setLatestEventInfo(MainActivity.this, "樣本", "點擊查看", pi);14                 //通知管理器15                 NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);16                 //發送Notification17                 notificationManager.notify(0, myNotification);   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.