Android开发基础之AlertDialog的单选对话框的使用
2015-07-30 23:00
633 查看
有时候我们有这样的需求,就是弹出一个单选对话框,然后让用户在对话框中进行选择,如性别的选择,年龄的选择等等。而在Android的AlertDialog中封装了单选对话框,完全能胜任这个需求。具体实现如下:
(1)声明一个全局数组,作为单选对话框中的每一项;
(2)为按钮添加响应事件,在该响应事件中弹出对话框,具体代码在下列方法中实现,代码如下:
(3)最后的实现效果如下:
。
。
有了AlertDialog的单选对话框,能有效的提高用户体验,灵活的与用户进行交互。
github主页:https://github.com/chenyufeng1991 。欢迎大家访问!
(1)声明一个全局数组,作为单选对话框中的每一项;
String[] single_list = {"菜单一", "菜单二", "菜单三"};
(2)为按钮添加响应事件,在该响应事件中弹出对话框,具体代码在下列方法中实现,代码如下:
private void showSingleChoiceDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("单选对话框"); builder.setIcon(R.mipmap.ic_launcher); builder.setSingleChoiceItems(single_list, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String str = single_list[which]; Toast.makeText(MainActivity.this, str + "被点击了", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); AlertDialog dialog = builder.create(); dialog.show(); }
(3)最后的实现效果如下:
。
。
有了AlertDialog的单选对话框,能有效的提高用户体验,灵活的与用户进行交互。
github主页:https://github.com/chenyufeng1991 。欢迎大家访问!
相关文章推荐
- Android蓝牙串口通信模板及demo,trick
- 《Android软件安全与逆向分析》 signapk.jar签名apk工具--【整理学习分享,一部到位!】
- Android 百分比布局库(percent-support-lib) 解析与扩展
- Android开发基础之动态添加菜单项
- Android基础知识之四大组件Activity(二)一个Activity的生命周期?
- Android开发之两种方式实现连续点击两次返回键退出程序
- android 定时器的使用
- android 定时器的使用
- android 定时器的使用
- android 定时器的使用
- Android开发基础之菜单键的使用
- android 定时器的使用
- android 定时器的使用
- android 定时器的使用
- android 定时器的使用
- [Android问答] px、dp和sp,这些单位有什么区别?
- android 定时器的使用
- android 定时器的使用
- Android 多媒体开发学习之撕衣服
- Android中Activity切换时共享视图元素的切换动画(4.x兼容方案)