欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

对话框

程序员文章站 2022-05-29 09:57:50
...

一.常用的对话框

对话框 特殊
普通 AlertDialog.Builder()
单选 AlertDialog.Builder() setSingleChoiceItems()
多选 AlertDialog.Builder() setMultiChoiceItems()
日期 DatePickerDialog new DatePickerDialog(context,DatePickerDialog.OnDateSetListener,year,month,day);
时间 TimePickerDialog new TimePickerDialog(context, TimePickerDialog.OnTimeSetListener,时,分,是否24进制);
水平 ProgressDialog setStyle(ProgressDialog.STYLE_HORIZONTAL)
圆圈 ProgressDialog setStyle(ProgressDialog.STYLE_SPINNER)
自定义 AlertDialog.Builder() setView()

1.普通对话框

对话框

public void normal_dialog(){
        //TODO 1:构建者
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        //TODO 2:设置属性
        builder.setIcon(R.drawable.ic_launcher_background);//设置图标
        builder.setTitle("hello body");//设置标题
        builder.setMessage("我是美女陈林");//设置内容
        //设置确定按钮
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"你点击了ok",Toast.LENGTH_SHORT).show();
            }
        });
        //设置取消按钮
        builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"你点击了cancel",Toast.LENGTH_SHORT).show();
            }
        });
        //TODO 3:使用建造者创建对话框
        AlertDialog dialog = builder.create();
        //TODO 4:显示
        dialog.show();
        }

2.单选对话框

对话框


  //单选对话框
    private void single_dialog() {
        //TODO 1:构建者
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        //TODO 2:设置属性
        builder.setIcon(R.drawable.ic_launcher_background);//设置图标
        builder.setTitle("杨成果的成分是");//设置标题
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"你点击了ok",Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"你点击了cancel",Toast.LENGTH_SHORT).show();
            }
        });
        final String[] items={"男人","女人","不想"};
        //TODO 设置单选列表
        //TODO  参数一 列表  参数二 默认选中下表  参数三 事件监听
        builder.setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你选中烦恼的"+items[which], Toast.LENGTH_SHORT).show();
            }
        });
                //TODO 3:使用建造者创建对话框
        AlertDialog dialog = builder.create();
        //TODO 4:显示
        dialog.show();
        }

3.多选对话框

对话框


private void mulite_dialog() {
        final String[] items={"凤姐","小赵","如花","陈琳"};
        final boolean[] flags={true,true,true,false};
        //TODO 1:构建着
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        //TODO 2:画画
        builder.setIcon(R.drawable.ic_launcher_background);//设置图标
        builder.setTitle("杨成果的女朋友们");//设置标题
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                for(int i=0;i<flags.length;i++){
                    if(flags[i]){
                        Toast.makeText(MainActivity.this,"是"+items[i],Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        builder.setNegativeButton("cancel",new DialogInterface.OnlickListener(){
	@Override
	public void onClick(DialogInterface dialog,int which){
		Toast.makeText(MainActivity.this,"是"+items[i],Toast.LENGTH_SHORT).show();

	}
});
//TODO 设置多列表
//TODO 参数一 列表  参数二  默认选中下表  参数三事件监听
builder.setMultiChoiceItems(items,flags,new DialogInterface.OnMultiChoiceClickListener(){
	@Override
	public void onClick(DialogInterface dialog,int which,boolean isChecked){
	flags[which] = isChecked;

}
});
//TODO 3:使用建造者创建对话框
AlerDialog dialog = builder.create();
//TODO 4:显示
dialog.show();

4.自定义对话框

对话框
1.自定义布局 customer_dialog.xml

<?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:orientation="vertical"
    android:gravity="center">
    <TextView
        android:textSize="30sp"
        android:text="大美女"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
      android:id="aaa@qq.com/iv"
        android:src="@drawable/tp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

2.java代码:

 //TODO 0:获得视图
        View view=LayoutInflater.from(this).inflate(R.layout.customer_dialog,null);
        ImageView imageView=view.findViewById(R.id.iv);
        imageView.setOnclickListener(new OnclickListsner(){
        //点击图片就可以吐司或者关闭对话框了
        });
        //TODO 1:构建着
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
       
        //设置自定义布局
        builder.setView(view);
               //TODO 3:使用建造者创建对话框
        AlertDialog dialog = builder.create();
        //TODO 4:显示
        dialog.show();

5.水平进度条对话框

对话框

//水平进度条
    private void hor_progress_dialog() {
        //TODO 1:实例化对象
        final ProgressDialog dialog=new ProgressDialog(this);
        //TODO 2:设置属性
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置水平央视
        dialog.setMax(100);
        dialog.setMessage("正在下载");
        //TODO 3:显示
        dialog.show();
        //TODO 4:模拟定时器
        final Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            int progress=0;
            @Override
            public void run() {
                if(progress==100){
                    dialog.dismiss();//消失
                    timer.cancel();
                }
                dialog.setProgress(progress+=20);
            }
        },0,1000);//0秒后执行,每隔1000毫秒执行一次
    }

6.圆形进度条对话框

对话框

private void hor_progress_dialog() {
        //TODO 1:实例化对象
        final ProgressDialog dialog=new ProgressDialog(this);
        //TODO 2:设置属性
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置圆圈
        dialog.setMax(100);
        dialog.setMessage("正在下载");
        //TODO 3:显示
        dialog.show();
        //TODO 4:模拟定时器
        final Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            int progress=0;
            @Override
            public void run() {
                if(progress==100){
                    dialog.dismiss();//消失
                    timer.cancel();
                }
                dialog.setProgress(progress+=20);
            }
        },0,1000);//0秒后执行,每隔1000毫秒执行一次
    }

7.日期选择对话框

对话框

     //日期选择对话框DatePickerDialog
    private void date_dialog() {
        /**
         *
         *
         * @param context 上下文
         * @param listener 监听器
         * @param year 默认的年
         * @param month 默认的月
         * @param dayOfMonth 默认的日
         */
        Calendar calendar=Calendar.getInstance();//日历对象
        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            Toast.makeText(MainActivty.this,yera+"-"+(month+1)+"-"+dayofMonth,Toast.LENGTH_SHORT).show();
            }
        },calndar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
        }

8.时间选择对话框

对话框

//时间对话框:TimePickerDialog
   private void time_dialog() {
       /**
        * @param context 上下文
        * @param listener 监听器
        * @param hourOfDay 默认的时
        * @param minute 默认的分钟
        * @param is24HourView 是否采用24时禁止
        */
       Calendar calendar=Calendar.getInstance();//日历对象
       //TODO 1:实例化对象 TODO 2:show
       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();
           }
       },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();
   }

二.自定义对话框

1.思路:

1.自定义继承Dialog
2.重写Dialog的方法并添加需要的功能
3.在Activity中实例化自定义类的对象

2.代码

对话框