Android中几种常用的话框

时间:2023-03-09 20:21:22
Android中几种常用的话框
 1.普通对话框:

 Builder alert=new AlertDialog.Builder(MainActivity.this);
alert.setTitle("提示");
alert.setMessage("普通对话框");
alert.setPositiveButton("确定", null);
alert.show(); 2.自定义对话框: LayoutInflater layout=LayoutInflater.from(MainActivity.this);//设置布局文件的过滤是从MainActivity中进行的
View view=layout.inflate(R.layout.custmer, null);//建一个View 对象用来存放自定义的布局文件,这里的R.layout.custmer就是自定义的文件布局
Builder customer=new AlertDialog.Builder(MainActivity.this);
customer.setView(view);//通过此方法可以将自定义的布局加载到对话框中
customer.setTitle("提示");
customer.setMessage("自定义对话框");
customer.setPositiveButton("确定", null).create();
customer.show(); 3.类似单选按钮形式的对话框: Builder customer=new AlertDialog.Builder(MainActivity.this);
customer.setTitle("提示");
customer.setMessage("自定义对话框");
customer.setSingleChoiceItems(new String[]{"a","b","c"}, 0, new OnClickListener(){
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "你选择了"+arg1+"项", Toast.LENGTH_LONG).show();
} });
customer.setPositiveButton("确定", null).create();
customer.show(); 4.多选类型的对话框
Builder customer=new AlertDialog.Builder(MainActivity.this);
customer.setTitle("提示");
customer.setMessage("自定义对话框");
设置为多选对话框,前面的是选项,会面对应的是,是否处于选中状态
customer.setMultiChoiceItems(new String[]{"a","b","c","d"}, new boolean[]{true,false,false,false}, new OnMultiChoiceClickListener(){
public void onClick(DialogInterface arg0, int arg1, boolean arg2)
{
Toast.makeText(MainActivity.this, "你选择了"+arg1+"项", Toast.LENGTH_LONG).show();
} });
customer.show(); 5.进度对话框:
1)。ProgressDialog progress=ProgressDialog.show(MainActivity.this, "安装进度", "正在安装");
52 第二个参数是:提示的标题;第三个参数是:messge信息
53
2)。也可以这样写:
ProgressDialog dialog=new ProgressDialog(MainActivity.this);
dialog.setButton2("确定", new OnClickListener()){ @Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub } });
dialog.setTitle("安装进度");
dialog.setMessage("正在安装");
dialog.setIndeterminate(false);//设置进度为不明确类型的
dialog.setSecondaryProgress(progress);//设置进度值的二次进度
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)//设置为水平进队条
dialog.show(); 6.时间选择对话框:
calendar=Calendar.getInstance();
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH);
day=calendar.get(Calendar.DAY_OF_MONTH);
minute=calendar.get(Calendar.MINUTE);
hour=calendar.get(Calendar.HOUR);
new TimePickerDialog(this, new OnTimeSetListener(){ @Override
public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
calendar.set(year,month,day,hour,minute);
}}, hour, minute, false).show(); 7.日期选择对话框:
new DatePickerDialog(this,new OnDateSetListener(){
public void onDateSet(DatePicker arg0, int arg1, int arg2,
int arg3) {
calendar.set(year,month,day);
} },year,month,day).show();

相关文章