在Android开发中,AlertDialog常用于处理用户的登录等。那么如何获取由 AlertDialog 生成的对话框中EditText的文本内容呢?

其实Alertdialog弹出的Activity 可以认为是默认的Activity的子Activity。不能直接使用findViewById()来获取。强行使用会提示一个空指针的错误,我们只需要加上  Alertdialog弹出的Activity.findViewById()就可以获取我们想要的EditText对象了。

关键代码如下:

/**  Activity01.java  **/

public String strUsername = "";

LayoutInflater factory = LayoutInflater.from(Activity01.this);

//得到自定义对话框

final View DialogView = factory.inflate(R.layout.dialog, null);

//创建对话框

AlertDialog dlg = new AlertDialog.Builder(Activity01.this)

.setTitle("登录框")

.setView(DialogView)//设置自定义对话框的样式

.setPositiveButton("确定", //设置"确定"按钮

new DialogInterface.OnClickListener() //设置事件监听

{

public void onClick(DialogInterface dialog, int whichButton) {

//输入后点击“确定”,开始获取我们要的内容 DialogView就是AlertDialog弹出的Activity

EditText edtUserName = (EditText)DialogView.findViewById(R.id.username);

strUserName = edtUserName.getText().toString();

}

})

.setNegativeButton("取消", //设置“取消”按钮

new DialogInterface.OnClickListener()

{

public void onClick(DialogInterface dialog, int whichButton) {

//点击"取消"按钮之后退出程序

Activity01.this.finish();

}

})

.create();//创建弹出框

dlg.show();//显示

/**  dialog.xml  **/

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

android:id="@+id/usernamet"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:layout_marginLeft="20dip"

android:layout_marginRight="20dip"

android:text="账号"

android:gravity="left"

android:textAppearance="?android:attr/textAppearanceMedium" />

android:id="@+id/username"

android:layout_height="wrap_content"

android:layout_width="fill_parent"

android:layout_marginLeft="20dip"

android:layout_marginRight="20dip"

android:scrollHorizontally="true"

android:autoText="false"

android:capitalize="none"

android:gravity="fill_horizontal"

android:textAppearance="?android:attr/textAppearanceMedium"/>

android:id="@+id/passwordt"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:layout_marginLeft="20dip"

android:layout_marginRight="20dip"

android:text="密码"

android:gravity="left"

android:textAppearance="?android:attr/textAppearanceMedium" />

android:id="@+id/password"

android:layout_height="wrap_content"

android:layout_width="fill_parent"

android:layout_marginLeft="20dip"

android:layout_marginRight="20dip"

android:scrollHorizontally="true"

android:autoText="false"

android:capitalize="none"

android:gravity="fill_horizontal"

android:password="true"

android:textAppearance="?android:attr/textAppearanceMedium" />

