android其他类型的对话框
来源:互联网 发布:二次元网页源码 编辑:程序博客网 时间:2024/05/29 06:58
上次已经说过那种比较简单的alertDialog,这次我在上次的基础上加大了一点难度,对话框的主体内容使用列表、单选框、复选框。以及自定义对话框的使用。这次的内容可能在自定义对话框上比较复杂一些。其他几种都是比较简单的
1.列表类型的对话框
final CharSequence[] items={"海南","浙江","武汉"}; AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("你最想去什么地方"); builder.setItems(items,new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), items[item],Toast.LENGTH_LONG).show(); } }); //创建对话框 AlertDialog alert = builder.create(); //把对话框显示在手机屏幕上 alert.show();下面是运行效果图
2.单选按钮类型的对话框
builder.setSingleChoiceItems(items,-1,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int item) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), items[item],Toast.LENGTH_LONG).show();}});
这里的-1表示当选按钮全部是未选中状态,0则表示第一个是选中状态
3.复选框类型
boolean[] b={false,false,true};
builder.setMultiChoiceItems(items,b,new DialogInterface.OnMultiChoiceClickListener() { @Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stubStringBuilder str=new StringBuilder();if(isChecked){Toast.makeText(getApplicationContext(), str.append(items[which]),Toast.LENGTH_LONG).show();}}});
4.自定义对话框
这里布局还是写在xml文件里,下面是我的xml文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/layout_root" android:orientation="horizontal"android:layout_width="fill_parent" android:layout_height="fill_parent"android:padding="10dp"><ImageView android:id="@+id/image" android:layout_width="wrap_content"android:layout_height="fill_parent" android:layout_marginRight="10dp" /><TextView android:id="@+id/text" android:layout_width="wrap_content"android:layout_height="fill_parent" android:textColor="#FFF" /></LinearLayout>
我把R文件也贴出来
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */package org.lxh.activity;public final class R { public static final class attr { } public static final class drawable { public static final int android=0x7f020000; public static final int icon=0x7f020001; } public static final class id { public static final int image=0x7f050001; public static final int layout_root=0x7f050000; public static final int text=0x7f050002; } public static final class layout { public static final int custom_dialog=0x7f030000; public static final int main=0x7f030001; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; public static final int text=0x7f040002; }}
下面就是展开布局的代码了,大家应该可以看懂
//使用LayoutInflater展开布局 LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); //取得布局对象 View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello, this is a custom dialog!"); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.android); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(layout); AlertDialog alertDialog = builder.create(); alertDialog.setTitle("我的自定义对话框"); alertDialog.show();
很简单吧,暂时就到这里吧。
- android其他类型的对话框
- RCP-其他类型对话框
- Android 各种类型的对话框教程详解
- 其他类型的标签
- S2010 怎么导入其他工程的对话框
- Android 绑定类型服务---其他注意事项
- Android 绑定类型服务---其他注意事项
- Android--Alertdialog对话框,设置点击其他位置不消失
- 让非模态对话框像模态对话框一样,没有处理完就不能选择其他的对话框
- Junit4单元测试 (三) 其他类型的Runner和其他功能
- CString 与 其他类型的转换
- CString 与 其他类型的转换收藏
- Cstring和其他类型变量的转换
- QString 到其他类型的转化
- JsonObject 和其他类型数据的转换
- cscope中添加其他类型的文件
- CString和其他类型之间的转换规则:
- js其他类型到string的转换
- 使用hibernate工具类将对象模型生成关系模型
- java获取当前日期时间和本周日期时间
- android根文件夹结构
- websphere集群环境配置oracle数据源Test Connection失败问题解决
- Java中运算精度的问题(double与BigDecimal)
- android其他类型的对话框
- Hibernate之ID主键生成策略
- C#多线程简述(三)
- Java高新技术第一天学习日记
- 【JAVA核心技术】本地方法
- httpSession生命周期
- 学习随笔
- TCP/IP传输层,你懂多少?
- Linux firmware 加载 (2012-04-01 15:46)