Android自带各式各样的弹出框。弹出框也是安卓主要的组件之中的一个。同一时候安卓程序能够对菜单键、返回键的监听。但在安卓4.0之后就禁止对Home键的屏蔽与监听,强制保留为系统守护按键。假设非要对Home键的屏蔽与监听。就会出现java.lang.IllegalArgumentException: Window type can not be changed after the window is added.的错误。

以下写一个小程序,来说明Android各式各样的弹出框。同一时候,安卓是怎样对菜单键、返回键的监听。

例如以下图:

按下Menu键则在弹出消息,

之后这个程序提供各式各样的弹出框。

每个弹出框加入不同的监听器,用来监听用户对各个button的点击。

最后按下返回键,结束这个程序。

这个程序比較简单,就一个Activity。

1、首先是在res\values\strings.xml设置app名字与各个button的显示内容例如以下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.  
  4. <string name="app_name">弹出框</string>
  5. <string name="action_settings">Settings</string>
  6. <string name="button1">带多按钮的弹出框</string>
  7. <string name="button2">带列表的弹出框</string>
  8. <string name="button3">带单选列表的弹出框</string>
  9. <string name="button4">带多选列表的弹出框</string>
  10.  
  11. </resources>

2、之后。与《【Android】利用Notification操作设备的通知栏》(点击打开链接),在res\layout\activity_main.xml设置一个自上而下的线性布局。摆放四个按钮。分别给四个按钮。设置不同的id。一会儿在MainActivity.java进行监听。各式各样的弹出框是通过java生成的。

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical" >
  5.  
  6. <Button
  7. android:id="@+id/button1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="@string/button1"
  11. android:textSize="24sp" />
  12.  
  13. <Button
  14. android:id="@+id/button2"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="@string/button2"
  18. android:textSize="24sp" />
  19.  
  20. <Button
  21. android:id="@+id/button3"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="@string/button3"
  25. android:textSize="24sp" />
  26.  
  27. <Button
  28. android:id="@+id/button4"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:text="@string/button4"
  32. android:textSize="24sp" />
  33.  
  34. </LinearLayout>

3、最后是对MainActivity.java文件的编写。整个程序分为两部分。一个是获取各个button之后,对不同button的点击事件,加入各式各样的点击事件,里面生成不同的弹出框。带多button的弹出框通过AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();创建,带列表的对话框,则通过AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);去创建。

它们都能够通过setIcon与setTitle设置图标与标题,可是之后的监听器的标题是不同的。

注意监听器的重名问题,重名的监听器,必须在声明其所在类进行区分。否则无法通过编译。

