在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式。AlertDialog中定义的一些对话框往往无法满足我们关于对话框的需求,这时我们就需要通过自定义对话框VIEW来实现需求,这里我自定义一个登陆的提示对话框,效果图显示如下:


Layout(alertdialog自定义登陆按钮)界面代码:
 <?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">
<Button
android:text="自定义登陆"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button5"
android:onClick="login"/>
</LinearLayout>

Layout(login_layout登陆窗口)界面:

 <?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">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="请输入用户名"
android:id="@+id/et_username"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="请输入密码"
android:id="@+id/et_password"/>
</LinearLayout>
java功能实现代码:
 public class AlertDialogDemo extends AppCompatActivity {
private EditText et_username,et_password;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alertdialog);
}
public void login(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("登录");
//通过布局填充器获login_layout
View view = getLayoutInflater().inflate(R.layout.login_layout,null);
//获取两个文本编辑框(密码这里不做登陆实现,仅演示)
final EditText et_username = (EditText) view.findViewById(R.id.et_username);
final EditText et_password = (EditText) view.findViewById(R.id.et_password);
builder.setView(view);//设置login_layout为对话提示框
builder.setCancelable(false);//设置为不可取消
//设置正面按钮,并做事件处理
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String name = et_username.getText().toString().trim();
String pass = et_password.getText().toString().trim();
Toast.makeText(AlertDialogDemo.this,name + "正在登录....",Toast.LENGTH_SHORT).show();
}
});
//设置反面按钮,并做事件处理
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlertDialogDemo.this,"取消登录",Toast.LENGTH_SHORT).show();
}
});
builder.show();//显示Dialog对话框
}
}
												

Android中的AlertDialog使用示例五(自定义对话框)的更多相关文章

  1. Android中的AlertDialog使用示例一(警告对话框)

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...

  2. Android中的AlertDialog使用示例二(普通选项对话框)

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...

  3. Android中的AlertDialog使用示例三(单向选择确定对话框)

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...

  4. Android中的AlertDialog使用示例四(多项选择确定对话框)

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...

  5. Android 中的AlertDialog使用自定义布局

    Android使用指定的View开发弹窗功能 Android开发中进程会使用到我们的AlertDialog,但是比较可惜的是我们的Android原生的AlertDialog的效果又比较的简陋,这个时候 ...

  6. Android中使用AlertDialog实现几种不同的对话框

    场景 app中常见的对话框. 简单的带确定取消按钮的对话框 带列表的对话框 带单项选择的对话框 带多项选择的对话框 注: 博客: https://blog.csdn.net/badao_liumang ...

  7. Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)

    昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...

  8. Android中ProgressDialog的简单示例

    网上一般对进度条的示例都是如何显示,没有在任务结束如何关闭的文章,参考其他文章经过试验之后把整套进度条显示的简单示例如下: 建立android工程等工作都略去,Google一下就可以了. 下面来介绍主 ...

  9. android中的AlertDialog具体概述

    android的AlertDialog具体解释 AlertDialog的构造方法所有是Protected的.所以不能直接通过new一个AlertDialog来创建出一个AlertDialog. 要创建 ...

随机推荐

  1. Jexus服务器SSL二级证书安装指南

    申请获得服务器证书有三张,一张服务器证书,二张中级CA证书.在Android微信中访问Https,如果服务器只有一张CA证书,就无法访问. 获取服务器证书中级CA证书: 为保障服务器证书在客户端的兼容 ...

  2. 分段二次插值——用Python进行数值计算

    事实上在实际使用中,高次插值显然是很不适合的,高次插值将所有样点包涵进一个插值函数中,这是次幂高的原因.高次计算复杂,而且刚开始的一点误差会被方的很大.因此将整个区间分为若干个小区间,在每一个小区间进 ...

  3. Spark 运行架构核心总结

    摘要: 1.基本术语 2.运行架构 2.1基本架构 2.2运行流程  2.3相关的UML类图  2.4调度模块: 2.4.1作业调度简介 2.4.2任务调度简介 3.运行模式 3.1 standalo ...

  4. 【.NET深呼吸】Zip文件操作(1):创建和读取zip文档

    .net的IO操作支持对zip文件的创建.读写和更新.使用起来也比较简单,.net的一向作风,东西都准备好了,至于如何使用,请看着办. 要对zip文件进行操作,主要用到以下三个类: 1.ZipFile ...

  5. 【热文】 为什么很多硅谷工程师偏爱 OS X,而不是 Linux 或 Windows?

    校对:伯乐在线 - 黄利民 链接: 1. Why do most of the developers in Silicon Valley prefer OS X over Linux or Windo ...

  6. 计算机程序的思维逻辑 (49) - 剖析LinkedHashMap

    之前我们介绍了Map接口的两个实现类HashMap和TreeMap,本节来介绍另一个实现类LinkedHashMap.它是HashMap的子类,但可以保持元素按插入或访问有序,这与TreeMap按键排 ...

  7. CRL快速开发框架系列教程二(基于Lambda表达式查询)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  8. Yii2的深入学习--行为Behavior

    我们先来看下行为在 Yii2 中的使用,如下内容摘自 Yii2中文文档 行为是 [[yii\base\Behavior]] 或其子类的实例.行为,也称为 mixins,可以无须改变类继承关系即可增强一 ...

  9. 排版紧凑情况下IOS 浏览器的文字部分选中问题

    一.需求 一个每一项都是图文混排的列表页,在需要对其中的某一部分文字进行选中copy的时候,IOS个二货每次都是直接选中了整个列表项,无论怎么操作它的选框都没有办法做到部分选中. 这是我本周遇到遇到的 ...

  10. 用CIL写程序:从“call vs callvirt”看方法调用

    前文回顾:<用CIL写程序系列> 前言: 最近的时间都奉献给了加班,距离上一篇文章也有半个多月了.不过在上一篇文章<用CIL写程序:定义一个叫“慕容小匹夫”的类>中,匹夫和各位 ...