我有以下Dialog代码,但问题是选择在我按下OK按钮之前生效。按下“确定”按钮后,如何使选择生效?
public void cropSelect()
{
final CharSequence[] cropType = {"Wheat","Corn","Soybean"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainContext.myContext);
builder.setTitle("Choose Crop");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
});
builder.setSingleChoiceItems(cropType, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if("Wheat".equals(cropType[which])){
cropSelect = "Wheat";
}
else if("Corn".equals(cropType[which])){
cropSelect = "Corn";
}
else if("Soybean".equals(cropType[which])){
cropSelect = "Soybean";
}
}
});
builder.show();
答案 0 :(得分:1)
您希望保存他们在onClick中选择的内容,但不要将其设置为全局cropSelect变量,直到onClick的正按钮。现在,您只需在触摸项目时立即编写cropSelect变量。