程序还有一部分是对物理button的监听。这个非常easy的。无需使用xml进行声明,直接写监听代码就可以。

  1. package com.alertdialog;
  2.  
  3. import android.os.Bundle;
  4. import android.view.KeyEvent;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.Toast;
  8. import android.app.Activity;
  9. import android.app.AlertDialog;
  10. import android.content.DialogInterface;
  11. import android.content.DialogInterface.OnMultiChoiceClickListener;
  12.  
  13. public class MainActivity extends Activity {
  14. private Button button1;
  15. private Button button2;
  16. private Button button3;
  17. private Button button4;
  18. private String[] listItems = new String[] { "选项1", "选项2", "选项3", "选项4" };// 选项列表项数组
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. button1 = (Button) findViewById(R.id.button1);
  25. button2 = (Button) findViewById(R.id.button2);
  26. button3 = (Button) findViewById(R.id.button3);
  27. button4 = (Button) findViewById(R.id.button4);
  28. // 这里的普通按钮点击监听器与对话框按钮的点击监听器是不同的,要声明是View这个类
  29. button1.setOnClickListener(new View.OnClickListener() {
  30.  
  31. @Override
  32. public void onClick(View arg0) {
  33. AlertDialog alertDialog = new AlertDialog.Builder(
  34. MainActivity.this).create();
  35. alertDialog.setIcon(R.drawable.ic_launcher);// 设置对话框的图标为应用程序的图标
  36. alertDialog.setTitle("带多按钮的弹出框");
  37. alertDialog.setMessage("对话框的内容");
  38. // 这里的对话框按钮的点击监听器与普通按钮点击监听器是不同的,要声明是DialogInterface这个类
  39. alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
  40. new DialogInterface.OnClickListener() {
  41.  
  42. @Override
  43. public void onClick(DialogInterface arg0, int arg1) {
  44. // TODO Auto-generated method stub
  45. Toast.makeText(MainActivity.this, "取消按钮被点击",
  46. Toast.LENGTH_LONG).show();
  47. }
  48. });
  49. // 不想要这个按钮则不写这种方法
  50. alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
  51. new DialogInterface.OnClickListener() {
  52.  
  53. @Override
  54. public void onClick(DialogInterface arg0, int arg1) {
  55. // TODO Auto-generated method stub
  56. Toast.makeText(MainActivity.this, "中立按钮被点击",
  57. Toast.LENGTH_LONG).show();
  58. }
  59. });
  60. // 不想要这个按钮则不写这种方法
  61. alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
  62. new DialogInterface.OnClickListener() {
  63.  
  64. @Override
  65. public void onClick(DialogInterface arg0, int arg1) {
  66. // TODO Auto-generated method stub
  67. Toast.makeText(MainActivity.this, "OK按钮被点击",
  68. Toast.LENGTH_LONG).show();
  69. }
  70. });
  71. alertDialog.show();// 必需要有这种方法。否则对话框不显示
  72. }
  73. });
  74. button2.setOnClickListener(new View.OnClickListener() {
  75.  
  76. @Override
  77. public void onClick(View arg0) {
  78. // TODO Auto-generated method stub
  79. AlertDialog.Builder builder = new AlertDialog.Builder(
  80. MainActivity.this);// 这里差别与其他组件的builder,必须这样写
  81. builder.setIcon(R.drawable.ic_launcher);// 设置对话框的图标为应用程序的图标
  82. builder.setTitle("带列表的弹出框");
  83. builder.setItems(listItems,
  84. new DialogInterface.OnClickListener() {
  85.  
  86. @Override
  87. public void onClick(DialogInterface arg0, int which) {
  88. Toast.makeText(MainActivity.this,
  89. listItems[which] + "被点击",
  90. Toast.LENGTH_LONG).show();
  91. }
  92. });
  93. builder.create().show();// 必需要有这种方法,否则对话框不显示
  94. }
  95. });
  96. button3.setOnClickListener(new View.OnClickListener() {
  97. private String chooseItem;// 用于记录被选择的项
  98.  
  99. @Override
  100. public void onClick(View arg0) {
  101. // TODO Auto-generated method stub
  102. AlertDialog.Builder builder = new AlertDialog.Builder(
  103. MainActivity.this);// 这里差别与其他组件的builder,必须这样写
  104. builder.setIcon(R.drawable.ic_launcher);// 设置对话框的图标为应用程序的图标
  105. builder.setTitle("带列表的弹出框");
  106. builder.setSingleChoiceItems(listItems, 0,
  107. new DialogInterface.OnClickListener() {
  108.  
  109. @Override
  110. public void onClick(DialogInterface arg0, int which) {
  111. chooseItem = listItems[which];
  112. }
  113. });
  114. builder.setPositiveButton("确定",
  115. new DialogInterface.OnClickListener() {
  116.  
  117. @Override
  118. public void onClick(DialogInterface arg0, int which) {
  119. Toast.makeText(MainActivity.this,
  120. chooseItem + "被点击", Toast.LENGTH_LONG)
  121. .show();
  122. }
  123. });
  124. builder.create().show();// 必需要有这种方法,否则对话框不显示
  125. }
  126. });
  127. button4.setOnClickListener(new View.OnClickListener() {
  128. private boolean[] checkedItems;// 用于记录被选择的项
  129.  
  130. @Override
  131. public void onClick(View arg0) {
  132. // TODO Auto-generated method stub
  133. AlertDialog.Builder builder = new AlertDialog.Builder(
  134. MainActivity.this);// 这里差别与其他组件的builder。必须这样写
  135. builder.setIcon(R.drawable.ic_launcher);// 设置对话框的图标为应用程序的图标
  136. builder.setTitle("带列表的弹出框");
  137. checkedItems = new boolean[] { false, false, true, true };// 初始化被选择的项
  138. builder.setMultiChoiceItems(listItems, checkedItems,
  139. new OnMultiChoiceClickListener() {
  140. // 多项选择监听器是独立的,所以在前面无须声明其所在类
  141. @Override
  142. public void onClick(DialogInterface arg0,
  143. int which, boolean isChecked) {
  144. // TODO Auto-generated method stub
  145. checkedItems[which] = isChecked;// 随意一项被选择与否。其相应数组的布尔值会被改变
  146. }
  147. });
  148. builder.setPositiveButton("确定",
  149. new DialogInterface.OnClickListener() {
  150.  
  151. @Override
  152. public void onClick(DialogInterface arg0, int which) {
  153. // 寻找被选择项所相应的选项
  154. String result = "";
  155. for (int i = 0; i < checkedItems.length; i++) {
  156. if (checkedItems[i]) {
  157. result += listItems[i] + ",";
  158. }
  159. }
  160. if (!result.equals("")) {// 假设用户有选择东西
  161. Toast.makeText(MainActivity.this,
  162. result + "被选择", Toast.LENGTH_LONG)
  163. .show();
  164. } else {
  165. Toast.makeText(MainActivity.this,
  166. "没有选项被选择", Toast.LENGTH_LONG)
  167. .show();
  168. }
  169. }
  170. });
  171. builder.create().show();// 必需要有这种方法,否则对话框不显示
  172. }
  173. });
  174. }
  175.  
  176. //对物理按钮的监听
  177. @Override
  178. public boolean onKeyDown(int keyCode, KeyEvent event) {
  179. switch (keyCode) {
  180. case KeyEvent.KEYCODE_MENU:
  181. Toast.makeText(MainActivity.this, "菜单键被按下", Toast.LENGTH_LONG)
  182. .show();
  183. break;
  184. case KeyEvent.KEYCODE_BACK:
  185. finish();
  186. break;
  187. }
  188. return super.onKeyDown(keyCode, event);
  189. }
  190. }

