android中常用的弹出提示框
来源:互联网 发布:webp转换jpg软件 编辑:程序博客网 时间:2024/06/05 11:24
我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的朋友都会知道,大部分的平台都只提供了几个最简单的实现,如果我们想实现自己特定需求的对话框,大家可能首先会想到,通过继承等方式,重写我们自己的对话框。当然,这也是不失为一个不错的解决方式,但是一般的情况却是这样,我们重写的对话框,也许只在一个特定的地方会用到,为了这一次的使用,而去创建一个新类,往往有点杀鸡用牛刀的感觉,甚至会对我们的程序增加不必要的复杂性,对于这种情形的对话框有没有更优雅的解决方案呢?
幸运的是,android提供了这种问题的解决方案,刚开始接触android的时候,我在做一个自定义对话框的时候,也是通过继承的方式来实现,后来随着对文档了解的深入,发现了android起始已经提供了相应的接口Dialog Builder ,下面我就吧相关的内容在这里分享一下,也能让更多的初学者少走弯路。
首先是一个最简单的应用,就是弹出一个消息框,在android中可以这样实现
- 1
- new AlertDialog.Builder(self)
- 2
- .setTitle("标题" )
- 3
- .setMessage("简单消息框" )
- 4
- .setPositiveButton("确定" , null )
- 5
- .show();
- new AlertDialog.Builder(self)
- .setTitle("确认" )
- .setMessage("确定吗?" )
- .setPositiveButton("是" , null )
- .setNegativeButton("否" , null)
- .show();
注意到,这里有两个null参数,这里要放的其实是这两个按钮点击的监听程序,由于我们这里不需要监听这些动作,所以传入null值简单忽略掉,但是实际开发的时候一般都是需要传入监听器的,用来响应用户的操作。
下面是一个可以输入文本的对话框
- new AlertDialog.Builder(self)
- .setTitle("请输入" )
- .setIcon(android.R.drawable.ic_dialog_info)
- .setView(new EditText(self))
- .setPositiveButton("确定" , null)
- .setNegativeButton("取消" , null )
- .show();
- new AlertDialog.Builder(self)
- .setTitle("请选择" )
- .setIcon(android.R.drawable.ic_dialog_info)
- .setSingleChoiceItems(new String[] {"选项1", "选项2", "选项3" , "选项4" }, 0 ,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- }
- )
- .setNegativeButton("取消" , null )
- .show();
- new AlertDialog.Builder(self)
- .setTitle("多选框" )
- .setMultiChoiceItems(new String[] {"选项1", "选项2", "选项3" , "选项4" }, null , null)
- .setPositiveButton("确定" , null)
- .setNegativeButton("取消" , null )
- .show();
- new AlertDialog.Builder(self)
- .setTitle("列表框" )
- .setItems(new String[] {"列表项1", "列表项2", "列表项3" }, null )
- .setNegativeButton("确定" , null )
- .show();
- ImageView img = new ImageView(self);
- img.setImageResource(R.drawable.icon);
- new AlertDialog.Builder(self)
- .setTitle("图片框" )
- .setView(img)
- .setPositiveButton("确定" , null )
- .show();
0 0
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- android中常用的弹出提示框
- Uinty之EditorWindow-Label、Toggle、Slider -二
- iOS 上常用的两个功能:点击屏幕和return退出隐藏键盘和解决虚拟键盘挡住UITextField的方法
- 信号的阻塞
- codeforces 499A watching a movie
- hadoop2.2完全分布式最新高可靠安装文档
- android中常用的弹出提示框
- 聚类与分类的区别
- opencv中的Mat结构操作
- 经习近平主席批准 中央军委颁发 《关于努力建设听党指挥、善谋打仗的新型司令机关的意见》
- 快速安装CocoaPods教程
- 日经春秋 20141226
- 一步步将vim改造成C/C++开发环境(IDE)
- 算法复杂度中的O(logN)底数是多少
- Java Oracle存储过程问题