AlertDialog(对话框)

它也是其他 Dialog的的父类!比如ProgressDialog,TimePickerDialog等,而AlertDialog的父类是:Dialog!

AlertDialog并不能直接new出来,构造方法是protected的,要创建AlertDialog的话,需要使用到该类中的一个静态内部类:public static class Builder,然后来调用AlertDialog 里的相关方法,来对AlertDialog进行定制,最后调用show()方法来显示AlertDialog对话框!

AlertDialog有几个基本方法,

例如:setTitile() setMessage() setCancelable() setPositiveButton() setNegativeButton() setNeutralButton() 等等。

1、带按钮的AlertDialog

点击按钮弹出提示内容;

public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_dialog1:
AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
builder.setTitle("请回答").setMessage("想见你会加更吗?")
.setPositiveButton("会", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "你点击了会", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("不会", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "你点击了不会", Toast.LENGTH_SHORT).show();
}
}).show();
break;

2.1、类似RadioButton的AlertDialog,单选

case R.id.btn_dialog2:
final String[] array2 = new String[]{"男", "女"};
AlertDialog.Builder builder2 = new AlertDialog.Builder(DialogActivity.this);
builder2.setTitle("选择性别").setItems(array2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, array2[which], Toast.LENGTH_SHORT).show();
}
}).show();
break;

2.2、与RadioButton一样的AlertDialog

case R.id.btn_dialog3:
final String[] array3 = new String[]{"男", "女"};
AlertDialog.Builder builder3 = new AlertDialog.Builder(DialogActivity.this);
builder3.setTitle("选择性别").setSingleChoiceItems(array3, 0, new DialogInterface.OnClickListener() {
@Override //默认选中第几个
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, array3[which], Toast.LENGTH_SHORT).show();
//选择之后对话框会消失
dialog.dismiss();
}//点框外对话框不会消失
}).setCancelable(false).show();
break;

3、与CheckBox一样的AlertDialog

case R.id.btn_dialog4:
final String[] array4 = new String[]{"唱", "跳","rap"};
boolean[] isSelected = new boolean[]{false,false,true};
AlertDialog.Builder builder4 = new AlertDialog.Builder(DialogActivity.this);
builder4.setTitle("选择兴趣").setMultiChoiceItems(array4, isSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//which表示当前选中第几个,isChecked表示选中或取消选中
Toast.makeText(DialogActivity.this, array4[which]+":"+isChecked, Toast.LENGTH_SHORT).show();
}
}).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//
}
}).show();
break;

4、自定义View的AlertDialog

要实现一个Login画面,有用户名和密码,这时我们就要用到自定义View的AlertDialog,步骤如下:

  • 先创建自定义登录框的布局文件layout_dialog.xml
  • 在Activity中创建自定义的AlertDialog

layout_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"> <EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="15sp"
android:textColor="#FF8F44"
android:background="@drawable/bg_username"
android:hint="用户名"
android:paddingLeft="10dp"
android:paddingRight="10dp"/> <EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="15sp"
android:textColor="#FF8F44"
android:layout_below="@id/edit_1"
android:background="@drawable/bg_username"
android:hint="密码"
android:inputType="textPassword"
android:layout_marginTop="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"/> <Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/edit_2"
android:layout_marginTop="20dp"
android:background="@drawable/bg_btn4"
android:text="登录"
android:textSize="20sp"
android:textColor="#fff"/>
</LinearLayout>

Activity:

case R.id.btn_dialog5:
AlertDialog.Builder builder5 = new AlertDialog.Builder(DialogActivity.this);
View view = LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog,null);
//声明布局里的控件
EditText etUsername = view.findViewById(R.id.et_username);
EditText etPassword = view.findViewById(R.id.et_password);
Button btnLogin = view.findViewById(R.id.btn_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//
}
});
builder5.setTitle("请登录").setView(view).show();
break;

完整代码:

DialogActivity:

