对话框----按钮确定、取消、忽视

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侦听器)
 */

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值