alert获取输入框内容_获取由 AlertDialog 生成的对话框中EditText的文本内容相关推荐

  1. python爬取付费隐藏内容_如何用python 爬取网页中隐藏的div内容?

    图片所在的html元素id叫 viewimg ,这个元素是由 view.js 当中的 loadview 函数创建的.该函数吧serverurl跟photosrc这个array里头的元素拼接,并把他设置 ...

  2. c语言如何用fscanf将字符串读取,在c语言中如何将文本内容 赋给一个 字符串

    在c语言中如何将文本内容 赋给一个 字符串 来源:互联网  宽屏版  评论 2009-08-13 04:59:12 分类: 电脑/网络 >> 程序设计 >> 其他编程语言 问题 ...

  3. Excel表格中多个文本内容快速合并到一个单元格内

    Excel表格中多个文本内容快速合并到一个单元格内 目录 Excel表格中多个文本内容快速合并到一个单元格内 1.在合并单元格内输入"=PHONETIC()"函数 2.框选需要合并 ...

  4. html通过php获取mysql数据_怎样借助PHP从HTML网页中获取phpmyadmin数据库里数据表的内容...

    这是我以前写的,先把文档改成 .php格式,代码写在html最后,$sql = "SELECT num,foodname, foodprice, foodcontent,foodImg FR ...

  5. java获取空闲端口_获取系统空闲端口

    端口取值范围 以下搜自互联网 一般用到的是1到65535,其中0不使用,1-1023为系统端口,也叫BSD保留端口;1024-65535为用户端口,又分为: BSD临时端口(1024-5000)和BS ...

  6. 如何在ppt下面加入讲解内容_学术展示系列:学术PPT教程(下)内容 讲解

    在学术PPT教程(上)中,介绍了学术PPT基本的外观要求,学术PPT外观应该是比较容易达到基本要求的,对于新生来说,画不太大的练习成本.做一份规范美观的PPT就可以帮你的报告加分不少,教程(上): h ...

  7. datefromstring 转换不准确_免费的在线OCR工具,将图片内容转换为文本内容

    利爪按:OCR工具我不常用,但貌似有些小伙伴还是对此有着较大需求的,之前分享了一款手机端的免费OCR应用「白描」,应该一直都可以用吧?今天分享一个在线OCR的网站,不想下载软件的可以收藏一下. Eas ...

  8. word中插入公式的快捷键_如何使用插入键在Word中插入复制的内容

    word中插入公式的快捷键 In Word, the "Insert" key on the keyboard can be used to switch between Inse ...

  9. 5 控件固定大小_【聊技术】在Android中实现自适应文本大小显示

    本周的聊技术话题和大家说说如何在Android中实现自适应文本大小显示. 想象一下,在布局中,通常显示文本的区域大小是固定的,但是文本长度并不总是固定的.比如列表中的文章标题.界面下方的按钮文本等等. ...

最新文章

  1. mysql数据库自动转储_mysql数据库数据定时封装转储
  2. 开启计算机开机引导兼容模式,Intel决定2020年封禁UEFI兼容模式 将无法开启CSM来启动操作系统...
  3. HD Piggy-Bank完全背包
  4. ots在线考计算机的word,ots在线考试系统1(OTS online examination system 1).doc
  5. 工作125:各个接口需要的token不同 token是获取数据的关键
  6. 作者:郑飞翔(1982-),男,中国农业科学院农业环境与可持续发展研究所副研究员。...
  7. 深入剖析Linux IO原理和几种零拷贝机制的实现
  8. codeforce 985C Liebig's Barrels
  9. JS魔法堂:LINK元素深入详解
  10. 软考论文-写作大纲-备考思路总结
  11. SuperMap iDesktop之夜景特效制作
  12. html显示emoji表情,在web页面显示emoji表情
  13. 技术是如何创造价值的
  14. 360度反馈调查表中的问题示范
  15. Krpano元素的一些解析
  16. 自动化工具之UIAutomator
  17. 甘肃一名高考生偷偷带手机进考场,拍题并上传到网上出钱求答案……
  18. java-IO流-将文件夹以树形结构打印(打印文件夹目录)问题
  19. ios swiftui_ios swiftui中的本地化
  20. TreeView的使用方法

热门文章

  1. [VBA] 设置行高和列宽,以及全选单元格
  2. 对《生产流水线模式》讨论的总结性回复
  3. MATLAB图像处理基础
  4. 澳洲虚拟主机空间_澳洲空间|澳洲虚拟主机|澳洲主机|澳洲虚拟空间-万纵科技 www.xmwzidc.cn...
  5. 如何卸载mysql server 2005_如何卸载SQL Server 2005
  6. C++ #include <.h>和“.h“的区别
  7. pytorch每次迭代训练前都重新对数据集进行采样形成平衡数据集
  8. python asyncio 异步编程-协程 2
  9. batch批处理程序easyadd——追加单行文本到指定txt文件末尾
  10. Android之多线程断点下载