android - 如何在我的程序中使用处理程序?

标签 android handler android-alertdialog

在我的应用程序中,我正在使用 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/

相关文章:

go - 从 Go 中的子函数关闭/返回 ResponseWriter

android - 警告对话框的背景颜色如何设置为窗口

android - 场景与 fragment 和 View

android - HtpsURLConnection 无法建立连接

java - 如何使用 JNI 在 C++ 中创建 Java 对象?

python - 当 Firebug 显示正在发送的参数时,Tornado 处理程序认为 POST 缺少参数

android - Android 上有 apktool 吗?我想在 Android 应用程序中组装 ".smali"并制作新的 ".apk"

android - 处理程序未在辅助线程中加速应用程序

android - 在显示警告对话框 Android 时隐藏状态栏

android - AlertDialog 使应用程序崩溃