應用示例如下: ...
應用示例如下:
/* * 提示類型dialog */ private void dialog1(){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("test"); builder.setMessage("Is it exit?"); //設置內容 builder.setIcon(R.mipmap.ic_launcher);//設置圖標 builder.setPositiveButton("ok", new DialogInterface.OnClickListener() { //which:點擊ok,which是-1 //點擊cancel,which是-3 //點擊ignore,which是-2 @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.setNeutralButton("ignore", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); //顯示dialog builder.create().show(); } private void dialog1(){ DialogInterface.OnClickListener dialogOnclicListener=new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { switch(which){ case Dialog.BUTTON_POSITIVE: break; case Dialog.BUTTON_NEGATIVE: break; case Dialog.BUTTON_NEUTRAL: break; } } } }; AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("test"); builder.setMessage("Is it exit?"); builder.setIcon(R.mipmap.ic_launcher); builder.setPositiveButton("ok",dialogOnclicListener); builder.setNegativeButton("cancel", dialogOnclicListener); builder.setNeutralButton("ignore", dialogOnclicListener); builder.create().show(); } /* * 列表類型dialog */ private void dialog2() { final String items[]={"test1","test2","test3"}; AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("test"); builder.setIcon(R.mipmap.ic_launcher); //設置列表顯示,註意設置了列表顯示就不要設置builder.setMessage()了,否則列表不起作用。 builder.setItems(items,new DialogInterface.OnClickListener() { //which是items數組的下標 @Override public void onClick(DialogInterface dialog, int which) { } }); builder.setPositiveButton("ok",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create().show(); } /* * 單選類型dialog */ private void dialog3(){ final String items[]={"single","double"}; AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("test"); //0表示預設選中是第一個 builder.setSingleChoiceItems(items,0,new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.setPositiveButton("ok",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create().show(); } /* * 多選類型dialog */ private void dialog4(){ final String items[]={"North Korea","American","Syria"}; final boolean selected[]={true,false,true}; AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("test"); //selected是預設的對應的選中狀態。當你選擇時,系統會自動幫你把selected中的值做相應改變,所以在確定按鈕中可以得到所有的選擇狀態 builder.setMultiChoiceItems(items,selected,new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { } }); builder.setPositiveButton("ok",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //android會自動根據你選擇的改變selected數組的值。 } }); builder.create().show(); }