天天看點

系出名門Android(3) - 對話框(Dialog)和通知(Notification)

<a href="http://webabcd.blog.51cto.com/1787395/341976" target="_blank">[索引頁]</a>

<a href="http://down.51cto.com/data/100088" target="_blank">[源碼下載下傳]</a>

系出名門Android(3) - 對話框(Dialog)和通知(Notification)

介紹

在 Android 中種對話框及各種通知效果的應用

常用對話框的使用,彈出式對話框、日期選擇對話框、時間選擇對話框、進度條對話框  

通知(出現在通知清單)和提示性通知(Toast)的示範

1、常用對話框的示範

res/layout/main.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt; 

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

        android:orientation="vertical" android:layout_width="fill_parent" 

        android:layout_height="fill_parent"&gt; 

        &lt;TextView android:id="@+id/txtMsg" android:layout_width="wrap_content" 

                android:layout_height="wrap_content"&gt;&lt;/TextView&gt; 

        &lt;Button android:id="@+id/btn1" android:layout_width="wrap_content" 

                android:layout_height="wrap_content"&gt;&lt;/Button&gt; 

        &lt;Button android:id="@+id/btn2" android:layout_width="wrap_content" 

        &lt;Button android:id="@+id/btn3" android:layout_width="wrap_content" 

        &lt;Button android:id="@+id/btn4" android:layout_width="wrap_content" 

        &lt;Button android:id="@+id/btn5" android:layout_width="wrap_content" 

        &lt;Button android:id="@+id/btn6" android:layout_width="wrap_content" 

&lt;/LinearLayout&gt;

res/layout/view.xml

        &lt;TextView android:text="我是一個 View" 

                android:layout_width="wrap_content" android:layout_height="wrap_content"&gt; 

        &lt;/TextView&gt; 

res/values/array.xml

&lt;resources&gt; 

        &lt;!-- 

                定義一個名為 ary 的 string 類型的數組 

        --&gt; 

        &lt;string-array name="ary"&gt; 

                &lt;item&gt;項目 1&lt;/item&gt; 

                &lt;item&gt;項目 2&lt;/item&gt; 

        &lt;/string-array&gt; 

&lt;/resources&gt;

Main.java

package com.webabcd.dialog; 

import java.util.Calendar; 

import android.app.Activity; 

import android.app.AlertDialog; 

import android.app.DatePickerDialog; 

import android.app.Dialog; 

import android.app.ProgressDialog; 

import android.app.TimePickerDialog; 

import android.app.DatePickerDialog.OnDateSetListener; 

import android.app.TimePickerDialog.OnTimeSetListener; 

import android.content.DialogInterface; 

import android.content.DialogInterface.OnClickListener; 

import android.os.Bundle; 

import android.view.View; 

import android.widget.DatePicker; 

import android.widget.TextView; 

import android.widget.TimePicker; 

import android.widget.Button; 

public class Main extends Activity { 

        /** Called when the activity is first created. */ 

        @Override 

        public void onCreate(Bundle savedInstanceState) { 

                super.onCreate(savedInstanceState); 

                setContentView(R.layout.main); 

                // 彈出式對話框的 Demo。先調用 Builder(),在 Create(), 需要顯示對話框的是後再調用 show() 

                AlertDialog dialog = new AlertDialog.Builder(this).setTitle("彈出對話框").create(); 

                dialog.show(); 

                // 以下是各種對話框的 Demo 

                MyButtonClickListener listener = new MyButtonClickListener(); 

                Button btn1 = (Button) this.findViewById(R.id.btn1); 

                btn1.setText("簡單的對話框的 Demo"); 

                btn1.setOnClickListener(listener); 

                Button btn2 = (Button) this.findViewById(R.id.btn2); 

                btn2.setText("包括常用設定的對話框(資料來自 xml)"); 

                btn2.setOnClickListener(listener); 

                Button btn3 = (Button) this.findViewById(R.id.btn3); 

                btn3.setText("彈出的對話框的内容是一個 View"); 

                btn3.setOnClickListener(listener); 

                Button btn4 = (Button) this.findViewById(R.id.btn4); 

                btn4.setText("日期選擇對話框"); 

                btn4.setOnClickListener(listener); 

                Button btn5 = (Button) this.findViewById(R.id.btn5); 

                btn5.setText("時間選擇對話框"); 

                btn5.setOnClickListener(listener); 

                Button btn6 = (Button) this.findViewById(R.id.btn6); 

                btn6.setText("進度條對話框"); 

                btn6.setOnClickListener(listener); 

        } 

