Android编程实现带有单选按钮和复选按钮的dialog功能示例

Android编程实现带有单选按钮和复选按钮的dialog功能示例,第1张

概述本文实例讲述了Android编程实现带有单选按钮和复选按钮的dialog。分享给大家供大家参考,具体如下:

本文实例讲述了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,new DialogInterface.OnClickListener() {          @OverrIDe          public voID onClick(DialogInterface dialog,int which) {            selectedCityIndex = which;        //选中项的索引保存到选中项变量          }        })        //添加取消按钮并增加监听处理        .setNegativebutton("取消",int which) {            // Todo auto-generated method stub          }        })        //添加确定按钮并增加监听处理        .setPositivebutton("确认",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};    //实例化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("确认",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("取消",int which) {            // Todo auto-generated method stub          }        })        .create();    alertDialog.show();  }}

更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家AndroID程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Android编程实现带有单选按钮和复选按钮的dialog功能示例全部内容,希望文章能够帮你解决Android编程实现带有单选按钮和复选按钮的dialog功能示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144411.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存