AlertDialog的使用
来源:互联网 发布:头晕是怎么回事 知乎 编辑:程序博客网 时间:2024/05/05 01:41
一、AlertDialog创建简单列表项对话框核心代码如下:
final String items[] = getResources().getStringArray(R.array.books); AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("简单的列表项对话框").setIcon(R.mipmap.ic_launcher) .setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "选中了"+items[which], Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",null); AlertDialog dialog = builder.create(); dialog.show();</span>其中数组books为xml资源,定义在res/values/array.xml中,代码如下:
<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="books"> <item>基础英语</item> <item>工程数学</item> <item>高等计算机网络</item> <item>软件安全</item> </string-array></resources></span>getResource().getStringArray(R.array.X)可以从xml数组文件中获得字符串数组
二、AlertDialog创建单选列表项对话框
将builder.setItems()改为builder.setSingleItems(items[],int(默认选中项的索引),listener);
三、AlertDialog创建多选列表项对话框
将上面例子中的builder.setItems()改为builder.setMultiChoiceItems(items[],new boolean[]{false,true,true}); //此为默认选中2、3项
自定义列表项对话框还可以使用builder.setAdapter(Adapter adapter,listener)来填充对话框中的内容
四、自定义View对话框
1.先建立layout文件,作为自定义View对话框的内容界面
2.使用View view = getLayoutInflater.inflate(R.layout.x,null) 获得你定义的界面
3.使用builder.setView(view)即可
注:对话框风格的窗口设置
在AndroidManifest.xml文件中对相应的Activity的主题设置成对话框风格的主题
1 0
- AlertDialog类的使用
- AlertDialog的使用
- android alertDialog的使用
- AlertDialog的使用(一)
- AlertDialog对话框的使用
- 关于AlertDialog的使用
- AlertDialog的使用
- AlertDialog的使用
- AlertDialog.Builder的使用
- 安卓下AlertDialog的使用
- AlertDialog的使用
- alertDialog的使用
- AlertDialog的使用
- AlertDialog的使用
- AlertDialog的简单使用
- AlertDialog对话框的使用
- 对话框AlertDialog的使用
- AlertDialog的初级使用
- Android 打开另一个app的两种方式
- 对极几何——本质矩阵E和基础矩阵F
- Guava缓存值CacheBuilder介绍
- 逻辑回归(LR)
- cmp(x,y)
- AlertDialog的使用
- minSdk(API 21) > deviceSdk(API 17)解决
- Android 导航条效果实现(四) ViewPager+自定义导航条
- android developer tiny share-20160913
- bin()函数
- 处理百万级以上的数据提高查询速度的方法
- 话说神奇的content="IE=edge,chrome=1"的meta标签内容
- 获取设备UDID等设备信息
- U3D获取ios信息:IDFA、设备型号、包名