Android 程式開發:(一)詳解活動 —— 1.4 顯示普通對話方塊

來源:互聯網
上載者:User

有的時候,可能需要彈出一個對話方塊,以便從使用者的輸入來擷取某些確認資訊。這種情況下,可以重寫Activity基類中的受保護方法(protected)onCreateDialog()。
1、建立一個工程:Dialog。
 
2、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" > 
     
<Button 
    android:id="@+id/btn_dialog" 
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content" 
    android:text="Click to display a dialog" 
    android:onClick="onClick" /> 
  
</LinearLayout> 
 
3、DialogActivity.java中的代碼。
 
package net.horsttnann.Dialog; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Toast; 
 
public class DialogActivity extends Activity { 
    CharSequence[] items = { "Google", "Apple", "Microsoft" }; 
    boolean[] itemsChecked = new boolean[items.length]; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    } 
 
    public void onClick(View v) { 
        showDialog(0); 
    } 
 
    @Override 
    protected Dialog onCreateDialog(int id) { 
        switch (id) { 
        case 0: 
            return new AlertDialog.Builder(this) 
                    .setIcon(R.drawable.ic_launcher) 
                    .setTitle("This is a dialog with some simple text...") 
                    .setPositiveButton("OK", 
                            new DialogInterface.OnClickListener() { 
                                public void onClick(DialogInterface dialog, 
                                        int whichButton) { 
                                    Toast.makeText(getBaseContext(), 
                                            "OK clicked!", Toast.LENGTH_SHORT) 
                                            .show(); 
                                } 
                            }) 
                    .setNegativeButton("Cancel", 
                            new DialogInterface.OnClickListener() { 
                                public void onClick(DialogInterface dialog, 
                                        int whichButton) { 
                                    Toast.makeText(getBaseContext(), 
                                            "Cancel clicked!", 
                                            Toast.LENGTH_SHORT).show(); 
                                } 
                            }) 
                    .setMultiChoiceItems(items, itemsChecked, 
                            new DialogInterface.OnMultiChoiceClickListener() { 
                                public void onClick(DialogInterface dialog, 
                                        int which, boolean isChecked) { 
                                    Toast.makeText( 
                                            getBaseContext(), 
                                            items[which] 
                                                    + (isChecked ? " checked!" 
                                                            : " unchecked!"), 
                                            Toast.LENGTH_SHORT).show(); 
 
                                } 
                            }).create(); 
        } 
        return null; 
    } 

 
4、調試。
 
點擊按鈕彈出對話方塊,在CheckBox上面打勾,就會彈出一個Toast提示,顯示選中物件的文本資訊。點擊“OK”或“Cancel”按鈕會使對話方塊消失。
 
 
 
 
 
 


 
 
 
想要顯示對話方塊,首先要重寫Activity基類中的onCreateDialog()方法:
 
@Override  www.2cto.com
protected Dialog onCreateDialog(int id) { 
    // ... 

當調用showDialog()的時候,上面被重寫的方法就被調用了:public void onClick(View v) { 
    showDialog(0); 

這個建立對話方塊的onCreateDialog()方法是一個被Activity控制的回呼函數,當調用showDialog()時,onCreateDialog()回呼函數就被觸發了。showDialog()方法接受一個Integer參數,用來識別到底要顯示哪個對話方塊。一般情況下,使用switch語句去判斷顯示不同的對話方塊。
想要建立一個對話方塊,還需要使用AlertDialog類的Builder構造器,設定不同的屬性,比標、標題、按鈕、單選框等等:
 
    @Override 
    protected Dialog onCreateDialog(int id) { 
        switch (id) { 
        case 0: 
            Builder builder = new AlertDialog.Builder(this); 
            builder.setIcon(R.drawable.ic_launcher); 
            builder.setTitle("This is a dialog with some simple text..."); 
            builder.setPositiveButton("OK", 
                    new DialogInterface.OnClickListener() { 
                        public void onClick(DialogInterface dialog, 
                                int whichButton) { 
                            Toast.makeText(getBaseContext(), "OK clicked!", 
                                    Toast.LENGTH_SHORT).show(); 
                        } 
                    }); 
 
            builder.setNegativeButton("Cancel", 
                    new DialogInterface.OnClickListener() { 
                        public void onClick(DialogInterface dialog, 
                                int whichButton) { 
                            Toast.makeText(getBaseContext(), "Cancel clicked!", 
                                    Toast.LENGTH_SHORT).show(); 
                        } 
                    }); 
 
            builder.setMultiChoiceItems(items, itemsChecked, 
                    new DialogInterface.OnMultiChoiceClickListener() { 
                        public void onClick(DialogInterface dialog, int which, 
                                boolean isChecked) { 
                            Toast.makeText( 
                                    getBaseContext(), 
                                    items[which] 
                                            + (isChecked ? " checked!" 
                                                    : " unchecked!"), 
                                    Toast.LENGTH_SHORT).show(); 
                        } 
                    }); 
            return builder.create(); 
        } 
        return null; 
    } 
 
 
 

摘自 manoel的專欄
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.