Java AlertDialog使用分隔符将多个项目存储在一个变量中
如何将Java AlertDialog使用分隔符将多个项目存储在一个变量中,java,android,arrays,android-alertdialog,multiple-choice,Java,Android,Arrays,Android Alertdialog,Multiple Choice,如何将AlertDialog-multipleechoiceitems中的项目存储在一个变量中。还带有分隔符,。我需要它,以便通过php-explode函数传递到远程服务器并提取它 这是我的演示代码:MainActivity.java final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl); Button btn = (Button) findViewById(R.id.btn); final TextVi
AlertDialog
-multipleechoiceitems
中的项目存储在一个变量中。还带有分隔符,
。我需要它,以便通过php
-explode
函数传递到远程服务器并提取它
这是我的演示代码:MainActivity.java
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
Button btn = (Button) findViewById(R.id.btn);
final TextView tv = (TextView) findViewById(R.id.tv);
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
true, // Green
false, // Blue
true, // Purple
false // Olive
};
// Convert the color array to list
final List<String> colorsList = Arrays.asList(colors);`
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();
}
});
final RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl);
按钮btn=(按钮)findViewById(R.id.btn);
最终文本视图电视=(文本视图)findViewById(R.id.tv);
btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//构建一个AlertDialog
AlertDialog.Builder=新建AlertDialog.Builder(MainActivity.this);
//警报对话框多选项的字符串数组
字符串[]颜色=新字符串[]{
“红色”,
“绿色”,
“蓝色”,
“紫色”,
“橄榄”
};
//初始选定项的布尔数组
最终布尔值[]选中颜色=新布尔值[]{
错,//红色
对,//绿色
错,//蓝色
是的,紫色
假//橄榄
};
//将颜色数组转换为列表
最终列表颜色列表=数组.asList(颜色)`
setMultiChoiceItems(颜色、选中的颜色、新的DialogInterface.OnMultiChoiceClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int,其中布尔值被选中){
//更新当前焦点项目的选中状态
选中的颜色[其中]=已选中;
//获取当前的焦点项目
字符串currentItem=colorsList.get(哪个);
//通知当前操作
Toast.makeText(getApplicationContext(),
currentItem+“”+isChecked,Toast.LENGTH\u SHORT.show();
}
});
我想将所选项目存储在currentItem
变量中
因此,示例输出将如下所示(在
Logcat
中):红、绿、蓝、紫
您可以在设置MultiChoiceItems onClick事件中使用它:
currentItem = currentItem+colorsList.get(which)+",";
Toast.makeText(context,"Appended String :
"+currentItem,Toast.LENGTH_LONG).show();
并声明变量:
String currentItem=""; globally
发送到服务器时,只需执行以下操作:
currentItem=currentItem.substring(0,currentItem.length()-1);
它将在最后删除一个额外的“,”
编辑:
对于button onClick事件中评论中的问题,请使用以下命令:
for(int i=0;i<colors.length;i++)
currentItem = currentItem+colors[i]+",";
Toast.makeText(context,"Appended String : "+currentItem,Toast.LENGTH_LONG).show();
for(int i=0;我希望你能尝试一下,如果有任何问题,请告诉我。如果我提供了一个按钮来选择它们,我将如何做?嗨@RoCk,如果我的答案对你有效,请接受。你好@RoCk,你得到答案了吗?