Back弹出AlertDialog
package com.pingyijinren.helloworld.activity; import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast; import com.pingyijinren.helloworld.R;
public class MainActivity extends AppCompatActivity {
private AlertDialog alertDialog;
private CharSequence[] data=new CharSequence[]{"香蕉","苹果","桃子"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("程序退出?");
// builder.setMessage("确定退出吗?");
builder.setPositiveButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { }
});
builder.setSingleChoiceItems(data, 0, new DialogInterface.OnClickListener() { //其实就是setMessage
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,data[which],Toast.LENGTH_SHORT).show();
}
});
alertDialog=builder.create();
alertDialog.show();
}
return super.onKeyDown(keyCode, event);
}
}
Back弹出AlertDialog的更多相关文章
- android 弹出AlertDialog的学习案例
我在编写的时候,测试的关键代码: AlertDialog.Builder builder=new AlertDialog.Builder(MainPointListActivity.this); bu ...
- 弹出AlertDialog的时候报You need to use a Theme.AppCompat theme (or descendant) with this activity错误
今天遇到一个bug,用百度地图的时候,我对上面的标注设置了点击监听,设置的相应的反应是弹出一个AlertDialog 记录一解决bug的历程 但是Dialog却没有弹出来,一看AS下面,报了这错,起初 ...
- AlertDialog弹出时背景明暗程度调整
今天有个需求是把弹出AlertDialog时的变暗的背景调整得不要那么暗. 一开始懒惰就直接百度中文搜索,结果找到的代码试了几次都不行. 后来老老实实开google.stackoverflow搜索,搜 ...
- (原)android的alertdialog中加入edittext但是不弹出软键盘等问题的解决与原因
摘要:alertdialog中加入edittext但是不弹出软键盘等问题网上有很多不管用的解决方案, 本文意在给出更有效的解决办法,并初步探究其原因 正文 在对话框中插入文本框是十分常见的需求 通常我 ...
- AlertDialog.Builder弹出对话框
在Android中,弹出对话框使用AlertDialog.Builder方法. new AlertDialog.Builder(MainActivity.this).setTitle("本机 ...
- android 自定义弹出框AlertDialog ,很炫的哦
于是就小小的模仿了下自己写了这个这样的效果,主要代码如下:dlg = new AlertDialog.Builder(context).create();dlg.show();dlg.getWin ...
- 安卓弹出对话框——Alertdialog
在Android开发当中,在界面上弹出一个Dialog对话框使我们经常需要做的,本篇随笔将详细的讲解Dialog对话框这个概念,包括定义不同样式的对话框. 一.Dialog 我们首先来看看androi ...
- 安卓弹出对话框——Alertdialog(一)
首先看各种样式的对话框: 我们看到,Dialog有很多的子类实现,所以我们要定义一个对话框,使用其子类来实例化一个即可,而不要直接使用Dialog这个父类来构造. 二.AlertDialog 今天我们 ...
- Android 自定义AlertDialog的写法和弹出软键盘和覆盖状态栏
private void showMyDialog(int layoutId){ AlertDialog myDialog = new AlertDialog.Builder(context).cre ...
随机推荐
- Vue 2.0入门基础知识之内部指令
1.Vue.js介绍 当前前端三大主流框架:Angular.React.Vue.React前段时间由于许可证风波,使得Vue的热度蹭蹭地上升.另外,Vue友好的API文档更是一大特色.Vue.js是一 ...
- Android 基础知识总结
搞了这么久安卓开发,对基础的知识点总会遗忘,所有有必要总结一下:
- synchronize早已经没那么笨重
我发现一些同学在网络上有看不少synchronize的文章,可能有些同学没深入了解,只看了部分内容,就急急忙忙认为不能使用它,很笨重,因为是采用操作系统同步互斥信号量来实现的.关于这类的对于synch ...
- git push时报错filename too long的解决
命令行输入:git config core.longpaths true 之后再进行 git 的push命令
- Java练习题02
问题: 编程求一个整数数组的最大值.最小值.平均值和所有元素的和. 代码: public class Page99{ public static void main(String args[] ...
- WEB前端JS与UI框架
前端Js框架汇总 概述: 有些日子没有正襟危坐写博客了,互联网飞速发展的时代,技术更新迭代的速度也在加快.看着Java.Js.Swift在各领域心花路放,也是煞是羡慕.寻了寻.net的消息,也是振奋人 ...
- Big Data Mindmap
- cyclic swapping algorithm
原文见:https://leetcode.com/problems/couples-holding-hands/discuss/113362/JavaC%2B%2B-O(N)-solution-usi ...
- python 网络编程篇
基础模拟通话网络程序: #客户端 import socket client = socket.socket() client.connect(('localhost',6969)) client.se ...
- Windows虚拟桌面
PROCESS_INFORMATION ProcessInfo; STARTUPINFO StartupInfo; HDESK hDesktop; HDESK hOriginalThread; HDE ...