Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android 常見對話框,android對話框

Android 常見對話框,android對話框

編輯:關於android開發

Android 常見對話框,android對話框


1、對話框通知(Dialog Notification)

當你的應用需要顯示一個進度條或需要用戶對信息進行確認時,可以使用對話框來完成。

下面代碼將打開一個如圖所示的對話框:

    public void click1(View view) {
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("工學1號館");
        builder.setIcon(R.drawable.ic_launcher);
        builder.setMessage("浏覽wuyudong的博客?");
        builder.setPositiveButton("確定", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Uri uri = Uri.parse("http://www.wuyudong.com/");// 打開鏈接
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);

            }
        });
        builder.setNegativeButton("取消", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                 dialog.cancel();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

本文地址:http://www.cnblogs.com/wuyudong/p/5854896.html,轉載請注明源地址。

2、創建帶單選項列表的對話框

    public void click2(View view) {
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("單選對話框");
        final String[] items = new String[] { "java", ".net", "php" };

        builder.setSingleChoiceItems(items, 0, new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, items[which] + "被點擊了", 0)
                        .show();
            }
        });
        builder.show();

    }

3、創建帶多選項列表的對話框

    public void click3(View view) {
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("多選對話框");
        final String[] items = new String[] { "java", ".net", "php", "C++" };

        builder.setMultiChoiceItems(items, new boolean[] { true, false, false,
                true }, new OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which,
                    boolean isChecked) {
                Toast.makeText(MainActivity.this, items[which] + isChecked, 0)
                        .show();

            }
        });
        builder.setNegativeButton("取消", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.show();

    }

 4、進度對話框(ProgressDialog)

使用代碼ProgressDialog.show(ProgressDialogActivity.this, "請稍等", "數據正在加載中...", true);創建並顯示一個進度對話框。
調用setProgressStyle()方法設置進度對話框風格。有兩種風格:
ProgressDialog.STYLE_SPINNER 旋體進度條風格 (為默認風格)
ProgressDialog.STYLE_HORIZONTAL 橫向進度條風格

    public void click4(View view) {
        ProgressDialog pd = new ProgressDialog(this);
        pd.setTitle("提醒");
        pd.setMessage("正在加載中......");
        pd.show();
    }

下面的代碼實現橫向進度條風格:

    public void click5(View view) {
        final ProgressDialog pd = new ProgressDialog(this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMax(100);
        pd.show();

        new Thread() {
            public void run() {
                for (int i = 0; i < 100; i++) {
                    pd.setProgress(i);
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                pd.dismiss();
            };
        }.start();

    }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved