package com.example.lin.exercise;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
/**
* Created by Lin on 2018/4/29.
*/
public class Activity_AlertDialog extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myDialog();
}
private void myDialog(){
//初始化一个AlterDialog的内置对象builder
AlertDialog.Builder builder=new AlertDialog.Builder(
Activity_AlertDialog.this
);
//设置Dialog的title
builder.setTitle("提示");
//设置显示的内容
builder.setMessage("正在显示的是……");
//设置图标icon
builder.setIcon(R.drawable.p2);
//添加一个确定按钮,当按下对话框的正面按钮时,设置一个侦听器被调用。
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//设置当点击确定后使用Toast显示一行文字,并让Dialog消失
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(Activity_AlertDialog.this,"确定被点击",Toast.LENGTH_SHORT).show();
//解除对话框
dialogInterface.dismiss();
}
});
//添加一个否定按钮,
builder.setNegativeButton("以后再说", new DialogInterface.OnClickListener() {
//设置点击此按钮后使用Toast显示一行文字,并让Dialog消失
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(Activity_AlertDialog.this,"以后再说",Toast.LENGTH_SHORT).show();
//解除对话框
dialogInterface.dismiss();
}
});
//添加一个忽略按钮
builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(Activity_AlertDialog.this,"无视",Toast.LENGTH_SHORT).show();
//取消对话框
dialogInterface.cancel();
}
});
//使用模态,即不对上述3个按钮来操作就不能进行其他操作。默认为可以继续进行其他操作
builder.setCancelable(false);
//显示AlertDialog
builder.show();
}
}
/*
方法:
1、setPositiveButton
(1)介绍
当按下对话框的正面按钮时,设置一个侦听器被调用。
(2)语法格式
a、AlertDialog.Builder setPositiveButton(int textId(int:显示在正面按钮中的文本的资源ID),DialogInterface.OnClickListener侦听器)
b、AlertDialog.Builder setPositiveButton(CharSequence(显示在正面按钮中的文字),DialogInterface.OnClickListener侦听器)
2、setNegativeButton
(1)介绍
当按下对话框的否定按钮时,设置一个侦听器来调用。
(2)语法格式
a、AlertDialog.Builder setNegativeButton(int textId(int:显示在正面按钮中的文本的资源ID),DialogInterface.OnClickListener侦听器)
b、AlertDialog.Builder setNegativeButton(CharSequence(显示在正面按钮中的文字),DialogInterface.OnClickListener侦听器)
3、setNeutralButton
(1)介绍
设置一个侦听器,当按下对话框的中性按钮时被调用。
(2)语法格式
a、AlertDialog.Builder setNeutralButton(int textId(int:显示在正面按钮中的文本的资源ID),DialogInterface.OnClickListener侦听器)
b、AlertDialog.Builder setNeutralButton(CharSequence(显示在正面按钮中的文字),DialogInterface.OnClickListener侦听器)
*/
对话框----按钮确定、取消、忽视
最新推荐文章于 2022-04-22 23:06:19 发布