推扬网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
推扬网 门户 经验分享 查看内容

android AlertDialog的简单使用实例

2020-4-11 13:36| 发布者: admin| 查看: 386| 评论: 0

本篇文章主要介绍了android AlertDialog的简单使用实例,具有一定的参考价值,有兴趣的可以了解一下。

当你的应用需要显示一个进度条或需要用户对信息进行确认时,可以使用alertDialog来完成。下面来介绍常用的四种AlertDialog。

1、普通对话框

package com.example.yk.dialogtest; 
 
import android.content.DialogInterface; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.Toast; 
 
/** 
 * AlertDialog普通对话框 
 */ 
public class GeneralDialogActivity extends AppCompatActivity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_general_dialog); 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this) 
        .setTitle("操作title")//设置title 
        .setMessage("操作message")//设置要显示的message 
        .setCancelable(false)//表示点击dialog其它部分不能取消(除了“取消”,“确定”按钮) 
        .setPositiveButton("确定", new 
            DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
        Toast.makeText(GeneralDialogActivity.this, "点击了确定", Toast.LENGTH_SHORT).show(); 
 
      } 
    }).setNegativeButton("取消", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
//        dialogInterface.dismiss(); 
      } 
    }); 
    alertDialog.show();//别忘了show 
  } 
} 

3、多选对话框

package com.example.yk.dialogtest; 
 
import android.content.DialogInterface; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.Toast; 
 
/** 
 * 多选对话框 
 */ 
public class MultiChoiceDialogActivity extends AppCompatActivity { 
  private String[] items={"java","php","c"}; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_multi_choice_dialog); 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this) 
        .setTitle("提示title") 
        .setCancelable(false) 
        .setMultiChoiceItems(items, new boolean[]{false, false, false}, new DialogInterface.OnMultiChoiceClickListener() { 
 
 
          @Override 
          public void onClick(DialogInterface dialogInterface, int i, boolean b) { 
            if(b){ 
              Toast.makeText(MultiChoiceDialogActivity.this, "选中了"+items[i], Toast.LENGTH_SHORT) 
                  .show(); 
            } 
 
          } 
        }) 
        .setPositiveButton("确定", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialogInterface, int i) { 
 
          } 
        }); 
    alertDialog.show(); 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持


鲜花

握手

雷人

路过

鸡蛋

最新评论

精选推荐

    广告服务|投稿要求|禁言标准|版权说明|免责声明|手机版|小黑屋|推扬网 ( 粤ICP备18134897号 )|网站地图 | 邮箱:vayae@hotmail.com

    GMT+8, 2025-6-9 21:21 , Processed in 0.317489 second(s), 28 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    返回顶部