以下代码,如何让弹窗必须选择后才能继续向下运行,要等待弹窗确认final int[] ide = {0}; unit_kind = "科室"; unit_xingzhi = ""; String[] keys = kezhang_names.keySet().toArray(new String[0]); View view = LayoutInflater.from(LuzhiAdmin_Xiangmu_Xiao0.this).inflate(R.layout.tanchuang, null); AlertDialog.Builder builder = new AlertDialog.Builder(LuzhiAdmin_Xiangmu_Xiao0.this); builder.setView(view); builder.setTitle("确定人员:"); builder.setSingleChoiceItems(keys, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ide[0] = which; return; } }).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { id_perx = "#"+kezhang_names.get(keys[ide[0]])+"#"; } }); AlertDialog dialog = builder.create(); Window window = dialog.getWindow(); if (window != null) { WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); layoutParams.width = 600; layoutParams.height = 800; window.setAttributes(layoutParams); } dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialogInterface) { //等待确认执行 } }); dialog.show();
时间: 2024-01-18 15:02:34 浏览: 132
你可以在弹窗的"确定"按钮的点击事件中,将"ide[0]"的值更新为当前选中项的索引。然后在弹窗的"onDismiss"方法中,判断"ide[0]"的值是否发生了变化,如果没有变化,说明用户没有做出选择,可以弹出提示框提示用户进行选择,否则继续向下执行。代码如下所示:
```
final int[] ide = {-1}; // 初始化为-1,表示用户还没有做出选择
unit_kind = "科室";
unit_xingzhi = "";
String[] keys = kezhang_names.keySet().toArray(new String[0]);
View view = LayoutInflater.from(LuzhiAdmin_Xiangmu_Xiao0.this).inflate(R.layout.tanchuang, null);
AlertDialog.Builder builder = new AlertDialog.Builder(LuzhiAdmin_Xiangmu_Xiao0.this);
builder.setView(view);
builder.setTitle("确定人员:");
builder.setSingleChoiceItems(keys, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ide[0] = which;
return;
}
}).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (ide[0] == -1) {
// 如果用户没有做出选择,弹出提示框
Toast.makeText(LuzhiAdmin_Xiangmu_Xiao0.this, "请先选择人员", Toast.LENGTH_SHORT).show();
return;
}
id_perx = "#" + kezhang_names.get(keys[ide[0]]) + "#";
}
});
AlertDialog dialog = builder.create();
Window window = dialog.getWindow();
if (window != null) {
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.width = 600;
layoutParams.height = 800;
window.setAttributes(layoutParams);
}
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
if (ide[0] == -1) {
// 如果用户没有做出选择,弹出提示框
Toast.makeText(LuzhiAdmin_Xiangmu_Xiao0.this, "请先选择人员", Toast.LENGTH_SHORT).show();
return;
}
// 继续向下执行
}
});
dialog.show();
```
这样,弹窗就会等待用户做出选择后才能继续向下执行。如果用户没有做出选择,会弹出提示框提醒用户。
阅读全文