package com.example.helloworld;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class DialogActivity extends AppCompatActivity { private Button mBtnDialog1, mBtnDialog2, mBtnDialog3, mBtnDialog4,mBtnDialog5; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
mBtnDialog1 = findViewById(R.id.btn_dialog1);
mBtnDialog2 = findViewById(R.id.btn_dialog2);
mBtnDialog3 = findViewById(R.id.btn_dialog3);
mBtnDialog4 = findViewById(R.id.btn_dialog4);
mBtnDialog5 = findViewById(R.id.btn_dialog5);
onClick onClick = new onClick();
mBtnDialog1.setOnClickListener(onClick);
mBtnDialog2.setOnClickListener(onClick);
mBtnDialog3.setOnClickListener(onClick);
mBtnDialog4.setOnClickListener(onClick);
mBtnDialog5.setOnClickListener(onClick);
} class onClick implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_dialog1:
AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
builder.setTitle("请回答").setMessage("想见你会加更吗?")
.setPositiveButton("会", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "你点击了会", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("不会", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "你点击了不会", Toast.LENGTH_SHORT).show();
}
}).show();
break;
case R.id.btn_dialog2:
final String[] array2 = new String[]{"男", "女"};
AlertDialog.Builder builder2 = new AlertDialog.Builder(DialogActivity.this);
builder2.setTitle("选择性别").setItems(array2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, array2[which], Toast.LENGTH_SHORT).show();
}
}).show();
break;
case R.id.btn_dialog3:
final String[] array3 = new String[]{"男", "女"};
AlertDialog.Builder builder3 = new AlertDialog.Builder(DialogActivity.this);
builder3.setTitle("选择性别").setSingleChoiceItems(array3, 0, new DialogInterface.OnClickListener() {
@Override //默认选中第几个
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, array3[which], Toast.LENGTH_SHORT).show();
//选择之后对话框会消失
dialog.dismiss();
}//点框外对话框不会消失
}).setCancelable(false).show();
break;
case R.id.btn_dialog4:
final String[] array4 = new String[]{"唱", "跳","rap"};
boolean[] isSelected = new boolean[]{false,false,true};
AlertDialog.Builder builder4 = new AlertDialog.Builder(DialogActivity.this);
builder4.setTitle("选择兴趣").setMultiChoiceItems(array4, isSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//which表示当前选中第几个,isChecked表示选中或取消选中
Toast.makeText(DialogActivity.this, array4[which]+":"+isChecked, Toast.LENGTH_SHORT).show();
}
}).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//
}
}).show();
break;
case R.id.btn_dialog5:
AlertDialog.Builder builder5 = new AlertDialog.Builder(DialogActivity.this);
View view = LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog,null);
//声明布局里的控件
EditText etUsername = view.findViewById(R.id.et_username);
EditText etPassword = view.findViewById(R.id.et_password);
Button btnLogin = view.findViewById(R.id.btn_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//
}
});
builder5.setTitle("请登录").setView(view).show();
break; }
}
} }

activity_edit_text.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<EditText
android:id="@+id/edit_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="15sp"
android:textColor="#FF8F44"
android:background="@drawable/bg_username"
android:hint="用户名"
android:paddingLeft="10dp"
android:paddingRight="10dp"/> <EditText
android:id="@+id/edit_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="15sp"
android:textColor="#FF8F44"
android:layout_below="@id/edit_1"
android:background="@drawable/bg_username"
android:hint="密码"
android:inputType="textPassword"
android:layout_marginTop="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"/> <Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/edit_2"
android:layout_marginTop="20dp"
android:background="@drawable/bg_btn4"
android:text="登录"
android:textSize="20sp"
android:textColor="#fff"/>
</RelativeLayout>