【Android】各式各样的弹出框与对菜单键、返回键的监听的更多相关文章

  1. android 三种弹出框之一PopupWindow

    PopupWindow 在android的弹出框我目前了解到的是有三种:AlertDialog,PopupWindow,Activity伪弹框, AlertDialog太熟悉了,这里就不介绍了 就先看 ...

  2. Android 自定义界面的弹出框(可输入数据)

    上午写了一篇博文,介绍了如何定义从屏幕底部弹出PopupWindow,写完之后,突然想起之前写过自定义内容显示的弹出框,就随手写了两个实例,分享出来: 第一种实现方式:继承Dialog 1.1 线定义 ...

  3. Android应用中返回键的监听及处理

    MainActivity: package com.testnbackpressed;  import android.os.Bundle;  import android.view.KeyEvent ...

  4. Android窗口为弹出框样式

    1.XML android:theme="@android:style/Theme.Dialog <?xml version="1.0" encoding=&quo ...

  5. android开发学习 ------- 弹出框

    这是一种方法,是我觉得简单易懂代码量较少的一种: /* 创建AlertDialog对象并显示 */ final AlertDialog alertDialog = new AlertDialog.Bu ...

  6. Android 开发笔记 “弹出框”

    AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this); builder.setMessage("Are y ...

  7. [转]Android应用中返回键的监听及处理

    用户在点击手机的返回按钮时,默认是推出当前的activty,但是有时用户不小心按到返回,所以需要给用户一个提示,这就需要重写onkeydown事件,实现的效果如下:   标签:    Android ...

  8. android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果

    需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果,  总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...

  9. android标题栏下面弹出提示框(一) TextView实现,带动画效果

    产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...

随机推荐

  1. codeforces@281 B

    shui #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g ...

  2. 英语音乐---二、Burning

    英语音乐---二.Burning 一.总结 一句话总结:Burning - Maria Arredondo 玛丽亚·亚瑞唐多(Maria Arredondo),1985年7月6日出生于文内斯拉小镇,挪 ...

  3. struts2入门(搭建环境、配置、示例)

    转自:https://blog.csdn.net/u012862311/article/details/53412716 1.下载Struts2的jar包 下载地址:http://archive.ap ...

  4. windows下远程链接虚拟机Linux下MySQL数据库问题处理

    参考解决:https://www.cnblogs.com/blogforly/p/5997553.html 今天远程连接虚拟机中的MySQL数据库,一直连不上,在网上搜索了一下,发现原因是MySQL对 ...

  5. 976 B. Lara Croft and the New Game

    You might have heard about the next game in Lara Croft series coming out this year. You also might h ...

  6. numa 和 mysql

    cpu numa结构反应的内存访问速度问题: 在多核cpu的时代引入了cpu的numa(非一致内存访问结构): NUMA引入了node的概念,每个物理CPU都被视作一个node,而每个node都有一个 ...

  7. Python3基础笔记--生成器

    目录: 一.列表生成器 二.生成器 三.迭代器 一.列表生成器 a = [x for x in range(10)] b= [y*2 for y in range(10)] def f(n) retu ...

  8. WIFI 测试和调试

    WIFI测试和调试 本文将介绍如何使用 ASOP 中提供的工具测试和调试 WLAN 实现. 测试 为了测试 WLAN 框架,AOSP 提供了一系列单元测试.集成测试 (ACTS) 和 CTS 测试. ...

  9. LINUX 上源代码安装与配置samba服务,支持从windows上读写LINUX文件。

    ###动机###在windows编写代码文件比较方便,因为有source insight.但是需要在LINUX上编译.一种办法就是使用samba文件共享. [1] 下载samba代码.按照config ...

  10. Intel NUC迷你机2019年底迎来i9 8核心16线程

    Intel处理器这两年全年提速,虽然10nm新工艺受阻,但核心数在全面增加,从发烧到桌面到低功耗莫不如此,如今连NUC迷你机也要全新进化了,一年多之后就会迎来8核心16线程,而且也划入i9序列. 根据 ...