Android简单警报对话框

LS_*_*LS_ 132 android dialog android-alertdialog

我需要向点击我的Android应用程序上的按钮的用户显示一条短信,在IOS上我只需要创建一个简单易用的AlertView,但是我正在努力,因为解决方案似乎难以实现x10倍.我看到我需要使用DialogFragment,但我无法理解如何使其工作,有人可以解释一下吗?此外,我的解决方案是正确的还是更容易向用户显示简单的短信?

Mys*_*icϡ 402

你只需要在你的onClick:

AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
alertDialog.show();
Run Code Online (Sandbox Code Playgroud)

我不知道你从哪里看到你需要DialogFragment来显示警报.

希望这可以帮助.

  • 仅供参考 - Google的Android Dev网站上的第一个示例显示了如何使用片段进行此操作:http://developer.android.com/guide/topics/ui/dialogs.html我认为这可能是导致开发人员认为他的原因需要使用片段作为基本的AlertDialog.我今天搜索过,想到也许就是这样. (8认同)
  • 最好在构建器而不是alertDialog实例上设置属性! (4认同)

Sag*_*wal 24

没有我的朋友非常简单,尝试使用这个:

AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create();
alertDialog.setTitle("Alert Dialog");
alertDialog.setMessage("Welcome to dear user.");
alertDialog.setIcon(R.drawable.welcome);

alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
    }
});

alertDialog.show();
Run Code Online (Sandbox Code Playgroud)

教程介绍如何使用xml创建自定义对话框,然后将其显示为警报对话框.


gre*_*pps 12

您可以轻松制作自己的"AlertView"并在任何地方使用它.

alertView("You really want this?");
Run Code Online (Sandbox Code Playgroud)

实施一次:

private void alertView( String message ) {
 AlertDialog.Builder dialog = new AlertDialog.Builder(context);
 dialog.setTitle( "Hello" )
       .setIcon(R.drawable.ic_launcher)
       .setMessage(message)
//     .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
//      public void onClick(DialogInterface dialoginterface, int i) {
//          dialoginterface.cancel();   
//          }})
      .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialoginterface, int i) {   
        }               
        }).show();
 }
Run Code Online (Sandbox Code Playgroud)