文章出處
文章列表
單選對話框,顧名思義就是只能選一項(setSingleChoiceItems(Items,))
1 public void click(View v){ 2 //創建對話框類 3 AlertDialog.Builder builder = new AlertDialog.Builder(this); 4 //定義item選項 5 final String items[] = new String[]{"一只","兩只","三只"}; 6 builder.setTitle("沒有什么事是一只口紅解決不了的,如果有那就兩只")//設置標題 7 .setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {//設置單選 8 @Override 9 public void onClick(DialogInterface dialogInterface, int i) { 10 Toast.makeText(MainActivity.this,"你選擇了用"+items[i]+"口紅解決問題",Toast.LENGTH_SHORT).show(); 11 } 12 }).setPositiveButton("是的", new DialogInterface.OnClickListener() { 13 @Override 14 public void onClick(DialogInterface dialogInterface, int i) { 15 Toast.makeText(MainActivity.this,"我選擇了是",Toast.LENGTH_SHORT).show(); 16 } 17 }).setNegativeButton("取消", new DialogInterface.OnClickListener() { 18 @Override 19 public void onClick(DialogInterface dialogInterface, int i) { 20 Toast.makeText(MainActivity.this,"我選擇了取消,因為我相信口紅解決不了",Toast.LENGTH_SHORT).show(); 21 } 22 }).show(); 23 }
下面是多選對話框
1 public void click1(View v){ 2 AlertDialog.Builder builder = new AlertDialog.Builder(this); 3 final String items[] = {"First","Second","Thrid","Fourth"}; 4 final boolean boo[] = {true,false,false,false}; 5 6 builder.setTitle("人生有多種選擇") 7 .setMultiChoiceItems(items, boo, new DialogInterface.OnMultiChoiceClickListener() { 8 @Override 9 public void onClick(DialogInterface dialogInterface, int i, boolean b) { 10 11 } 12 }).setPositiveButton("確定", new DialogInterface.OnClickListener() { 13 @Override 14 public void onClick(DialogInterface dialogInterface, int i) { 15 16 } 17 }).show(); 18 }
單選是setSingleChoiceItems(),多選是setMultiChoiceItems(),
進度條對話框
1 public void click2(View v){ 2 final ProgressDialog pd = new ProgressDialog(this); 3 pd.setTitle("加載"); 4 pd.setProgress(0); 5 pd.setMax(10); 6 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 7 pd.show(); 8 new Thread(){ 9 @Override 10 public void run() { 11 super.run(); 12 for (int i=0; i<=10; i++){ 13 pd.setProgress(i); 14 try { 15 Thread.sleep(1000); 16 } catch (InterruptedException e) { 17 e.printStackTrace(); 18 } 19 } 20 } 21 }.start(); 22 }
文章列表
全站熱搜