Android中AlertDialog的使用
2019-08-11 15:19
781 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43219615/article/details/99193474
1.简单介绍
AlertDialog是安卓中很常用的对话框,只能通过AlertDialog.Builder完成参数设置后才能创建。使用AlertDialog的核心代码及注释如下。
AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this); //设置标题 builder.setTitle("Demo"); //设置显示的消息 builder.setMessage("我也不知道发生了什么"); //设置肯定的按钮和监听器 builder.setPositiveButton("好的", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //do something } }); //设置中立的按钮和监听器(感觉基本上很少用) builder.setNeutralButton("中立", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //do something } }); //设置否定的按钮和监听器 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //do something } }); //构造AlertDialog AlertDialog alertDialog = builder.create(); //显示AlertDialog alertDialog.show();
2.例子
创建AlertDialogActivity,代码如下。
- activity_alter_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".AlertDialogActivity" android:orientation="vertical"> <Button android:id="@+id/btn_pop_alert_dialog" android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="弹出AlertDialog"/> </LinearLayout>
- AlertDialogActivity.java
package xyz.strasae.androidlearn.my; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class AlertDialogActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alter_dialog); findViewById(R.id.btn_pop_alert_dialog).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this); //设置标题 builder.setTitle("Demo"); //设置显示的消息 builder.setMessage("我也不知道发生了什么"); //设置肯定的按钮和监听器 builder.setPositiveButton("好的", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(AlertDialogActivity.this, "你点击了好的", Toast.LENGTH_SHORT).show(); } }); //设置中立的按钮和监听器 builder.setNeutralButton("中立", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(AlertDialogActivity.this, "你点击了中立", Toast.LENGTH_SHORT).show(); } }); //设置否定的按钮和监听器 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(AlertDialogActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show(); } }); //构造AlertDialog AlertDialog alertDialog = builder.create(); //显示AlertDialog alertDialog.show(); } }); } }
相关文章推荐
- android入门-------案例七(alertdialog的使用)
- Android使用自定义AlertDialog(退出提示框)
- ANDROID 系统提示对话框(ALERTDIALOG)的使用
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android入门(42)——第四章 使用AlertDialog实现提示框
- Android中AlertDialog和ProgressDialog的使用
- android中AlertDialog的使用方法
- Android使用自定义AlertDialog(退出提示框)
- android推荐使用dialogFrament而不是alertDialog
- Android 为什么AlertDialog 里不能使用getapplicationcontext
- Android中AlertDialog的使用
- Android中AlertDialog使用
- Android中的AlertDialog使用示例一(警告对话框)
- Android中的AlertDialog使用示例五(自定义对话框)
- Android使用自定义AlertDialog(退出提示框)
- android 一个小例子说明handler和AlertDialog的简单使用
- Android使用自定义AlertDialog(退出提示框)
- Android中使用AlertDialog,使用函数setButton()出现的关于“new OnClickListener"错误
- Android中AlertDialog的使用小结