Android Studio中对话框

//弹出对话框不需要任何控件,直接在逻辑代码中写
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
//设置对话框
dialog.setTitle("我是一个对话框的标题!");  //设置标题
dialog.setMessage("请问你需要干嘛!");  //设置信息
dialog.setIcon(R.mipmap.ic_launcher);  //设置图标


//设置按钮
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        Toast.makeText(MainActivity.this, "你点击了确定!", Toast.LENGTH_SHORT).show();
    }
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        Toast.makeText(MainActivity.this, "你点击了取消!", Toast.LENGTH_SHORT).show();
    }
});
//显示对话框
dialog.show();

2,时间对话框


        //时间对话框
        TimePickerDialog dialog=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int i, int i1) {

                Toast.makeText(MainActivity.this, i+":"+i1, Toast.LENGTH_SHORT).show();
            }
        },8,00,true);   //报红不要慌,定初始值

        //显示对话框
        dialog.show();

3,日期对话框

 //日期对话框
        DatePickerDialog dialog=new DatePickerDialog(MainActivity.this);
        dialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                Toast.makeText(MainActivity.this, i+"年"+(i1+1)+"月"+i2+"日", Toast.LENGTH_SHORT).show();

            }
        });

        //显示对话框
        dialog.show();

你可能感兴趣的:(Android集合,android-studio)