AlertDialog按钮不起作用,它不会被解雇

时间:2017-08-26 13:09:09

标签: android events listener alertdialog

我有这个AlertDialog应该是两个玩家之间的决斗。它显示了一个决斗的图像,三秒后,由于一个计时器,它显示了一个" BANG"图片。当" BANG"图像出现后,玩家必须按下" BANG"按钮和溃烂的玩家获胜。我的问题是按钮上的聆听者没有工作,我无法解除alertDialog。这是代码

package com.example.root.gbu;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.SystemClock;
import android.widget.ImageView;

public class DuelFrame {

private Context context;
private long startTime;
private long endTime;
private boolean timeToShot=false;
private ImageView image;
private AlertDialog alertDialog;

public DuelFrame(String enemy, Context context){
        this.context = context;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("Duel against " + enemy);
        image = new ImageView(context);
        image.setImageResource(R.drawable.duel_start);
        builder.setView(image);
        builder.setNegativeButton("BANG",
                new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {
                        System.out.println("********************BANG PREMUTO*************************");
                        if(timeToShot) {
                            endTime = SystemClock.elapsedRealtimeNanos();
                            ConnectionStream.getOut().println("TIME:" + String.valueOf(endTime - startTime));
                        }
                        else
                            ConnectionStream.getOut().println("TIME:LOSE");
                    }
                });
        alertDialog = builder.create();
    }

    public void start(){
        ((Activity)context).runOnUiThread(new Runnable(){//mostro la finestra
            @Override
            public void run(){
                alertDialog.show();
            }
        });
        new java.util.Timer().schedule(//timer per cambiare l'immagine
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                        ((Activity)context).runOnUiThread(new Runnable(){
                            @Override
                            public void run(){
                                image.getLayoutParams().height = image.getHeight();
                                image.getLayoutParams().width = image.getWidth();
                                image.setImageResource(R.drawable.bang);
                                startTime = SystemClock.elapsedRealtimeNanos();
                                timeToShot = true;
                            }
                        });
                    }
                }, 3000);
    }

    public void cancel(){
        ((Activity)context).runOnUiThread(new Runnable(){
            @Override
            public void run(){
                alertDialog.dismiss();
            }
        });
    }
}

所以我需要修复onClickListener和cancel()函数

2 个答案:

答案 0 :(得分:2)

你需要打电话给 showDialog(int)方法。 然后覆盖该方法 onCreateDialog(int)在其中创建对话框并设置正面和负面按钮然后它将起作用。

虽然这种方式已被弃用。你现在应该使用片段。但你仍然可以尝试一下! 请检查此链接:https://developer.android.com/guide/topics/ui/dialogs.html

答案 1 :(得分:1)

使用此警告框。它为我工作

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(SettingActivity.this);
                alertDialogBuilder.setMessage("Message");

                alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        //Code
                        dialog.cancel();
                    }
                });

                alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();