Android编程实现带有单选按钮和复选按钮的dialog功能示例
2017-09-25 10:36
776 查看
本文实例讲述了Android编程实现带有单选按钮和复选按钮的dialog。分享给大家供大家参考,具体如下:
带有单选按钮的dialog:
package example.com.myapplication; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends Activity { //声明选中项变量 private int selectedCityIndex = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //定义城市数组 final String[] arrayCity = new String[] { "杭州", "纽约", "威尼斯", "北海道" }; //实例化AlertDialog对话框 Dialog alertDialog = new AlertDialog.Builder(this) .setTitle("你最喜欢哪个地方?") //设置标题 .setIcon(R.mipmap.ic_launcher) //设置图标 //设置对话框显示一个单选List,指定默认选中项,同时设置监听事件处理 .setSingleChoiceItems(arrayCity, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { selectedCityIndex = which; //选中项的索引保存到选中项变量 } }) //添加取消按钮并增加监听处理 .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) //添加确定按钮并增加监听处理 .setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplication(), arrayCity[selectedCityIndex], Toast.LENGTH_SHORT).show(); } }) .create(); alertDialog.show(); } }
带有复选按钮的dialog代码:
package example.com.myapplication; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //定义运动数组 final String[] arraySport = new String[] { "足球", "篮球", "网球", "乒乓球" }; final boolean[] arraySportSelected = new boolean[] {false, false, false, false}; //实例化AlertDialog对话框 Dialog alertDialog = new AlertDialog.Builder(this) .setTitle("你喜欢哪些运动?") //设置标题 .setIcon(R.mipmap.ic_launcher) //设置图标 //设置对话框显示一个复选List,指定默认选中项,同时设置监听事件处理 .setMultiChoiceItems(arraySport, arraySportSelected, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { arraySportSelected[which] = isChecked; //选中项的布尔真假保存到选中项变量 } }) //添加取消按钮并增加监听处理 .setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < arraySportSelected.length; i++) { if (arraySportSelected[i] == true){ stringBuilder.append(arraySport[i] + "、"); } } Toast.makeText(getApplication(), stringBuilder.toString(), Toast.LENGTH_SHORT).show(); } }) //添加确定按钮并增加监听处理 .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .create(); alertDialog.show(); } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- Silverlight中利用ListBox特性实现单选按钮组RadioButtonList和复选按钮组CheckBoxList的功能
- (亲测) 带有单选按钮和复选按钮 的dialog+详细代码+注释
- Android编程实现分页加载ListView功能示例
- android使用Dialog实现复选功能与数据库结合综合实例
- dialog里面实现自定义样式单选按钮功
- JQuery操作单选按钮以及复选按钮示例
- Android ListView实现单选及多选等功能示例
- Android编程实现canvas绘制饼状统计图功能示例【自动适应条目数量与大小】
- Android编程实现google消息通知功能示例
- 微信小程序实现点击按钮修改view标签背景颜色功能示例【附demo源码下载】
- javascript实现复选框实现单选按钮功能
- JS基于onclick事件实现单个按钮的编辑与保存功能示例
- 自定义Dialog(图片,文字说明,单选按钮)----类ListPreference实现(2)
- 微信小程序实现点击按钮移动view标签的位置功能示例【附demo源码下载】
- Android编程实现多列显示的下拉列表框Spinner功能示例
- Android编程实现对话框Dialog背景透明功能示例
- jQuery实现的点击按钮改变样式功能示例
- Android编程实现长按Button按钮连续响应功能示例
- 在窗体上放置一个标签、一个按钮、一个fontDialog控件。标签内容改为“烟台大学”。实现功能:1)程序运行时,单击打开字体对话框按钮,可选择字体,并以所选字体作为标签字体。
- MFC功能实现单选按钮通道切换对话框有记忆功能