Si8*_*Si8 3 android android-alertdialog
private void openDialog(){
LayoutInflater inflater = LayoutInflater.from(TrueAct.this);
View subView = inflater.inflate(R.layout.newdialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Enter New");
builder.setView(subView);
blEntryExistToday = true;
builder.setPositiveButton("ADD", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!blEntryExistToday) {
//CLOSE DIALOG
}
else {
tvM.setText("An entry for this day already exist!");
//DO NOT CLOSE DIALOG
}
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_LONG).show();
//CLOSE THE DIALOG
}
});
builder.show();
}
Run Code Online (Sandbox Code Playgroud)
无论我单击添加还是取消,对话框都会关闭。如果blEntryExistToday是,我怎么能保持打开状态true。
我想保持与现在相同的主题,颜色和文字。
您可以在构建AlertDialog 时停止设置侦听器,并将正负按钮的侦听器设置为null并自行处理点击。
您可以通过以下方式更改代码:
private void openDialog() {
LayoutInflater inflater = LayoutInflater.from(TrueAct.this);
View subView = inflater.inflate(R.layout.newdialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Enter New");
builder.setView(subView);
blEntryExistToday = true;
builder.setPositiveButton("ADD", null);
builder.setNegativeButton("CANCEL", null);
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(final DialogInterface dialog) {
Button positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!blEntryExistToday) {
//CLOSE DIALOG
dialog.dismiss();
} else {
tvM.setText("An entry for this day already exist!");
//DO NOT CLOSE DIALOG
}
}
});
Button negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
negativeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_LONG).show();
//CLOSE THE DIALOG
dialog.dismiss();
}
});
}
});
alertDialog.show();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1868 次 |
| 最近记录: |