Android开发学习:[8]AlertDialog

 时间:2026-02-16 03:41:41

1、首先我们在界面布局上面布局四个按钮

Android开发学习:[8]AlertDialog

2、然后我们在代码中关联这四个按钮:

Button button3=(Button)findViewById(R.id.button3);

        Button button4=(Button)findViewById(R.id.button4);

        Button button5=(Button)findViewById(R.id.button5);

        Button button6=(Button)findViewById(R.id.button6);

Android开发学习:[8]AlertDialog

3、然后我们敲入AlterDialog代码:

  //获取带单选列表的对话框按钮

        button5.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                final String[] iterms=new String[]{"standard","silent","meeting","outdoor","offline"};

                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);

                //builder.setIcon(R.drawable.desert);

                builder.setTitle("Choose your favable contextual model:");

                builder.setSingleChoiceItems(iterms, 0, new DialogInterface.OnClickListener() {

                    @Override

                    public void onClick(DialogInterface dialog, int which) {

                        Toast.makeText(getApplicationContext(), "You choosed " + iterms[which], Toast.LENGTH_LONG).show();

                    }

                });

                builder.create().show();//创建对话框并显示

            }

        });

Android开发学习:[8]AlertDialog

4、现在我们点击运行按钮,运行此程序,界面如下图所示

Android开发学习:[8]AlertDialog

5、然后我们点击Button5会出现单选列表

Android开发学习:[8]AlertDialog

6、点击任意一个回出现Toast显示你选择了哪个列表项

Android开发学习:[8]AlertDialog

7、后面将继续介绍后续的三个AlterDialog欢迎继续关注。

  • Android 中dialog的使用
  • Android开发之深入理解AlertDialog使用说明
  • Android学习笔记:[4]AlertDialog警告对话框
  • 【Android】AlertDialog禁止返回键
  • Android开发之对话框创建的几种方式总结
  • 热门搜索
    清明手抄报图片大全 音乐手抄报图片 红五月手抄报 以环保为主题的手抄报 安全手抄报版面设计图 抗震救灾手抄报 清明节的手抄报大全 小学生二年级手抄报 70周年手抄报内容文字 廉洁教育手抄报