AlertDialog button doesn't work and it doesn't dismiss

妖精的绣舞 提交于 2020-01-22 03:46:13

问题


I have this AlertDialog that should be a duel between two players. It shows an image of a duel and after three seconds, thanks to a timer, it shows a "BANG" image. When the "BANG" image appears, the player must press the "BANG" button and the fester player wins. My problem is that the listener on the button doesn't work and i can't dismiss the alertDialog. This is the code

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();
            }
        });
    }
}

So i need to fix the onClickListener and the cancel() function


回答1:


You need to call the showDialog(int)method. Then override the method onCreateDialog(int) inside this create the dialog and set the positive and negative buttons and then it will work.

Though this way is deprecated. You should use fragments now. But still you can give it a try!
Check this link: https://developer.android.com/guide/topics/ui/dialogs.html




回答2:


Use this alert box. Its work for me

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();


来源:https://stackoverflow.com/questions/45895734/alertdialog-button-doesnt-work-and-it-doesnt-dismiss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!