我有一个消息列表,我想向用户发送。它们存储在一个数组中。然后我想显示一个警报,并在用户单击肯定按钮时,逐个浏览消息。
_msgCount是我类中的一个私有变量。
msgs 是一个局部最终变量。
我所有的日志记录都会发生。但信息永远不会改变。我也尝试过做_alert.hide((,然后是setMessage((,然后是_alert.show((。
感谢您的任何帮助!
AlertDialog.Builder builder = new AlertDialog.Builder(this);
_alert = builder.setTitle("Whoa there!")
.setMessage(msgs[_msgCount])
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
Log.i("EasterHunt", "On positive click");
_alert.setMessage(msgs[++_msgCount]);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
Log.i("EasterHunt", "On negative click");
Toast.makeText(MapsMarkerActivity.this, R.string.nope, Toast.LENGTH_SHORT).show();
}
})
.create();
_alert.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog)
{
Log.i("EasterHunt", "what!");
}
});
您需要覆盖正按钮单击以禁用对话框的关闭操作。做这样的事情
...
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// leave this empty
}
})
...;
_alert.show(); // show the dialog before getting its button
_alert.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("EasterHunt", "On positive click");
if (_msgCount < msgs.length - 1) {
_alert.setMessage(msgs[++_msgCount]);
} else {
// do some action
_alert.dismiss();
}
}
})
不确定是否可以更改已构建的 AlertDialog 的消息。尝试每次创建一个新的。
String[] msgs = new String[] { "ciao", "boh", "buh" };
int _msgCount = 0;
private void showDialog() {
if (_msgCount >= msgs.length) return;
new AlertDialog.Builder(this).setTitle("Whoa there!")
.setMessage(msgs[_msgCount++])
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.i("EasterHunt", "On positive click");
showDialog();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.i("EasterHunt", "On negative click");
Toast.makeText(MainActivity.this, "nope", Toast.LENGTH_SHORT).show();
}
})
.show();
}