目录
- 普通对话框
- 列表对话框
- 单选对话框
- 多选对话框
- 参考文章
普通对话框
private void showAlertDialogShow(){ AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); alertDialog.setIcon(R.mipmap.ic_launcher); alertDialog.setTitle("确定选择?"); alertDialog.setMessage("........"); alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"三连支持",Toast.LENGTH_SHORT).show(); } }); alertDialog.setNegativeButton("拒绝", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"下次一定",Toast.LENGTH_SHORT).show(); } }); alertDialog.setNeutralButton("忽视", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"假装看不见",Toast.LENGTH_SHORT).show(); } }); alertDialog.show(); }
列表对话框
private void showListDialogShow(){ AlertDialog.Builder listDialog = new AlertDialog.Builder(MainActivity.this); listDialog.setTitle("谁是最强的男人?"); listDialog.setIcon(R.mipmap.ic_launcher); final String items[] = {"A、折木奉太郎","B、空调承太郎","C、野比大雄"}; listDialog.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,items[which],Toast.LENGTH_SHORT).show(); } }); listDialog.show(); }
单选对话框
int choice; private void showSingleChoiceDialogShow(){ final String items[] = {"A、折木奉太郎","B、空调承太郎","C、野比大雄"}; AlertDialog.Builder singleDialog = new AlertDialog.Builder(MainActivity.this); singleDialog.setTitle("谁是最强的男人?"); singleDialog.setIcon(R.mipmap.ic_launcher); //参数2为默认选项 singleDialog.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { choice = which; } }); singleDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,items[choice],Toast.LENGTH_SHORT).show(); } }); singleDialog.show(); }
多选对话框
ArrayList<Integer> choices= new ArrayList<>(); private void showMultiDialogShow(){ choices.clear(); final boolean isChecked[] = {false,false,false}; final String items[] = {"A、折木奉太郎","B、空调承太郎","C、野比大雄"}; AlertDialog.Builder multiDialog = new AlertDialog.Builder(MainActivity.this); multiDialog.setTitle("谁是最强的男人?"); multiDialog.setIcon(R.mipmap.ic_launcher); multiDialog.setMultiChoiceItems(items, isChecked, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if(isChecked){ choices.add(which); }else{ choices.remove(which); } } }); multiDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String s=""; for (Integer choice : choices) { s = s+items[choice]; } Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show(); } }); multiDialog.show(); }
参考文章
传送门
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/addevelopment/892755.html