在我的应用程序中,我正在使用 Canvas 和上面的一些对象。这个实现是在一个名为 mythread 的单独线程中完成的。在特定情况下,我想在我的屏幕上显示一个警告对话框。首先我尝试通过使用下面给出的函数。
public void StopChecking()
{
if(stopgameflag==true)
{
context=MyActivity.this;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Game Over !!!")
.setCancelable(false)
.setPositiveButton("Play Again",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
setContentView(R.layout.main);
dialog.cancel();
}
})
.setNegativeButton("Exit",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
dialog.cancel();
}
});
Log.d(TAG, "Stopping...oooooooooooooooooooooooooooooo");
}
}
这是在 MyActivity 类(主要 Activity )中编写的函数。但它正在崩溃。
有人说处理程序是最好的方法。我已经搜索过相同的方法。但我什么都不懂..
谁能告诉我如何实现处理程序来显示警报对话框...
最佳答案
游戏结束条件检查后调用此函数
if(gameover) {
StopChecking();
}
public void StopChecking() {
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("*GameOver*");
alertDialog.setButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.setButton2("Play Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// start game again
return;
}
});
alertDialog.show();
}
关于android - 如何在我的程序中使用处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8395745/