我在我的应用程序中使用Dialog以允许用户进行多项选择,这是我的代码:
- 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中获取每个项目两次
解决方法
尝试在循环后更新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");