您的位置:首页 > 移动开发 > Android开发

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,代码如下。

  1. 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>
  1. 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();
}
});
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: