完成这样的效果
<?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=".MainActivity"> <Button android:id="@+id/btn1" android:text="弹出对话框" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="myAlert"/> </LinearLayout>
package com.example.eightalertdialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void myAlert(View view) {
//创建AlertDialog构建器
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//通过构建器进行链式设置
builder.setIcon(R.drawable.ic_android_black_24dp)
.setTitle("我是一个对话框")
.setMessage("今天天气怎么样呀")
.create()
.show();
}
}
完成如下效果
package com.example.eightalertdialog; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.os.Build; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void myAlert(View view) { //创建AlertDialog构建器 AlertDialog.Builder builder = new AlertDialog.Builder(this); //通过构建器进行链式设置 builder.setIcon(R.drawable.ic_android_black_24dp) .setTitle("我是一个对话框") .setMessage("今天天气怎么样呀") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .setNeutralButton("中间按钮", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .create() .show(); } }