        class MyButtonClickListener implements View.OnClickListener { 

                @Override 

                public void onClick(View v) { 

                        // 具體的對話框的實作可以通過重寫 onCreateDialog 完成 

                        switch (v.getId()) { 

                        case R.id.btn1: 

                                Main.this.showDialog(0); 

                                break; 

                        case R.id.btn2: 

                                Main.this.showDialog(1); 

                        case R.id.btn3: 

                                Main.this.showDialog(2); 

                        case R.id.btn4: 

                                Main.this.showDialog(3); 

                        case R.id.btn5: 

                                Main.this.showDialog(4); 

                        case R.id.btn6: 

                                Main.this.showDialog(5); 

                        } 

                } 

        public Dialog onCreateDialog(int id) { 

                switch (id) { 

                case 0: 

                        // 一個簡單的彈出對話框 

                        return new AlertDialog.Builder(this).setTitle("這是一個簡單的彈出對話框的 Demo") 

                                        .create(); 

                case 1: 

                        // 一個相對複雜的彈出對話框 

                        return new AlertDialog.Builder(this) 

                                        .setTitle("标題") // 設定标題 

                                        // .setCustomTitle(View) // 以一個 View 作為标題    

                                        .setIcon(R.drawable.icon01) // 設定标題圖檔 

                                        // .setMessage("資訊") // 需要顯示的彈出内容 

                                        .setPositiveButton("确定", new OnClickListener() { // 設定彈框的确認按鈕所顯示的文本,以及單擊按鈕後的響應行為 

                                                @Override 

                                                public void onClick(DialogInterface a0, int a1) { 

                                                        TextView txtMsg = (TextView) Main.this.findViewById(R.id.txtMsg); 

                                                        txtMsg.append("單擊了對話框上的“确認”按鈕\n"); 

                                                } 

                                        }) 

                                        .setItems(R.array.ary, new DialogInterface.OnClickListener() { // 彈框所顯示的内容來自一個數組。數組中的資料會一行一行地依次排列 

                                                public void onClick(DialogInterface dialog,        int which) { 

                                        // 其他常用方法如下 

                                        // .setMultiChoiceItems(arg0, arg1, arg2) 

                                        // .setSingleChoiceItems(arg0, arg1, arg2) 

                                        // .setNeutralButton(arg0, arg1) 

                                        // .setNegativeButton(arg0, arg1) 

                case 2: 

                        // 彈出對話框為指定的 View 的 Demo 

                        return new AlertDialog.Builder(this).setTitle("此對話框的内容是一個 View") 

                                        .setView(this.findViewById(R.layout.view)).create(); 

                case 3: 

                        // 彈出日期選擇對話框 

                        Calendar c = Calendar.getInstance(); 

                        return new DatePickerDialog(this, new OnDateSetListener() { 

                                @Override 

                                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 

                                        TextView txtMsg = (TextView) Main.this.findViewById(R.id.txtMsg); 

                                        txtMsg.append("新設定的日期為:" + String.valueOf(year) + "-" 

                                                        + String.valueOf(monthOfYear) + "-" 

                                                        + String.valueOf(dayOfMonth) + "\n"); 

                                } 

                        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)); 

                case 4: 

                        // 彈出時間選擇對話框 

                        Calendar c2 = Calendar.getInstance(); 

                        return new TimePickerDialog(this, new OnTimeSetListener() { 

                                public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 

                                        txtMsg.append("新設定的時間為:"    

                                                        + String.valueOf(hourOfDay) + ":" 

                                                        + String.valueOf(minute) + "\n"); 

                        }, c2.get(Calendar.HOUR), c2.get(Calendar.MINUTE), true); 

                case 5: 

                        // 彈出進度條對話框 

                        ProgressDialog progress = new ProgressDialog(this); 

                        progress.setMessage("loading..."); 

                        return progress; 

                default: 

                        return null; 

}

2、各種提示效果的示範

                android:layout_height="wrap_content"&gt; 

package com.webabcd.notification; 

import android.app.Notification; 

import android.app.NotificationManager; 

import android.app.PendingIntent; 

import android.content.Context; 

import android.content.Intent; 

import android.util.Log; 

import android.view.LayoutInflater; 

import android.widget.Toast; 

                // 通過 Tost.makeText().show() 來實作提示性的通知效果 

                // 短時間的提示性通知的 Demo 

                btn1.setText("短時間提示"); 

                btn1.setOnClickListener(new Button.OnClickListener() { 

                        public void onClick(View v) { 

                                Toast.makeText(Main.this, "我是短時間提示", Toast.LENGTH_SHORT).show(); 

                }); 

                // 長時間的提示性通知的 Demo 

                btn2.setText("長時間提示"); 

                btn2.setOnClickListener(new Button.OnClickListener() { 

                                Toast.makeText(Main.this, "我是長時間提示", Toast.LENGTH_LONG).show(); 

                // 以一個 View 作為提示性通知的 Demo 

                btn3.setText("以一個 View 做提示"); 

                btn3.setOnClickListener(new Button.OnClickListener() { 

                                View view = inflateView(R.layout.view); 

                                TextView txtMsg = (TextView) view.findViewById(R.id.txtMsg); 

                                txtMsg.setText("提示内容"); 

                                Toast toast = new Toast(Main.this); 

                                toast.setView(view); 

                                toast.setDuration(Toast.LENGTH_LONG); 

                                toast.show(); 

                btn4.setText("發出一個通知(Notification)"); 

                btn4.setOnClickListener(new Button.OnClickListener() { 

                                // 執行個體化通知管理器 

                                NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

                                // 指定單擊通知後所打開的詳細的通知頁面(單擊通知後打開 NotificationView) 

                                PendingIntent contentIntent = PendingIntent.getActivity( 

                                                Main.this, 0, new Intent(Main.this,        NotificationView.class), 0); 

                                // 執行個體化一個通知,并指定其圖示和标題(在提示欄上顯示) 

                                Notification n = new Notification(R.drawable.icon01, "我是滾動的通知資訊我是滾動的通知資訊我是滾動的通知資訊", System.currentTimeMillis()); 

                                // 設定通知的發送人和通知的詳細内容(打開提示欄後在通知清單中顯示) 

                                n.setLatestEventInfo(Main.this, "通知發送人", "我是詳細的通知資訊我是詳細的通知資訊我是詳細的通知資訊", contentIntent); 

                                // 100 毫秒延遲後,震動 250 毫秒,暫停 100 毫秒後,再震動 500 毫秒 

                                n.vibrate = new long[] { 100, 250, 100, 500 }; 

                                // 發出通知(其中第一個參數為通知辨別符) 

                                nm.notify(0, n); 

        // 将指定的 xml 資源轉換為一個 View 

        private View inflateView(int resource) { 

                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

                return vi.inflate(resource, null); 

        // 打開詳細通知頁後此 Activity 會被 Pause,從詳細通知頁傳回後此 Activity 會被 Resume 

        protected void onPause() { 

                // TODO Auto-generated method stub 

                super.onPause(); 

                Log.d("MyDebug", "onPause"); 

        protected void onResume() { 

                super.onResume(); 

                Log.d("MyDebug", "onResume"); 

NotificationView.java

// 單擊通知清單的某個通知後,所打開的詳細的通知頁 

public class NotificationView extends Activity { 

        protected void onCreate(Bundle savedInstanceState) { 

                setContentView(R.layout.view); 

                TextView txtMsg = (TextView)this.findViewById(R.id.txtMsg); 

                txtMsg.setText("點通知之後所連結到的 Activity"); 

                NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

                // 取消顯示在通知清單中的指定通知(參數為通知辨別符) 

                nm.cancel(0); 

                // 需要關閉此 Activity 的話就 finish 它既可 

                // this.finish(); 

OK

     本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/342014,如需轉載請自行聯系原作者

繼續閱讀