AlertDialog和AlertDialog.builder(二)
来源:互联网 发布:java培训四个月被骗 编辑:程序博客网 时间:2024/05/07 23:35
1、列表对话框。
列表对话框的实现可以使用两种方式,一种可以使用硬编码的方法,另外一种是使用XML配置
第一种的情况需要定义一个显示数组,然后使用AlertDialog进行展示的时候使用数组里面的内容进行显示
首先还是需要在activity_main.xml里面定义两个组件
<TextView android:id="@+id/showPhone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/show_phone"/> <Button android:id="@+id/showPhoneBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/show_phone_btn"/>
接着在MainActivity.java里面定义一个手机品牌的数组
// 喜欢的手机品牌private String[] tel = new String[] { "MOTO", "SAMSUNG", "APPLE", "NOKIA","HTC", "MEIZU" };
以下为详细实现
// 取得组件showPhone = (TextView) findViewById(R.id.showPhone);showPhoneBtn = (Button) findViewById(R.id.showPhoneBtn);// 设置监听showPhoneBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {new AlertDialog.Builder(MainActivity.this).setTitle("请选择您喜欢的品牌").setIcon(R.drawable.dialog)// 这里使用setItems(items, listener)方法.setItems(tel, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {showPhone.setText("您喜欢的手机品牌是:" + tel[which]);}}).show();}});
需要说明的是,这里使用的是AlertDialog.builder里面的setItems(CharSequence[] items, DialogInterface.OnClickListener listener)方法,里面的参数也很简单,不需要再加以赘述。
下图为运行的结果图
选择后的结果
第二种方法是进行XML配置
首先需要建立一个相关的XML文件进行存放数据color.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="color"> <item>红色</item> <item>绿色</item> <item>蓝色</item> </string-array></resources>
然后在activity_main.xml里面定义两个组件
<TextView android:id="@+id/showColor" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/show_color" /> <Button android:id="@+id/showColorBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/show_color_btn" />
那么详细的代码实现为下
// 取得组件showColor = (TextView) findViewById(R.id.showColor);showColorBtn = (Button) findViewById(R.id.showColorBtn);// 设置监听showColorBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {new AlertDialog.Builder(MainActivity.this).setTitle("请选择您喜欢的颜色").setIcon(R.drawable.dialog).setItems(R.array.color,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {// 取得资源的文件需要使用getResources()方法showColor.setText("您喜欢的颜色是:"+ getResources().getStringArray(R.array.color)[which]);}}).show();}});
运行结果如下
选择之后的结果
单选以及复选框
复选框的实现
同样的,首先先在activity_main.xml里面定义组件
<TextView android:id="@+id/showBooks" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/show_books" /> <Button android:id="@+id/showBooksBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/show_books" />
然后定义一个显示的信息列表
// 书籍列表private String[] books = new String[] { "Android开发", "thinking in java","android游戏", "SSH实战经典" };// 相对应的boolean型(这里的数组内容多少必须跟书籍列表的相同)// true表示默认选中,false表示不选中private boolean[] booksChoice = new boolean[]{false,false,false,false};
以下为详细的实现代码
// 取得组件showBooksBtn = (Button) findViewById(R.id.showBooksBtn);showBooks = (TextView) findViewById(R.id.showBooks);// 设置监听showBooksBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new AlertDialog.Builder(MainActivity.this).setTitle("请选择书籍").setIcon(R.drawable.dialog)// 复选框.setMultiChoiceItems(// 列表数组books,// 是否选中booksChoice,new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which, boolean isChecked) {// 循环是否已被选中for (int i = 0; i < books.length; i++) {// 如果选中则打印if (i == which && isChecked) {showBooks.append(books[i]+"、");}}}// 确定按钮}).setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}}).show();}});
setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)方法为设置
复选框,而setSingleChoiceItems(CharSequence[] items, int checkedItem, DialogInterface.OnClickListener listener)则为设置单选框的方法
两者的实现均差不多,则不必赘述。
而也可以在确定按钮的时候进行各种操作
下图为运行结果
选择选项之后的结果如下:
- AlertDialog和AlertDialog.builder(二)
- AlertDialog和AlertDialog.Builder
- 重访AlertDialog和AlertDialog.Builder
- [android]AlertDialog和AlertDialog.Builder
- AlertDialog.Builder 和spinner
- AlertDialog和AlertDialog.Builder?两者区别
- AlertDialog和AlertDialog.Builder?两者区别
- AlertDialog.Builder和AlertDialog之间的关系
- AlertDialog和AlertDialog.Builder两者区别
- android 开发零起步学习笔记(二十三): Android 对话框AlertDialog和AlertDialog.Builder两者的区别
- AlertDialog.Builder
- AlertDialog.Builder
- AlertDialog.Builder
- AlertDialog.Builder
- AlertDialog.Builder
- AlertDialog.Builder和Dialog分析
- AlertDialog和AlertDialog.builder、返回键退出操作
- Android 对话框AlertDialog和AlertDialog.Builder两者的区别
- VS2010项目出现“未处理System.AccessViolationException”异常
- Linux Bash Shell快速入门 在线教程
- js操作的部分常用技术
- AIDL
- button的两个方法 setImage 和 setBackgroundImage区别
- AlertDialog和AlertDialog.builder(二)
- 一起学mini2440裸机开发(七)--UART高级实验,可变参函数
- 三分算法
- VC日志类log调试信息输出
- opencv支持多种流行视频格式文件的读取
- HDU1428(BFS+DFS+记忆化搜索)
- cocos2d-x 一图多帧 取得当前帧的解决方案
- Linux静态库和动态库
- Core Data数据持久性存储基础教程