我想在编码中添加对话框。该对话框可以弹出。假设用户按下ok按钮后对话框将关闭,但对话框会自动关闭。这是我的代码。有什么问题吗?
private void updataAccount(int type){
Iterator<AccountData> iteratorSort = commondata.account.values().iterator();
while (iteratorSort.hasNext()){
AccountData data = iteratorSort.next();
if(data.id == Integer.parseInt(accountId[account_spn.getSelectedItemPosition()]))
{
if(type == INCOME_MODE){
data.balance = data.balance+Double.parseDouble(value);
commondata.updateAccount(data);
}else if(type == PAYOUT_MODE){
data.balance = data.balance-Double.parseDouble(value);
commondata.updateAccount(data);
if(data.balance < 0)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder
.setTitle("Care Money")
.setMessage("Your amount in this account is negative!")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
return;
}
}
答案 0 :(得分:1)
尝试使用此
builder.setCancelable(false);
builder.show();
而不是这个
AlertDialog alert = builder.create();
alert.show();
答案 1 :(得分:0)
Chitan请试试这个代码这对我来说很好,希望它能以某种方式帮助你
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Error");
builder.setMessage(error)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
error="";
}
});
AlertDialog alert = builder.create();
alert.show();
答案 2 :(得分:0)
创建显示对话框的AsyncTask
,然后休眠1000毫秒,然后关闭对话框。