Android学习05的更多相关文章

  1. Android学习第三天-打包常用命令

    在前面<Android学习第一天-adb常用命令>和 <Android学习第二天-android常用命令>两篇博文中,我们重点讲解了adb和android的常用命令,下面我们讲 ...

  2. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

  3. Android学习链接大放送

    虽然贴链接这种事情..真是一种很偷懒的做法... 但是我一个小菜鸟,果断还是要以多向别人学习为主... 好资源要和大家分享对不对! 况且..放博客里..比收藏夹的利用几率要大一点! 原作者应该也很喜欢 ...

  4. Android学习路线总结,绝对干货

    title: Android学习路线总结,绝对干货 tags: Android学习路线,Android学习资料,怎么学习android grammar_cjkRuby: true --- 一.前言 不 ...

  5. Android 学习资源

    下面这些资源对Android开发来说是很有帮助的! 最常用的: Android开发官方网站:http://developer.android.com/index.html 这个网站应该是Android ...

  6. Android学习资料收集

    1.Android 学习之路 http://stormzhang.com/android/2014/07/07/learn-android-from-rookie/

  7. Android学习——第一个NDK程序

    在前面的学习中,我们已经讲解了关于NDK编程的环境搭建流程,简单的使用我们也通过官网本身自带的例子进行说明了.可是相信大家一定还存在这么的一个疑惑:“如果我要自己利用NDK编写一个Android应用, ...

  8. Android学习——windows下搭建Cygwin环境

    在上一篇博文<Android学习——windows下搭建NDK_r9环境>中,我们详细的讲解了在windows下进行Android NDK开发环境的配置,我们也讲到了在NDk r7以后,我 ...

  9. Android学习——windows下搭建NDK_r9环境

    1. NDK(Native Development Kit) 1.1 NDK简介 Android NDK是一套允许开发人员使用本地代码(如C/C++)进行Android APP功能开发的工具,通过这个 ...

随机推荐

  1. 其他 - YAML 入门

    概述 简单介绍 YAML 语言 背景 很多地方, 都在使用 YAML k8s spring 其他 准备 验证工具 YAML.YML在线格式化校验工具 一个 YAML 转换 JSON 的工具 通常来说, ...

  2. 转载:Cubic interpolation

    https://www.paulinternet.nl/?page=bicubic Cubic interpolation If the values of a function f(x) and i ...

  3. Java-重载和重写区别剖析

    重载(Overload)和重写(Override)是任何一门面向对象的语言都会具有的两个特性,自然,Java语言中也具有此两种特性.但是,对于Java新手,或者没有面向对象语言经验的开发者而言,这会是 ...

  4. 解决windows10 OBS Studioobsstudio显示器捕获黑屏

    前提设置显卡,下载OBS studio 64bit别下载32bit了 如果电脑desktop右键无法显示NAVIDIA  控制面板则需要win+R  输入 msconfig选取服务,勾选所有NAIVI ...

  5. Nginx之反向代理

    所谓,反向代理就是,客户端向A服务器地址发送请求,A服务器接收到客户端请求后又将请求转发给了B服务器,最后又将B服务响应的数据响应给了客户端. 通过配置文件,可以实现Nginx的反向代理. 代码: s ...

  6. spring(六):事务

    事务特性ACID 原子性(Atomicity):即事务是不可分割的最小工作单元,事务内的操作要么全做,要么全不做: 一致性(Consistency):在事务执行前数据库的数据处于正确的状态,而事务执行 ...

  7. linux-zookeeper安装、配置

    1.下载zookeeper包 (地址:https://www-eu.apache.org/dist/zookeeper/) 2.上传zookeeper包到指定位置(例如: /usr/local/sof ...

  8. ssh连不上的问题

    新安装的ubuntu,想要ssh远程连接,发现连接不上,何解? 答 : 在ubuntu上安装ssh. sudo apt-get install openssh-server

  9. Android开发实战——记账本(6)

    开发日志——(6) 今天将app签名打包,并部署在了真机上.真机上的截图: 运行成功:

  10. 测试理论 - Test Double

    概述 简述 test double mock, fake 之类的东西 背景 最近在看 google 软件测试之道 妈的 13 年的老书了 书里有提到 mock, fake, stub 刚好, 我又不太 ...