iT邦幫忙

2022 iThome 鐵人賽

DAY 14
0

(1) 普通對話框(2顆按鈕)

AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setIcon(R.drawable.ic_baseline_mood); //設置圖示
dialog.setTitle("標題");
dialog.setMessage("內文");
//設置左邊按鈕和點擊事件
dialog.setNegativeButton("關閉", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        // TODO: ...
    }
});
//設置右邊按鈕和點擊事件
dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        // TODO: ...
    }
});
//顯示Dialog
dialog.show();
  • setNegativeButton 設置左邊按鈕
  • setPositiveButton 設置右邊按鈕

(2) 普通對話框(3顆按鈕)

AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("標題");
dialog.setMessage("這是內文");
dialog.setIcon(R.drawable.ic_baseline_mood);
dialog.setNeutralButton("按鈕1", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        //TODO: ...
    }
});

dialog.setNegativeButton("按鈕2", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        //TODO: ...
    }
});

dialog.setPositiveButton("按鈕3", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        //TODO: ...
    }
});
dialog.show(); // 顯示dialog
  • setNeutralButton 設置最左邊的按鈕
  • setNegativeButton 設置中間的按鈕
  • setPositiveButton 設置右邊的按鈕

(3) 列表對話框

String[] items = {"Item1", "Item2", "Item3", "Item4"};
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("標題_列表Dialog");
// 設置item點擊事件處理
dialog.setItems(items, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        Toast.makeText(MainActivity.this, "你點擊了" + items[i], Toast.LENGTH_SHORT).show();
    }
});
dialog.show(); // 顯示dialog
  • setItems 設置列表項

(4) 單選對話框

choice = 1; //預設選擇Item2
String[] items = {"Item1", "Item2", "Item3", "Item4"};
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
// 參數1:設置選項List;  參數2:設置預設選中項
dialog.setSingleChoiceItems(items, choice, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        choice = i;
    }
});
dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        Toast.makeText(MainActivity.this, "你選了" + items[choice], Toast.LENGTH_SHORT).show();
    }
});
dialog.show(); // 顯示dialog
  • setSingleChoiceItems 設置單選列表項

(5) 複選對話框

String[] items = {"Item1", "Item2", "Item3", "Item4"};
// 設置默認選中項, false表示未選中, true反之
boolean choiceSets[] = {false, true, false, false};
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("標題");
dialog.setMultiChoiceItems(items, choiceSets,
    new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i, boolean b) {
            choiceSets[i] = b;
        }
    }
);
dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        String result = "";
        for (int k = 0; k < items.length; k++) {
            if (choiceSets[k]) {
                result += items[k] + "\\";
            }
        }
        Toast.makeText(MainActivity.this, "你選中了" + result, Toast.LENGTH_SHORT).show();
    }
});
dialog.show(); // 顯示dialog
  • setMultiChoiceItems 設置複選列表項

(6) 等待型對話框

ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("進度");
dialog.setMessage("等待中...");
dialog.show();

(7) 進度條對話框

ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("載入中...");
//關閉
dialog.setCancelable(false);
//設置進度條樣式 - 水平
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100); // 設定進度條最大值

// 模擬進度條跑動
progressValue = 0;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        progressValue += new Random().nextInt(10);
        dialog.setProgress(progressValue);
        if (progressValue > 100) {
            timer.cancel();
            dialog.dismiss(); //關閉對話框
        }
    }
},0,200);
dialog.show();

選擇型對話框

(8) 日期選擇對話框

Calendar calendar = Calendar.getInstance();
DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
    // 當日期選擇完畢並按下"OK"按鈕 的事件觸發處理
    @Override
    public void onDateSet(DatePicker datePicker, int y, int m, int d) {
        StringBuilder result = new StringBuilder();
        result.append(y).append("-").append(m+1).append("-").append(d);
        Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
    }
},calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
dialog.show();

(9) 時間選擇對話框

  • 按下右下角按鈕可切換樣式
Calendar calendar = Calendar.getInstance();
TimePickerDialog dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
    // 當時間選擇完畢並按下"OK"按鈕 的事件觸發處理
    @Override
    public void onTimeSet(TimePicker timePicker, int h, int m) {
        StringBuilder result = new StringBuilder();
        result.append("H:").append(h).append("-M:").append(m);
        Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
    }
}, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
dialog.show();

上一篇
Day13 - RecyclerView 進階操作 | 拖曳排序、左右刪除、復原操作
下一篇
Day15 - Android - MVC、MVP、MVVM 設計架構
系列文
Android 開發 30天 初學之路筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言