如何控制MultiChoice AlertDialog [英] How Do I Control on MultiChoice AlertDialog
问题描述
我正在应用中使用Dialog允许用户进行多项选择,这是我的代码:
I am using Dialog in my app to allow user to make multiple selection, Here is my code:
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", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do something when click the negative button
}
});
// Set the neutral/cancel button click listener
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do something when click the neutral button
}
});
AlertDialog dialog = builder.create();
// Display the alert dialog on interface
dialog.show();
}
});
我有两个查询:
-
就像我选择了红色和紫色
Like I have selected Red and Purple
(然后在TextView中获得如下输出:Red, Purple,
)
(then in TextView getting output like this: Red, Purple,
)
首先,我要删除逗号(使用最后一个值)
我已经选择了红色和紫色,当我再次打开对话框时,默认情况下未显示红色和紫色(如何保存状态)enter code here
,因此,当我再次选择这些对话框时,我已经选择了红色和紫色(红色和紫色)两个项目,使每个项目在TextView中两次两次
I already selected Red and Purple, when i again open dialog not getting red and purple as selected by default (How can i save the state)enter code here
, and as a result, when i am again selecting these (Red and Purple) two items, getting each item twice in a TextView
推荐答案
尝试在循环后更新textview
Try updating your textview after the loop
如果循环迭代达到checkedcolors
的长度,则不要添加逗号.
And if your loop iteration reaches the length of the checkedcolors
then donot append a comma.
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);
}
您的textview仅更新一次,因此您不会在TextView中两次获取每个项目.
Yor textview will be updated only once, so you wont be getting each item twice in the TextView.
要保存状态,您必须使用SharedPreference
.
For saving the state you have to use SharedPreference
.
要保存为首选项,请使用此
For saving into preference use this
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");
这篇关于如何控制MultiChoice AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!