官术网_书友最值得收藏!

4.8 Toast和Notification

Toast和Notification是Android系統為用戶提供的輕量級的信息提醒機制。這種方式不會打斷用戶當前的操作,也不會獲取到焦點,非常方便。

本節我們通過實例學習Toast和Notification的使用方法。

4.8.1 Toast

創建工程NotificationDemo,并實現如圖4.44布局。

圖4.44 工程布局

main.xml代碼如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">



       <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Toast和Notification演示" />



       <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Toast" />



       <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Notification" />



       <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CancelNotification" />



    </LinearLayout>

在NotificationDemoActivity中為每個按鈕添加事件響應。單擊Toast按鈕,運行效果如圖4.45所示。

圖4.45 單擊Toast按鈕的效果

相關代碼如下:

    Button toastBtn=(Button)this.findViewById(R.id.button1);
            toastBtn.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    // TODO Auto-generated method stub
                    Toast.makeText(NotificationDemoActivity.this, "這是一個Toast演示!", Toast.LENGTH_LONG).show();
                }
            });

Toast用于向用戶顯示小信息量的提示,它不會中斷應用程序進程,不會對用戶操作造成任何干擾,也不能與用戶交互,在信息顯示后會自動消失。此處使用Toast.makeText(Context context, CharSequence text, int duration)方法來創建一個Toast。其中context指顯示Toast的上下文,text指Toast中顯示的文字內容,duration指Toast顯示延續的時間,該時間可以直接指定,也可以使用Toast提供LENGTH_LONG和LENGTH_SHORT常量。Toast.show()方法可以將Toast對象顯示出來。Toast默認情況下顯示在屏幕的下方,可以通過Toast.setGravity()方法設置Toast的顯示位置。例如,如下代碼:

    Toast toast=Toast.makeText(NotificationDemoActivity.this,
                             "這是一個位于中間位置的Toast", Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();

顯示效果如圖4.46所示。

圖4.46 顯示效果

4.8.2 Notification

Notification可以在手機屏幕頂部的狀態欄顯示一個帶圖標的通知,同時播放聲音或者使手機震動。Notification可以擴展以顯示詳細信息,單擊該Notification還可以跳轉到特定的Activity。

單擊Notification按鈕,運行效果如圖4.47所示,在視圖的狀態欄出現Notification提示。按住Notification并下拉,可將Notification內容進行擴展,效果如圖4.48所示。單擊圖標處,應用程序跳轉到NoteActivity視圖,運行效果如圖4.49所示。單擊“返回”按鈕,返回到NotificationDemoActivity視圖。

圖4.47 單擊Notification 按鈕的效果

圖4.48 下拉Notification 的效果

圖4.49 單擊圖標的效果

相關代碼如下:

    Button notifyBtn=(Button)this.findViewById(R.id.button2);
        notifyBtn.setOnClickListener(new View.OnClickListener(){



            @Override
            public void onClick(View v){
                // TODO Auto-generated method stub
                context=getApplicationContext();
                String ns=Context.NOTIFICATION_SERVICE;
                mNotificationManager=(NotificationManager)getSystemService(ns);



                int icon=R.drawable.icon01;
                CharSequence tickerText="這是一個Notification!";
                long when=System.currentTimeMillis();
                Notification.Builder builder=new Notification.Builder(context);
                builder.setSmallIcon(icon);
                builder.setTicker(tickerText);
                builder.setWhen(when);
                notification=builder.getNotification();
                CharSequence contentTitle="My notification";
                CharSequence contentText="單擊這個notification,可以跳轉到NoteActivity.";
                Intent notificationIntent=new Intent(context, NoteActivity.class);
                PendingIntent contentIntent=PendingIntent.getActivity(context, 0, notificationIntent, 0);
                notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
                notification.defaults=notification.DEFAULT_SOUND;
                mNotificationManager.notify(NOTIFICATION_ID, notification);
            }
    });

Notification.Builder是Android API Level 11以上版本提供的Notification的創建類,可以方便地創建Notification并設置各種屬性。此處創建了一個Notification,并指定了顯示內容和圖標。Notification.setLatestEventInfo()方法設定了當用戶擴展Notification時顯示的樣式,并通過PendingIntent對象指定了當用戶單擊擴展的Notification時應用程序如何跳轉,此處跳轉至NoteActivity。NotificationManager.notify(int id,Notification notification)方法為Notification對象指定一個id值,并將該Notification對象顯示到狀態欄上。NotificationManager.cancel(int id)方法會將id指向的Notification對象取消掉。

NoteActivity.java代碼如下:

    package introduction.android.notificationDemo;
     import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;
     public class NoteActivity extends Activity {
         private Button btn;



        public void onCreate(Bundle savedInstanceState){
                super.onCreate(savedInstanceState);
                setContentView(R.layout.other);
                btn=(Button)this.findViewById(R.id.button1);
                btn.setOnClickListener(new View.OnClickListener(){



                    @Override
                    public void onClick(View v){
                        // TODO Auto-generated method stub
                        Intent intent=new Intent(NoteActivity.this,NotificationDemoActivity.class);
                        startActivity(intent);
                    }
                });
         }
    }

NoteActivity所使用的布局文件other.xml代碼如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">



       <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="NoteActivity" />



       <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="返回" />



    </LinearLayout>
主站蜘蛛池模板: 陇西县| 建湖县| 门头沟区| 家居| 皋兰县| 红安县| 绵竹市| 民权县| 舟山市| 邓州市| 会泽县| 新丰县| 双桥区| 江津市| 平果县| 瑞昌市| 靖江市| 措美县| 广丰县| 南城县| 芜湖县| 陆良县| 洪雅县| 灌云县| 彰化市| 青铜峡市| 健康| 韶山市| 崇州市| 桦川县| 扶风县| 泊头市| 和静县| 湖州市| 张家界市| 鹿泉市| 台湾省| 庐江县| 中牟县| 双峰县| 大洼县|