
btn.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // Build an AlertDialog AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); // String array for alert dialog multi choice items String[] colors = new String[]{ "Red","Green","Blue","Purple","Olive" }; // Boolean array for initial selected items final boolean[] checkedcolors = new boolean[]{ false,// Red false,// Green false,// Blue false,// Purple false // Olive }; // Convert the color array to List final List<String> colorsList = Arrays.asList(colors); // Set multiple choice items for alert dialog builder.setMultiChoiceItems(colors,checkedcolors,new DialogInterface.OnMultiChoiceClickListener() { @OverrIDe public voID onClick(DialogInterface dialog,int which,boolean isChecked) { // Update the current focused item's checked status checkedcolors[which] = isChecked; // Get the current focused item String currentItem = colorsList.get(which); // Notify the current action Toast.makeText(getApplicationContext(),currentItem + " " + isChecked,Toast.LENGTH_SHORT).show(); } }); // Specify the dialog is not cancelable builder.setCancelable(false); // Set a Title for alert dialog builder.setTitle("Your preferred colors?"); // Set the positive/yes button click Listener builder.setPositivebutton("OK",new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog,int which) { // Do something when click positive button tv.setText("Your preferred colors..... \n"); for (int i = 0; i<checkedcolors.length; i++){ boolean checked = checkedcolors[i]; if (checked) { tv.setText(tv.getText() + colorsList.get(i) + ","); } } } }); // Set the negative/no button click Listener builder.setNegativebutton("No",int which) { // Do something when click the negative button } }); // Set the neutral/cancel button click Listener builder.setNeutralbutton("Cancel",int which) { // Do something when click the neutral button } }); AlertDialog dialog = builder.create(); // display the alert dialog on interface dialog.show(); } }); 我有两个问题:
>就像我选择了红色和紫色
(然后在TextVIEw中得到如下输出:红色,紫色,)
首先,我想删除逗号(获取最后一个值)
>我已经选择了红色和紫色,当我再次打开对话框时,默认情况下没有选择红色和紫色(我如何保存状态)在这里输入代码,结果,当我再次选择这些(红色和紫色)时两个项目,在TextVIEw中获取每个项目两次
如果你的循环迭代达到了checkedcolors的长度,那么就不要附加一个逗号.
public voID onClick(DialogInterface dialog,int which) { // Do something when click positive button tv.setText("Your preferred colors..... \n"); for (int i = 0; i < checkedcolors.length; i++) { boolean checked = checkedcolors[i]; String colors = ""; if (checked) { colors = colors + colorsList.get(i) ; if (i != checkedcolors.length - 1) { colors = colors + ","; } } } tv.setText(tv.getText() + colors); } Yor textvIEw只会更新一次,因此您不会在TextVIEw中两次获取每个项目.
要保存状态,您必须使用SharedPreference.
为了保存优先使用此功能
SharedPreferences preferences = mContext.getSharedPreferences("PREFERENCE_name",Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("yourcolor",isChecked); editor.commit(); 并检索
boolean isChecked = preferences.getBoolean("yourcolor"); 总结 以上是内存溢出为你收集整理的android – 如何控制MultiChoice AlertDialog全部内容,希望文章能够帮你解决android – 如何控制MultiChoice AlertDialog所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)