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

android对话框

程序员文章站 2022-03-09 22:50:45
...

Dialog:弹出框

分类有三种:1.普通对话框    2.列表对话框    3.适配器对话框

第一种:普通弹出框:

 

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("我是普通弹出框");
builder.setMessage("正在玩命加载中");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,"我是确定按钮哦",Toast.LENGTH_SHORT).show();
    }
});
builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,"哼!!!",Toast.LENGTH_SHORT).show();
    }
});
builder.setNegativeButton("等待", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,"等待一会哦",Toast.LENGTH_SHORT).show();
    }
});
AlertDialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(false);//设置弹出窗口时点击旁边,弹窗不会关闭
dialog.show();
builder.show();

效果图:

android对话框

 

第二种:列表选择弹出框

 

 

AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setTitle("列表选择");
final String[] items={"啦啦","嘻嘻嘻","嘿嘿嘿"};
builder1.setItems(items, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,items[which],Toast.LENGTH_SHORT).show();
    }
});
AlertDialog dialog1 = builder1.create();
dialog1.setCanceledOnTouchOutside(false);
dialog1.show();

效果图:

android对话框

 

第三种:列表适配器:

 

AlertDialog.Builder adapterBuilder = new AlertDialog.Builder(this);
adapterBuilder.setTitle("列表适配器");
final String[] list={"啦啦啦","呵呵呵","嘿嘿嘿","嘻嘻嘻","哈哈哈"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item,list);
adapterBuilder.setAdapter(adapter, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,list[which],Toast.LENGTH_SHORT).show();
    }
});
AlertDialog dialog2 = adapterBuilder.create();
dialog2.setCanceledOnTouchOutside(false);
dialog2.show();

第四种:单选按钮:

 

AlertDialog.Builder radio = new AlertDialog.Builder(this);
radio.setTitle("单选按钮");
final String[] sex = {"男","女"};
radio.setSingleChoiceItems(sex, 0, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,sex[which],Toast.LENGTH_SHORT).show();
    }
});
AlertDialog dialog3 = radio.create();
dialog3.setCanceledOnTouchOutside(false);
dialog3.show();

效果图:

android对话框

 

第五种:多选弹出框:

AlertDialog.Builder check = new AlertDialog.Builder(this);
final String[] hobby = {"看电视","学习","上课","吃饭","睡觉"};
final boolean[] checked = {true,false,true,false,true};
check.setTitle("多选弹出框");
check.setMultiChoiceItems(hobby, checked, new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        checked[which] = isChecked;
    }
});
AlertDialog dialog4 = check.create();
dialog4.setCanceledOnTouchOutside(false);
dialog4.show();

效果图:

android对话框

 

第六种:自定义弹出框:

 

AlertDialog.Builder custom = new AlertDialog.Builder(this);
View custonView = LayoutInflater.from(this).inflate(R.layout.custom_layout,null);
final EditText checket = custonView.findViewById(R.id.et);
Button btn = custonView.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String check = checket.getText().toString();
        Toast.makeText(MainActivity.this,check,Toast.LENGTH_SHORT).show();
    }
});
custom.setView(custonView);
custom.show();

第七种:时间选择弹出框:

TimePickerDialog timePickerDialog = 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();
    }
},5,20,true);
timePickerDialog.show();
效果图:

android对话框

 

第八种:日期选择弹出框:

 

Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
final int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = 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)+"-"+day,Toast.LENGTH_SHORT).show();
    }
},year,month,day);
datePickerDialog.show();
效果图:

android对话框

第九种:进度弹出框:

 

ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("进度弹出框");
progressDialog.setMessage("正在拼了老命加载中...");
progressDialog.setIcon(R.mipmap.ic_launcher);
progressDialog.setMax(100);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
progressDialog.setProgress(20);

效果图:

android对话框

 

 

 

 

 

 

 

 

相关标签: Dialog