1. 程式人生 > >【Android】AlertDialog PopupWindow對話方塊

【Android】AlertDialog PopupWindow對話方塊

Android最常見的對話方塊是

AlertDialog彈窗以及PopupWindow浮動對話方塊

一 . AlertDialog對話方塊

-> 帶訊息、帶按鈕的提示對話方塊 -> 帶列表、帶按鈕的列表對話方塊 -> 帶多個單選列表項、N個按鈕的對話方塊 -> 帶多個多選列表項、N個按鈕的對話方塊 另外AlertDialog還可以建立自定義對話方塊 使用AlertDialog建立對話方塊的大致步驟是 1)建立AlertDialog.Builder物件,該物件是AlertDialog的建立器 2)呼叫AlertDialog.Builder的方法設定圖示、標題、內容,以及按鈕 3)呼叫AlertDialog.Builder的create() 方法建立對話方塊 4)呼叫AlertDialog.Builder的show() 方法顯示對話方塊

1》顯示提示訊息的對話方塊


                Builder builder=new AlertDialog.Builder(DialogActivity.this);
                builder.setIcon(R.drawable.ic_launcher);
                builder.setTitle("訊息對話方塊");
                builder.setMessage("一個簡單提示");
                builder.setPositiveButton("確定", new OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(DialogActivity.this, "點選了確定",1).show();
                    }
                    
                });
                builder.setNegativeButton("取消", new OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(DialogActivity.this, "點選了取消",1).show();
                    }
                    
                });
                builder.create().show();

2》列表對話方塊


                Builder builder=new AlertDialog.Builder(DialogActivity.this);
                builder.setIcon(R.drawable.ic_launcher);
                builder.setTitle("列表對話方塊");
                builder.setItems(new String[]{  "紅色","藍色","綠色" }, new OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        TextView textView=(TextView)findViewById(R.id.textView_show_color);
                        switch(which){
                        case 0:
                            textView.setBackgroundColor(Color.RED);break;
                        case 1:
                            textView.setBackgroundColor(Color.BLUE);break;
                        case 2:
                            textView.setBackgroundColor(Color.GREEN);break;
                        default:
                            textView.setBackgroundColor(Color.BLACK);
                        }
                    }
                    
                });
                
                builder.create().show();

3》單選對話方塊


                int choose=0;
                Builder builder=new AlertDialog.Builder(DialogActivity.this);
                builder.setIcon(R.drawable.ic_launcher);
                builder.setTitle("列表單選對話方塊");
                
                builder.setSingleChoiceItems(new String[]{ "灰色","孔雀藍","黃色" }, choose,new OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        choose=which;
                    }
                    
                });
                builder.setPositiveButton("確定", new OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        TextView textView=(TextView)findViewById(R.id.textView_show_color);
                        switch(choose){
                        case 0:
                            textView.setBackgroundColor(Color.GRAY);break;
                        case 1:
                            textView.setBackgroundColor(Color.CYAN);break;
                        case 2:
                            textView.setBackgroundColor(Color.YELLOW);break;
                        default:
                            textView.setBackgroundColor(Color.BLACK);  
                        }
                    }
                    
                });
                builder.create().show();

4》多選對話方塊


                Builder builder=new AlertDialog.Builder(DialogActivity.this);
                final String myColor[]={ "蘋果","香蕉","橘子" };
                final boolean isChoose[]={true,false,true};
                builder.setIcon(R.drawable.ic_launcher);
                builder.setTitle("列表多選對話方塊");
                
                builder.setMultiChoiceItems(myColor,isChoose, new OnMultiChoiceClickListener(){

                    //選中某一選項的監聽事件
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        // TODO Auto-generated method stub
                        Toast.makeText(DialogActivity.this, myColor[which]+isChecked,1).show();
                    }
                    
                });
                builder.setPositiveButton("確定", null);
                builder.create().show();


5》自定義對話方塊


Java程式碼
                Builder builder=new AlertDialog.Builder(DialogActivity.this);
                builder.setIcon(R.drawable.ic_launcher);
                builder.setTitle("自定義登入對話方塊");
                
                TableLayout loginForm=(TableLayout)getLayoutInflater().inflate(R.layout.login, null);
                builder.setView(loginForm);//設定對話方塊為自定義佈局
                
                builder.setPositiveButton("登入", new OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        
                    }
                    
                });
                builder.setNegativeButton("取消", new OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        
                    }
                    
                });
                builder.create().show();

登入框的佈局檔案R.layout.login, 可自己隨意定義
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">
    <TableRow >
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="使用者名稱:"
            android:textSize="10pt"/>
        <EditText 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="請填寫賬號"
            android:selectAllOnFocus="true"/>
    </TableRow>
    
    <TableRow >
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="密碼:"
            android:textSize="10pt"/>
        <EditText 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:password="true"/>"
    </TableRow>
    
    <TableRow >
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="電話號碼:"
            android:textSize="10pt"/>
        <EditText 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="填寫電話號碼"
            android:phoneNumber="true"
            android:selectAllOnFocus="true"/>
    </TableRow>

    <TableRow >
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="註冊"/>
    </TableRow>
</TableLayout>


二. PopupWindow浮動對話方塊

介面上定義一個Button
點選後PopupWindow挨著button出現

        View root=this.getLayoutInflater().inflate(R.layout.popup, null);
        //建立popupWindows物件
        final PopupWindow popup=new PopupWindow(root,280,200);
        Button button=(Button)findViewById(R.id.button_show_popup);
        button.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                popup.showAsDropDown(v);//顯示對話方塊
            }
            
        });
        
        root.findViewById(R.id.button_close).setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                popup.dismiss();//關閉對話方塊
            }
            
        });

PopupWIndow的佈局R.layout.popup
<?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"
    android:background="#2E2E2E">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <CheckedTextView
        android:id="@+id/checkedTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckedTextView" />

    <Button
        android:id="@+id/button_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="close" />

</LinearLayout>

三. 總結

AlertDialog和PopupWindow基本能滿足Android開發中對話方塊的需求, 另外安卓還提供了DatePickerDialog(日期選擇對話方塊)和TimePickerDialog(時間選擇對話方塊),以及ProgressDialog(進度對話方塊)