package com.example.andday11customdialog; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.provider.Settings; import android.view.View; import android.widget.SimpleAdapter; import android.widget.TextView; public class MainActivity extends Activity { private TextView show_text; private int postion;// 全局變量記錄選中的位置 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show_text = (TextView) this.findViewById(R.id.show_text); } // 簡單列表對話框 public void openSimple(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("請選擇顏色"); builder.setIcon(R.drawable.ic_launcher); String[] color = { "紅色", "藍色", "黃色" }; builder.setItems(color, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: show_text.setTextColor(Color.RED); break; case 1: show_text.setTextColor(Color.BLUE); break; case 2: show_text.setTextColor(Color.YELLOW); break; } } }); builder.show(); } // 單選列表對話框,注意監聽方法變了 public void openSigleDialog(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("請選擇字體大小"); builder.setIcon(R.drawable.ic_launcher); String[] font = { "20sp", "30sp", "40sp" }; // 注意:單選列表對話框監聽方法變了,跟前面有點區別 builder.setSingleChoiceItems(font, 0, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { postion = which; } }); // 單選列表它有一個確認按鈕,確認按了哪一個按鈕的事件內容 builder.setPositiveButton("肯定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (postion) { case 0: show_text.setTextSize(20); break; case 1: show_text.setTextSize(30); break; case 2: show_text.setTextSize(40); break; } } }); builder.show(); } // 多選列表對話框,注意監聽方法變了 public void openMultDialog(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("請選擇愛好"); builder.setIcon(R.drawable.ic_launcher); final String[] hobbies = { "籃球", "游泳", "跑步" }; final boolean[] postions = new boolean[hobbies.length];// 記錄選中項的狀態 // 注意:多選列表對話框監聽方法也變了 builder.setMultiChoiceItems(hobbies, null, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { postions[which] = isChecked;// 記錄的位置,無論有沒有選中 } }); builder.setPositiveButton("肯定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < hobbies.length; i++) { if (postions[i]) {// 循環記錄的位置 sb.append(hobbies[i] + " "); } } show_text.setText(sb); } }); builder.show(); } // 帶圖標的列表對話框,要跟適配器搭配使用,並且監聽方法是builder.setAdapter public void openAdapterList(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("系統設置"); builder.setIcon(R.drawable.ic_launcher); String[] optionSettings = { "系統設置", "藍牙設置", "WIFI設置", "日期設置", "聲音設置" }; int[] optionImage = { R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, R.drawable.img05 }; List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); for (int i = 0; i < optionImage.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("image", optionImage[i]); map.put("name", optionSettings[i]); data.add(map); } SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.settings, new String[] { "image", "name" }, new int[] { R.id.setting_image, R.id.setting_name }); // 注意監聽方法變了 builder.setAdapter(adapter, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(); switch (which) { case 0: intent.setAction(Settings.ACTION_SETTINGS); break; case 1: intent.setAction(Settings.ACTION_BLUETOOTH_SETTINGS); break; case 2: intent.setAction(Settings.ACTION_WIFI_SETTINGS); break; case 3: intent.setAction(Settings.ACTION_DATE_SETTINGS); break; case 4: intent.setAction(Settings.ACTION_SOUND_SETTINGS); break; } startActivity(intent); } }); builder.show(); } } //