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

发布时间:2022-04-07 17:11:30 作者:iii
来源:亿速云 阅读:122

这篇文章主要介绍“Android编程怎么实现带有单选按钮和复选按钮的dialog功能”,在日常操作中,相信很多人在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, 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编程怎么实现带有单选按钮和复选按钮的dialog功能”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 单选按钮RadioGroup与复选框CheckBox
  2. 表单样式美化--复选框和单选按钮

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android dialog

上一篇:Android开发怎么实现应用层面屏蔽状态栏

下一篇:Android怎么实现短信验证码倒计时功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》