android对话框一个按钮,带有一个,两个和三个按钮的Android Alert对话框

一键

import android.support.v7.app.AlertDialog;

public class MainActivity extends AppCompatActivity {

public void showAlertDialogButtonClicked(View view) {

// setup the alert builder

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("My title");

builder.setMessage("This is my message.");

// add a button

builder.setPositiveButton("OK",null);

// create and show the alert dialog

AlertDialog dialog = builder.create();

dialog.show();

}

}

两个按钮

public class MainActivity extends AppCompatActivity {

public void showAlertDialogButtonClicked(View view) {

// setup the alert builder

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("AlertDialog");

builder.setMessage("Would you like to continue learning how to use Android alerts?");

// add the buttons

builder.setPositiveButton("Continue",null);

builder.setNegativeButton("Cancel",null);

// create and show the alert dialog

AlertDialog dialog = builder.create();

dialog.show();

}

}

三个按钮

public class MainActivity extends AppCompatActivity {

public void showAlertDialogButtonClicked(View view) {

// setup the alert builder

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Notice");

builder.setMessage("Launching this missile will destroy the entire universe. Is this what you intended to do?");

// add the buttons

builder.setPositiveButton("Launch missile",null);

builder.setNeutralButton("Remind me later",null);

// create and show the alert dialog

AlertDialog dialog = builder.create();

dialog.show();

}

}

如果按钮文本太长而不能全部水平放置,那么它将自动布局在三个按钮的垂直列中.

处理按钮单击

在上面的示例中,OnClickListener为null.当用户点击按钮时,您可以用侦听器替换null来执行某些操作.例如:

builder.setPositiveButton("Launch missile",new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,int which) {

// do something like...

launchMissile();

}

});

继续

您可以制作更多种类的对话框.有关此问题,请参阅documentation.

由于AlertDialog中仅支持三个按钮,因此这是带有列表的对话框的示例.

public class MainActivity extends AppCompatActivity {

public void showAlertDialogButtonClicked(View view) {

// setup the alert builder

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Choose an animal");

// add a list

String[] animals = {"horse","cow","camel","sheep","goat"};

builder.setItems(animals,new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,int which) {

switch (which) {

case 0: // horse

case 1: // cow

case 2: // camel

case 3: // sheep

case 4: // goat

}

}

});

// create and show the alert dialog

AlertDialog dialog = builder.create();

dialog.show();

}

}

有关单选按钮列表和复选框列表的类似示例,请参阅this answer.

笔记

>使用字符串资源而不是硬编码字符串.

>您可以将所有内容包装在扩展DialogFragment的类中,以便轻松重用对话框. (请参阅this获取帮助.)

>这些示例使用支持库来支持API 11之前的版本.因此导入应该是

import android.support.v7.app.AlertDialog;

>为简洁起见,我在上面的示例中省略了onCreate方法.那里没什么特别的.

也可以看看

> How to disable the positive button

> Use a Toast rather than an Alert for short messages

> Single-choice list,radio button list,and checkbox list

> How to implement a custom AlertDialog View

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ``` <html> <head> <script> function showDialog(){ alert("这是一个对话框"); } </script> </head> <body> <button onclick="showDialog()">点击按钮弹出对话框</button> </body> </html> ``` ### 回答2: 以下是一个简单的前端代码示例,点击按钮后会弹出一个对话框: ```html <!DOCTYPE html> <html> <head> <title>点击按钮弹出对话框</title> <script> function showDialog() { alert("这是一个对话框!"); } </script> </head> <body> <button onclick="showDialog()">点击这里</button> </body> </html> ``` 这段代码首先定义了一个名为`showDialog()`的函数,函数内部使用`alert()`方法弹出一个对话框,显示一个简单的提示信息。 在HTML部分,创建了一个按钮,其中使用`onclick`属性指定了`showDialog()`函数,表示按钮被点击时调用该函数。因此,当用户点击按钮时,就会弹出一个对话框显示所指定的提示信息。 这只是一个简单的示例,实际情况中可能会使用更复杂的对话框组件,如模态框。但是基本原理是相同的,通过某种方式触发相应的事件,弹出对话框供用户交互。 ### 回答3: 以下是一个简单的前端代码示例,点击按钮后弹出一个对话框。 ```html <!DOCTYPE html> <html> <head> <title>点击按钮弹出对话框</title> <style> .dialog-container { display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 300px; height: 200px; background-color: #fff; border: 1px solid #ccc; padding: 20px; } .dialog-title { font-size: 18px; font-weight: bold; margin-bottom: 10px; } .dialog-buttons { display: flex; justify-content: flex-end; } .dialog-button { margin-left: 10px; } </style> </head> <body> <button id="open-dialog">点击弹出对话框</button> <div class="dialog-container" id="dialog"> <div class="dialog-title">对话框标题</div> <div class="dialog-content">对话框内容</div> <div class="dialog-buttons"> <button class="dialog-button" id="dialog-confirm">确定</button> <button class="dialog-button" id="dialog-cancel">取消</button> </div> </div> <script> const openDialogButton = document.querySelector('#open-dialog'); const dialog = document.querySelector('#dialog'); const dialogConfirmButton = document.querySelector('#dialog-confirm'); const dialogCancelButton = document.querySelector('#dialog-cancel'); openDialogButton.addEventListener('click', () => { dialog.style.display = 'block'; }); dialogConfirmButton.addEventListener('click', () => { dialog.style.display = 'none'; // 在确定按钮点击后执行其他逻辑 }); dialogCancelButton.addEventListener('click', () => { dialog.style.display = 'none'; // 在取消按钮点击后执行其他逻辑 }); </script> </body> </html> ``` 在这个示例中,我们创建了一个按钮一个隐藏的对话框。当点击按钮时,通过JavaScript代码将对话框的display属性设置为block,使其显示出来。对话框的HTML结构包含一个标题、一个内容和两个按钮(确定和取消)。点击确定按钮将隐藏对话框并执行特定的逻辑,点击取消按钮仅隐藏对话框。您可以根据需要自定义对话框的样式和功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值