android – 如何控制MultiChoice AlertDialog

前端之家收集整理的这篇文章主要介绍了android – 如何控制MultiChoice AlertDialog前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的应用程序中使用Dialog以允许用户进行多项选择,这是我的代码
  1. btn.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. // Build an AlertDialog
  5. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  6.  
  7. // String array for alert dialog multi choice items
  8. String[] colors = new String[]{
  9. "Red","Green","Blue","Purple","Olive"
  10. };
  11.  
  12. // Boolean array for initial selected items
  13. final boolean[] checkedColors = new boolean[]{
  14. false,// Red
  15. false,// Green
  16. false,// Blue
  17. false,// Purple
  18. false // Olive
  19.  
  20. };
  21.  
  22. // Convert the color array to list
  23. final List<String> colorsList = Arrays.asList(colors);
  24.  
  25. // Set multiple choice items for alert dialog
  26.  
  27. builder.setMultiChoiceItems(colors,checkedColors,new DialogInterface.OnMultiChoiceClickListener() {
  28. @Override
  29. public void onClick(DialogInterface dialog,int which,boolean isChecked) {
  30.  
  31. // Update the current focused item's checked status
  32. checkedColors[which] = isChecked;
  33.  
  34. // Get the current focused item
  35. String currentItem = colorsList.get(which);
  36.  
  37. // Notify the current action
  38. Toast.makeText(getApplicationContext(),currentItem + " " + isChecked,Toast.LENGTH_SHORT).show();
  39. }
  40. });
  41.  
  42. // Specify the dialog is not cancelable
  43. builder.setCancelable(false);
  44.  
  45. // Set a title for alert dialog
  46. builder.setTitle("Your preferred colors?");
  47.  
  48. // Set the positive/yes button click listener
  49. builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
  50. @Override
  51. public void onClick(DialogInterface dialog,int which) {
  52. // Do something when click positive button
  53. tv.setText("Your preferred colors..... \n");
  54. for (int i = 0; i<checkedColors.length; i++){
  55. boolean checked = checkedColors[i];
  56. if (checked) {
  57. tv.setText(tv.getText() + colorsList.get(i) + ",");
  58. }
  59. }
  60. }
  61. });
  62.  
  63. // Set the negative/no button click listener
  64. builder.setNegativeButton("No",int which) {
  65. // Do something when click the negative button
  66. }
  67. });
  68.  
  69. // Set the neutral/cancel button click listener
  70. builder.setNeutralButton("Cancel",int which) {
  71. // Do something when click the neutral button
  72. }
  73. });
  74.  
  75. AlertDialog dialog = builder.create();
  76. // Display the alert dialog on interface
  77. dialog.show();
  78. }
  79. });

我有两个问题:

>就像我选择了红色和紫色

(然后在TextView中得到如下输出:红色,紫色,)

首先,我想删除逗号(获取最后一个值)
>我已经选择了红色和紫色,当我再次打开对话框时,默认情况下没有选择红色和紫色(我如何保存状态)在这里输入代码,结果,当我再次选择这些(红色和紫色)​​时两个项目,在TextView中获取每个项目两次

解决方法

尝试在循环后更新textview

如果你的循环迭代达到了checkedcolors的长度,那么就不要附加一个逗号.

  1. public void onClick(DialogInterface dialog,int which) {
  2. // Do something when click positive button
  3. tv.setText("Your preferred colors..... \n");
  4. for (int i = 0; i < checkedColors.length; i++) {
  5. boolean checked = checkedColors[i];
  6. String colors = "";
  7. if (checked) {
  8. colors = colors + colorsList.get(i) ;
  9. if (i != checkedColors.length - 1) {
  10. colors = colors + ",";
  11. }
  12. }
  13. }
  14. tv.setText(tv.getText() + colors);
  15. }

Yor textview只会更新一次,因此您不会在TextView中两次获取每个项目.

要保存状态,您必须使用SharedPreference.

为了保存优先使用此功能

  1. SharedPreferences preferences = mContext.getSharedPreferences("PREFERENCE_NAME",Context.MODE_PRIVATE);
  2.  
  3. SharedPreferences.Editor editor = preferences.edit();
  4. editor.putBoolean("yourColor",isChecked);
  5. editor.commit();

并检索

  1. boolean isChecked = preferences.getBoolean("yourColor");

猜你在找的Android相关文章