Android第二十天Dialog
弹出框的分类:
普通弹出框:
代码:
//普通的弹出框 // 构造方法 创建一个Builder 对象 参数是 上下文对象 AlertDialog.Builder normalBuilder = new AlertDialog.Builder(this); //使用Builder对象来赋予弹出框各种各样的属性 normalBuilder.setTitle("警告");//提示标题信息 normalBuilder.setMessage("正在加载中");//设置提示信息 normalBuilder.setIcon(R.mipmap.ic_launcher);//设置 提示图片 //给弹出框添加一个确定按钮 normalBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "点击了确定按钮", Toast.LENGTH_SHORT).show(); } }); normalBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show(); } }); normalBuilder.setNeutralButton("中立", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "点击了中立按钮", Toast.LENGTH_SHORT).show(); } }); // normalBuilder.show();//将对话框展示出来 //Builder 的create方法返回值是一个Dialog对象 AlertDialog dialog = normalBuilder.create(); //点击弹出框外部区域的时候弹出框是否消失 false 不消失(默认值是 true) dialog.setCanceledOnTouchOutside(false); // 展示 dialog.show();
列表条目弹出框:
代码:
//展示列表的 弹出框 AlertDialog.Builder itemBuilder = new AlertDialog.Builder(this); itemBuilder.setTitle("请选择"); final String[] items = {"王智勇", "关机", "气不气人", "看看柴伟", "张磊", "表现多好", "是不"}; /** *设置条目 列表 *参数一:string类型数组 表示 展示的数据源 *参数二:监听器 点击事件 */ itemBuilder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //which 表示 点击的是第几个 条目 Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show(); //当点击后,会默认的将弹出框关闭掉 } }); //展示出来 itemBuilder.show();
适配器弹出框:
代码:
AlertDialog.Builder adapterBuilder = new AlertDialog.Builder(this); adapterBuilder.setTitle("请选择"); final String[] list = {"王智勇", "关机", "气不气人", "看看柴伟", "张磊", "表现多好", "是不"}; //实例化一个 适配器 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list); /** * 设置适配器 * 参数一: 适配器对象 * 参数二: 监听器对象 */ adapterBuilder.setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //which 表示点击了条目中的第几个 Toast.makeText(MainActivity.this, list[which], Toast.LENGTH_SHORT).show(); } }); adapterBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show(); } }); //展示出来 adapterBuilder.show();
单选弹出框:
代码:
//单选弹出框 AlertDialog.Builder singleBuilder = new AlertDialog.Builder(this); singleBuilder.setTitle("请选择:"); final String[] sexs = {"男", "女", "其他"}; /** * 设置单选 * 参数一 : 数据源 列表的数据源 * 参数二: 默认选中的项 * 参数三:监听器 最终选择了那个 */ singleBuilder.setSingleChoiceItems(sexs, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, sexs[which], Toast.LENGTH_SHORT).show(); } }); singleBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show(); } }); //展示 singleBuilder.show();
多选弹出框:
代码:
private String[] hobby = {"打游戏", "睡觉", "吃饭", "看电视", "唱歌", "运动", "敲代码"}; private boolean[] checked = {true, false, true, false, true, false, true};
//多选 弹出框 AlertDialog.Builder moreBuilder = new AlertDialog.Builder(this); moreBuilder.setTitle("请选择:"); /** * 设置多选 * 参数一:表示多选数据源 * 参数二:默认多选项 * 参数三: 监听器 * */ moreBuilder.setMultiChoiceItems(hobby, checked, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { //which 表示的是 当前点击的是第几个,isChecked表示这个是 选中了 还是取消了 checked[which] = isChecked; } }); moreBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < checked.length; i++) { if (checked[i]) { buffer.append(hobby[i] + "\n"); } } Toast.makeText(MainActivity.this, buffer.toString(), Toast.LENGTH_SHORT).show(); } }); moreBuilder.show();
自定义弹出框:
代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <ImageView android:layout_width="250dp" android:layout_height="200dp" android:layout_marginTop="10dp" android:src="@mipmap/tupian" /> <EditText android:id="@+id/checkCode_bt" android:layout_width="match_parent" android:layout_margin="10dp" android:hint="请输入验证码" android:layout_height="wrap_content" /> <Button android:id="@+id/ok_bt" android:layout_width="match_parent" android:layout_margin="10dp" android:text="确定" android:layout_height="wrap_content" /> </LinearLayout>
//自定义对话框 AlertDialog.Builder customDialog = new AlertDialog.Builder(this); //初始化一个自定义视图 View customView = LayoutInflater.from(this).inflate(R.layout.custom_layout, null); //实例化自定义视图内部的子视图 final EditText checkCodeEt = (EditText) customView.findViewById(R.id.checkCode_bt); Button okBt = (Button) customView.findViewById(R.id.ok_bt); //给按钮设置监听器 okBt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取输入的验证码 String checkCode = checkCodeEt.getText().toString(); //Toast展示出来 Toast.makeText(MainActivity.this, checkCode, Toast.LENGTH_SHORT).show(); } }); //自定义对话框 设置自定义视图的方式 customDialog.setView(customView); //展示 customDialog.show();
时间选择弹出框:
代码:
//时间对话框 /** * 构造方法: * 参数一:上下文对象 * 参数二:监听器 当滚动时间的时候 监听器会执行重写方法 * 参数三:时间弹出框默认的时间显示 * 参数四: 时间弹出框默认的分钟显示 * 参数五: 时间弹出框是否是 24小时制 * */ TimePickerDialog timeDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(MainActivity.this, hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show(); } }, 11, 26, true); timeDialog.show();
日期选择弹出框:
代码:
//日期选择弹出框 //获取当前年月日 Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); /** * 参数一:上下文对象 * 参数二: 监听器 * 参数三:默认显示的年 * 参数四:默认显示的月 * 参数五:默认显示的日 */ DatePickerDialog dateDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { Toast.makeText(MainActivity.this, year + "/" + (month+1) + "/" + dayOfMonth, Toast.LENGTH_SHORT).show(); } }, year, month, day); dateDialog.show();
进度弹出框:
代码:
//进度条 ProgressDialog progressDialog = new ProgressDialog(this); //设置标题 progressDialog.setTitle("提示"); //设置信息 progressDialog.setMessage("正在玩命儿加载中"); //设置图标 progressDialog.setIcon(R.mipmap.ic_launcher); progressDialog.setMax(100);//设置进度条的最大值 //设置进度条展示的样式是 水平的(默认是转圈的没有刻度的) progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setSecondaryProgress(40);//设置第二进度是四十(缓冲进度) progressDialog.show();//展示数据 progressDialog.setProgress(20);//设置进度是20