1. 程式人生 > >安卓開發:四種對話框

安卓開發:四種對話框

sleep cal bool mas dism extends you port info

先展示一下四種對話框的效果圖:

普通對話框

技術分享圖片

單選對話框:

技術分享圖片

多選對話框:

技術分享圖片

進度條對話框:

技術分享圖片

一個簡單的布局,放四個按鈕:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
tools:context=".MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click1" android:text="普通對話框" /> <Button android:layout_width="wrap_content" android:layout_height
="wrap_content" android:onClick="click2" android:text="單選對話框" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click3" android:text="多選對話框" /> <Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="click4" android:text="進度條對話框" /> </LinearLayout>

四種對話框:

package org.dreamtech.dialog;

import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    // 普通對話框
    public void click1(View v) {
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("對話框標題");
        builder.setMessage("對話框內容");
        builder.setPositiveButton("確認", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "點擊了確定按鈕",
                        Toast.LENGTH_LONG).show();
            }
        });
        builder.setNegativeButton("取消", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "點擊了取消按鈕",
                        Toast.LENGTH_LONG).show();
            }
        });
        builder.show();
    }

    // 單選對話框
    public void click2(View v) {
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("對話框標題");
        final String items[] = { "選項1", "選項2", "選項3" };
        builder.setSingleChoiceItems(items, -1, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String item = items[which];
                Toast.makeText(getApplicationContext(), "選中" + item,
                        Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }
        });
        builder.show();
    }

    // 多選對話框
    public void click3(View v) {
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("對話框標題");
        final String items[] = { "選項1", "選項2", "選項3" };
        final boolean checkItems[] = { false, false, false };
        builder.setMultiChoiceItems(items, checkItems,
                new OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which,
                            boolean isChecked) {

                    }
                });
        builder.setPositiveButton("確定", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < checkItems.length; i++) {
                    if (checkItems[i]) {
                        String choice = items[i];
                        sb.append(choice + " ");
                    }
                }
                Toast.makeText(getApplicationContext(), "選中" + sb.toString(),
                        Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }
        });
        builder.show();
    }

    // 進度條對話框
    public void click4(View v) {
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setTitle("正在加載");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.show();
        new Thread() {
            public void run() {
                dialog.setMax(100);
                for (int i = 0; i <= 100; i++) {
                    dialog.setProgress(i);
                    SystemClock.sleep(50);
                }
                dialog.dismiss();
            };
        }.start();
    }
}

安卓開發:四種對話框