открыть диалоговое окно, когда я нажимаю кнопку - PullRequest
17 голосов
/ 31 января 2011

У меня есть кнопка, и я хотел бы открыть диалог при нажатии. Это мой код:

Button more = (Button) findViewById(R.id.more);
more.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        //Intent myIntent = new Intent(view.getContext(), agones.class);
        //startActivityForResult(myIntent, 0);

        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("hi");
        alertDialog.setMessage("this is my app");

        alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // here you can add functions
        }
        });
    }
});

Ответы [ 3 ]

36 голосов
/ 31 января 2011

Как сказал @Roflcoptr, вы не вызвали alertDialog.show() метод.таким образом, ваш диалог не появляется.

Вот ваш отредактированный код:

Button more = (Button) findViewById(R.id.more);
more.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        //Intent myIntent = new Intent(view.getContext(), agones.class);
        //startActivityForResult(myIntent, 0);


        AlertDialog alertDialog = new AlertDialog.Builder(<YourActivityName>this).create(); //Read Update
        alertDialog.setTitle("hi");
        alertDialog.setMessage("this is my app");

        alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
              // here you can add functions
           }
        });

        alertDialog.show();  //<-- See This!
    }

});

, если вы напишите this вместо <ActivityName>.this, тогда он будет использовать ссылку View.OnClickListener, так как this в настоящее время доступен внутри него.Вам нужно дать название вашей деятельности там.

11 голосов
/ 31 января 2011

Ваш диалог не отображается, потому что вы не вызываете AlertDialog # show .

0 голосов
/ 17 августа 2017
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);

            builder.setMessage("this is message");
            builder.setTitle("this is title");

            //Setting message manually and performing action on button click
            builder.setMessage("Do you want to close this application ?");T
            //This will not allow to close dialogbox until user selects an option
            builder.setCancelable(false);
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Toast.makeText(this, "positive button", Toast.LENGTH_SHORT).show();
                            //builder.finish();
                        }
                    });
             builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //  Action for 'NO' Button
                            Toast.makeText(this, "negative button", Toast.LENGTH_SHORT).show();
                            dialog.cancel();
                        }
                    });

            //Creating dialog box
            AlertDialog alert = builder.create();
            //Setting the title manually
            //alert.setTitle("AlertDialogExample");
            alert.show();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...