前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >笔记17 | 复习AlertDialog(对话框)示例

笔记17 | 复习AlertDialog(对话框)示例

作者头像
项勇
发布2018-06-19 15:21:49
6250
发布2018-06-19 15:21:49
举报
文章被收录于专栏:项勇项勇

前言

本节给大家带来是显示提示信息的第三个控件AlertDialog(对话框),查看源码知道是Dialog的子类!ProgressDialog,TimePickerDialog父类等。另外,会发现构造方法是保护的,如果我们要创建AlertDialog的话,我们需要使用到该类中的一个静态内部类:public static class Builder,然后来调用AlertDialog里的相关方法,来对AlertDialog进行定制,最后调用show()方法来显示我们的AlertDialog对话框!好的,下面我们就来学习AlertDialog的基本用法,以及定制我们的AlertDialog!

地址

http://blog.csdn.net/xiangyong_1521/article/details/50528403

目录

  • 实现效果
  • 实现过程
  • 主要代码

一.实现效果


二.实现过程

  1. 创建AlertDialog.Builder对象;
  2. 调用setIcon()来设置图标,的setTitle()或setCustomTitle()设置标题;
  3. 设置对话框的内容:setMessage()还有其他方法来指定显示的内容;
  4. 调用setPositive / Negative / NeutralButton()设置:确定,取消,中立按钮;
  5. 调用创建()方法创建这个对象,再调用显示()方法将对话框显示出来;

三.主要代码

  • MianActivity.java
代码语言:javascript
复制
package com.Evan.demo_alertdialog;  import android.R.menu;  import android.app.Activity;  import android.app.AlertDialog;  import android.content.DialogInterface;  import android.content.DialogInterface.OnClickListener;  import android.content.DialogInterface.OnMultiChoiceClickListener;  import android.os.Bundle;  import android.view.View;  import android.widget.Button;  import android.widget.Toast;  public class MainActivity extends Activity {      private Button dialog_1,dialog_2,dialog_3,dialog_4;      private AlertDialog alert;      private AlertDialog.Builder builder=null;      private boolean[] checkItems;      private final String[] Loves={              "benz","BMW","Lamborghini","Porsche","Audi"      };      @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          SetView();      }      private void SetView() {          dialog_1=(Button) findViewById(R.id.button1);          dialog_2=(Button) findViewById(R.id.button2);          dialog_3=(Button) findViewById(R.id.button3);          dialog_4=(Button) findViewById(R.id.button4);      }      public void onClick(View v){          switch (v.getId()) {          /*           * 普通选择对话框           */          case R.id.button1:              alert=null;              builder=new AlertDialog.Builder(MainActivity.this);              alert=builder.setIcon(R.drawable.aa4)//图标                      .setTitle("标题设置")                      .setMessage("弹出主题设置")                      .setNegativeButton("取消", new OnClickListener() {                          @Override                          public void onClick(DialogInterface dialog, int which) {                              Toast.makeText(MainActivity.this, "您选择了:取消", 0).show();                          }                      }).setPositiveButton("确认", new OnClickListener() {                          @Override                          public void onClick(DialogInterface dialog, int which) {                              Toast.makeText(MainActivity.this, "您选择了:确认", 0).show();                          }                      }).create();//创建              alert.show();//显示              break;              /*               * 普通单选对话框               */          case R.id.button2:              alert=null;              builder=new AlertDialog.Builder(MainActivity.this);              alert=builder.setIcon(R.drawable.aa4)                      .setTitle("标题设置")                      .setItems(Loves, new OnClickListener() {                          @Override                          public void onClick(DialogInterface dialog, int which) {                              Toast.makeText(MainActivity.this, "你选择了:"+Loves[which], 0).show();                          }                      }).create();              alert.show();              break;              /*               * 单项选择对话框               */          case R.id.button3:              alert=null;              builder=new AlertDialog.Builder(MainActivity.this);              alert=builder.setIcon(R.drawable.aa4)                      .setTitle("标题设置")                      .setSingleChoiceItems(Loves, 0, new OnClickListener() {                          @Override                          public void onClick(DialogInterface dialog, int which) {                              Toast.makeText(getApplicationContext(), "你选择了" + Loves[which], Toast.LENGTH_SHORT).show();                          }                      }).create();              alert.show();              break;              /*               * 多项选择对话框               */          case R.id.button4:              checkItems = new boolean[]{false, false, false, false, false};              alert=null;              builder=new AlertDialog.Builder(MainActivity.this);              alert=builder.setIcon(R.drawable.aa4)                      .setTitle("标题设置")                      .setMultiChoiceItems(Loves, checkItems, new OnMultiChoiceClickListener() {                          @Override                          public void onClick(DialogInterface dialog, int which, boolean isChecked) {                              checkItems[which]=isChecked;                          }                      }).setPositiveButton("确实选择", new OnClickListener() {                          @Override                          public void onClick(DialogInterface dialog, int which) {                              String result="";                              for(int i=0;i<checkItems.length;i++){                                  if(checkItems[i])                                      result+=Loves[i]+" ";                              }                              Toast.makeText(getApplicationContext(), "选择的是:" + result, Toast.LENGTH_SHORT).show();                          }                      })                      .create();              alert.show();              break;          }      }  }  
  • XML
代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      tools:context=".MainActivity" >      <Button          android:id="@+id/button1"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:layout_alignParentLeft="true"          android:layout_alignParentTop="true"          android:layout_marginTop="14dp"          android:onClick="onClick"          android:text="普通选择对话框" />      <Button          android:id="@+id/button2"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:layout_alignParentLeft="true"          android:layout_below="@+id/button1"          android:onClick="onClick"          android:text="普通单选对话框" />      <Button          android:id="@+id/button3"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:layout_alignLeft="@+id/button1"          android:layout_below="@+id/button2"          android:onClick="onClick"          android:text="单项选择对话框" />      <Button          android:id="@+id/button4"          android:layout_width="match_parent"          android:onClick="onClick"          android:layout_height="wrap_content"          android:layout_alignLeft="@+id/button2"          android:layout_below="@+id/button3"          android:text="多项选择对话框" />  </RelativeLayout>  

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-10-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 项勇 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 地址
  • 目录
    • 一.实现效果
      • 二.实现过程
        • 三.主要代码
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档