在android的编程中,对话框的用处是非常多的,然而,有时对对话框的需求也是不一样的,那么现在,就总结对话框的种类,以及事件的处理形势。
1、简单的对话框
public void showDialog(){ Dialog dialog = new AlertDialog.Builder(this) .setTitle("标题") .setMessage("消息") .setPositiveButton("好", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setNegativeButton("不好", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).create(); dialog.show(); }
2、带编辑框的对话框
public void showDialog(){ EditText et = new EditText(this);// 单独出来,方便取用户输入的数据。 Dialog dialog = new AlertDialog.Builder(this) .setTitle("请输入") .setIcon(android.R.drawable.ic_dialog_info) .setView(et) .setPositiveButton("确定", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { } }) .setNegativeButton("取消", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { }}) .create(); dialog.show(); }
3、含单选钮的对话框
public void showDialog(){ final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ; // 单独列出来方便之后取数据 Dialog dialog = new AlertDialog.Builder(this) .setTitle("单选框,请选择") .setSingleChoiceItems(str, 3, new DialogInterface.OnClickListener() { // 3 代表默认选中str的index=3的那个单选框 @Override public void onClick(DialogInterface dialog, int which) { Log.e("DIYDialogActivity", "showDialog->you select " + str[which] ) ; } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .create(); dialog.show(); }
4、含多选框的对话框
public void showDialog(){ final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;// 单独列出来方便之后取数据 boolean[] bstr = new boolean[]{true,false,false,false,false,false,false}; Dialog dialog = new AlertDialog.Builder(this) .setTitle("多选框,请选择") .setMultiChoiceItems(str, bstr, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Log.e("DIYDialogActivity", "showDialog->" + "you select " + str[which]+ "vaule = " +new Boolean(isChecked).toString() ) ; } }) .setPositiveButton("确定", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { } }).create(); dialog.show(); }
5、含列表的对话框
public void showDialog(){ final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ; // 单独列出来方便之后取数据 Dialog dialog = new AlertDialog.Builder(this) .setTitle("列表框,供选择") .setItems(str, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.e("DIYDialogActivity", "showDialog---------------------you select " + which); } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .create(); dialog.show(); }
6、自定义的对话框
public void showDialog(){ LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.dialog_showmsg, (ViewGroup) findViewById(R.id.view_dialog)); // 可指定对话框的背景色彩那些 Dialog dialog = new AlertDialog.Builder(this) .setView(layout) .create(); // 设置对话框的出现位置,借助于window对象 Window win = dialog.getWindow(); //win.setGravity(Gravity.CENTER); //win.setGravity(Gravity.BOTTOM); //win.setGravity(Gravity.TOP); win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);// 弹出对话框时,底部窗体,不变暗。 WindowManager.LayoutParams lp = win.getAttributes(); lp.x = -200;// x=0,y=0时,显示位置是屏幕中心。 lp.y = 0 ; lp.alpha = 0.6f;// 对话框的透明度 win.setAttributes(lp); Button but = (Button) layout.findViewById(R.id.but_positive); but.requestFocus(); dialog.show(); }