AlertDialog以确定/取消按钮 [英] AlertDialog with OK/Cancel buttons

查看:992
本文介绍了AlertDialog以确定/取消按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建这个AlertDialog:

I create this AlertDialog:

            String msg = "Connessione lenta o non funzionante";
            AlertDialog alertDialog;
            alertDialog = new AlertDialog.Builder(HomePage.this).create();
            alertDialog.setTitle("Timeout connessione");
            alertDialog.setMessage(msg);
            alertDialog.show();

我想补充确定和取消按钮。我在这里搜索在计算器上,但SET按钮的方法似乎是德precated。我还发现setPositiveButton和setNegativeButton为AlertDialog.Builder但即使他们似乎是德precated。

I want to add OK and Cancel buttons. I searched here on StackOverflow but setButton method seems to be deprecated. I also found setPositiveButton and setNegativeButton for AlertDialog.Builder but even them seem to be deprecated.

推荐答案

您可以使用 AlertDialog.Builder.setPositiveButton AlertDialog.Builder.setNegativeButton ,两者都没有去precated(<一个href=\"http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setPositiveButton(java.lang.CharSequence,%20android.content.DialogInterface.OnClickListener)\"相对=nofollow>看到文档):

You can use AlertDialog.Builder.setPositiveButton and AlertDialog.Builder.setNegativeButton, both are not deprecated (see the documentation):

new AlertDialog.Builder(HomePage.this)
        .setTitle("Timeout connessione")
        .setMessage("Connessione lenta o non funzionante")
        .setNegativeButton(android.R.string.cancel, null) // dismisses by default
        .setPositiveButton(android.R.string.ok, new OnClickListener() {
            @Override public void onClick(DialogInterface dialog, int which) {
                // do the acknowledged action, beware, this is run on UI thread
            }
        })
        .create()
        .show();

这篇关于AlertDialog以确定/取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