【Android】各式各样的弹出框与对菜单键、返回键的监听
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的显示内容例如以下:
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">弹出框</string>
<string name="action_settings">Settings</string>
<string name="button1">带多按钮的弹出框</string>
<string name="button2">带列表的弹出框</string>
<string name="button3">带单选列表的弹出框</string>
<string name="button4">带多选列表的弹出框</string> </resources>
2、之后。与《【Android】利用Notification操作设备的通知栏》(点击打开链接),在res\layout\activity_main.xml设置一个自上而下的线性布局。摆放四个按钮。分别给四个按钮。设置不同的id。一会儿在MainActivity.java进行监听。各式各样的弹出框是通过java生成的。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button1"
android:textSize="24sp" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button2"
android:textSize="24sp" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button3"
android:textSize="24sp" /> <Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button4"
android:textSize="24sp" /> </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进行声明,直接写监听代码就可以。
package com.alertdialog; import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener; public class MainActivity extends Activity {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private String[] listItems = new String[] { "选项1", "选项2", "选项3", "选项4" };// 选项列表项数组 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
// 这里的普通按钮点击监听器与对话框按钮的点击监听器是不同的,要声明是View这个类
button1.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) {
AlertDialog alertDialog = new AlertDialog.Builder(
MainActivity.this).create();
alertDialog.setIcon(R.drawable.ic_launcher);// 设置对话框的图标为应用程序的图标
alertDialog.setTitle("带多按钮的弹出框");
alertDialog.setMessage("对话框的内容");
// 这里的对话框按钮的点击监听器与普通按钮点击监听器是不同的,要声明是DialogInterface这个类
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "取消按钮被点击",
Toast.LENGTH_LONG).show();
}
});
// 不想要这个按钮则不写这种方法
alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "中立按钮被点击",
Toast.LENGTH_LONG).show();
}
});
// 不想要这个按钮则不写这种方法
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "OK按钮被点击",
Toast.LENGTH_LONG).show();
}
});
alertDialog.show();// 必需要有这种方法。否则对话框不显示
}
});
button2.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);// 这里差别与其他组件的builder,必须这样写
builder.setIcon(R.drawable.ic_launcher);// 设置对话框的图标为应用程序的图标
builder.setTitle("带列表的弹出框");
builder.setItems(listItems,
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int which) {
Toast.makeText(MainActivity.this,
listItems[which] + "被点击",
Toast.LENGTH_LONG).show();
}
});
builder.create().show();// 必需要有这种方法,否则对话框不显示
}
});
button3.setOnClickListener(new View.OnClickListener() {
private String chooseItem;// 用于记录被选择的项 @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);// 这里差别与其他组件的builder,必须这样写
builder.setIcon(R.drawable.ic_launcher);// 设置对话框的图标为应用程序的图标
builder.setTitle("带列表的弹出框");
builder.setSingleChoiceItems(listItems, 0,
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int which) {
chooseItem = listItems[which];
}
});
builder.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int which) {
Toast.makeText(MainActivity.this,
chooseItem + "被点击", Toast.LENGTH_LONG)
.show();
}
});
builder.create().show();// 必需要有这种方法,否则对话框不显示
}
});
button4.setOnClickListener(new View.OnClickListener() {
private boolean[] checkedItems;// 用于记录被选择的项 @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);// 这里差别与其他组件的builder。必须这样写
builder.setIcon(R.drawable.ic_launcher);// 设置对话框的图标为应用程序的图标
builder.setTitle("带列表的弹出框");
checkedItems = new boolean[] { false, false, true, true };// 初始化被选择的项
builder.setMultiChoiceItems(listItems, checkedItems,
new OnMultiChoiceClickListener() {
// 多项选择监听器是独立的,所以在前面无须声明其所在类
@Override
public void onClick(DialogInterface arg0,
int which, boolean isChecked) {
// TODO Auto-generated method stub
checkedItems[which] = isChecked;// 随意一项被选择与否。其相应数组的布尔值会被改变
}
});
builder.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int which) {
// 寻找被选择项所相应的选项
String result = "";
for (int i = 0; i < checkedItems.length; i++) {
if (checkedItems[i]) {
result += listItems[i] + ",";
}
}
if (!result.equals("")) {// 假设用户有选择东西
Toast.makeText(MainActivity.this,
result + "被选择", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(MainActivity.this,
"没有选项被选择", Toast.LENGTH_LONG)
.show();
}
}
});
builder.create().show();// 必需要有这种方法,否则对话框不显示
}
});
} //对物理按钮的监听
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
Toast.makeText(MainActivity.this, "菜单键被按下", Toast.LENGTH_LONG)
.show();
break;
case KeyEvent.KEYCODE_BACK:
finish();
break;
}
return super.onKeyDown(keyCode, event);
}
}
【Android】各式各样的弹出框与对菜单键、返回键的监听的更多相关文章
- android 三种弹出框之一PopupWindow
PopupWindow 在android的弹出框我目前了解到的是有三种:AlertDialog,PopupWindow,Activity伪弹框, AlertDialog太熟悉了,这里就不介绍了 就先看 ...
- Android 自定义界面的弹出框(可输入数据)
上午写了一篇博文,介绍了如何定义从屏幕底部弹出PopupWindow,写完之后,突然想起之前写过自定义内容显示的弹出框,就随手写了两个实例,分享出来: 第一种实现方式:继承Dialog 1.1 线定义 ...
- Android应用中返回键的监听及处理
MainActivity: package com.testnbackpressed; import android.os.Bundle; import android.view.KeyEvent ...
- Android窗口为弹出框样式
1.XML android:theme="@android:style/Theme.Dialog <?xml version="1.0" encoding=&quo ...
- android开发学习 ------- 弹出框
这是一种方法,是我觉得简单易懂代码量较少的一种: /* 创建AlertDialog对象并显示 */ final AlertDialog alertDialog = new AlertDialog.Bu ...
- Android 开发笔记 “弹出框”
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this); builder.setMessage("Are y ...
- [转]Android应用中返回键的监听及处理
用户在点击手机的返回按钮时,默认是推出当前的activty,但是有时用户不小心按到返回,所以需要给用户一个提示,这就需要重写onkeydown事件,实现的效果如下: 标签: Android ...
- android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果
需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果, 总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...
- android标题栏下面弹出提示框(一) TextView实现,带动画效果
产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...
随机推荐
- Android 输入框限制字符输入数
有时候对Android的输入框有字符输入数量的限制,而且显示字符输入的数量.通过下面方式能够实现: 1.自己定义LimitNumEditText继承EditText import android.co ...
- [BZOJ3670] [NOI2014] 动物园 解题报告 (KMP)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3670 Description 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅, ...
- C语言基础-第六章
数组和字符串 1.一维数组 数组当中最简单的数据 声明: 类型说明符 数组名[常量表达式] int a[3];说明a的长度为3,那么给a赋值的语句是:a={1,2,3}; 2.多维数组 2.1 二维数 ...
- mac终端(terminal)里的快捷键
Command + K 清屏 Command + T 新建标签 Command +W 关闭当前标签页 Command + S 保存终端输出 Command + D 垂直分隔当前标签页 Command ...
- Java中将String转json对象
import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple. ...
- 【agc004f】Namori Grundy
那个问一下有人可以解释以下这个做法嘛,看不太懂QwQ~ Description 有一个n个点n条边的有向图,点的编号为从1到n. 给出一个数组p,表明有(p1,1),(p2,2),…,(pn,n)这n ...
- Ubuntu下Matlab代码中中文注释乱码解决方案
环境:Ubuntu18.04,Matlab R2017b. 把matlab文件从windows拷贝到Ubuntu中,打开发现原先的中文注释全部乱码.真正原因是因为windows中.m文件采用的是gbk ...
- [Poi] Setup PostCSS and Tailwind with Poi
This lesson walks through setting up a Poi project using PostCSS and the popular Tailwind library fo ...
- MFC Wizard创建的空应用程序中各个文件内容的解析
创建的MFC应用程序名为:wd,那么: 一.wd.h解析 // wd.h : main header file for the WD application // #if !defined(AFX_W ...
- vue16 自定义键盘属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...