public voID bt1(VIEw vIEw) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("标题"); builder.setMessage("内容"); builder.setPositivebutton("确定", 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 alertDialog = builder.create(); alertDialog.show(); }
2.单选对话框@H_502_68@
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.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"你点击了cancel",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:使用建造者创建对话框 AlertDialog dialog = builder.create(); //Todo 4:显示 dialog.show(); }
4.自定义对话框(基础)1.Java代码:<?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="+@ID/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.日期选择对话框 //日期选择对话框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(MainActivity.this, year+"-"+(month+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show(); } },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show(); }
6.时间选择对话框 //时间对话框: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(); }
总结 以上是内存溢出为你收集整理的Android对话框全部内容,希望文章能够帮你解决Android对话框所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)