首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android:AlertDialog.Builder创造常见对话框

2012-08-30 
Android:AlertDialog.Builder创建常见对话框1.提示先来说一下“提示”(提示几秒后就会消失),也许你只是想提

Android:AlertDialog.Builder创建常见对话框

1.提示
先来说一下“提示”(提示几秒后就会消失),也许你只是想提示一下,不需要对方反馈,甚至不需要对方一定看见,也许你需要的是这个:

Toast.makeText(QueryCarInfoActivity.this, "上传数据成功", Toast.LENGTH_SHORT).show(); //从资源文件string.xml 里面取提示信息Toast.makeText(QueryCarInfoActivity.this, getString(R.string.hello), Toast.LENGTH_SHORT).show();

参数:
Toast.LENGTH_SHORT: Show the view or text notification for a short period of time
Toast.LENGTH_LONG: Show the view or text notification for a long period of time

2.使用new AlertDialog.Builder(context)创建一个对话框
(1)确定框
点击时,弹出一个消息框

/** * 确定框 */private void dialogShowOK() {new AlertDialog.Builder(this).setTitle("确定框").setMessage("简单消息框").setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {//按钮事件}}).show();}

Android:AlertDialog.Builder创造常见对话框

(2)确定取消框
确定还是取消,it is a problem,当你做某项操作时,弹出一个提示,来确保操作无误。如删除文件时,提示“确认删除文件”;当按返回按钮时,提示“确认退出”等。

/** * 确定取消框 */private void dialogShowOKCancel() {new AlertDialog.Builder(this).setTitle("确定取消框").setIcon(R.drawable.down).setMessage("确定退出吗").setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {//确定按钮事件setResult(RESULT_OK);//dialog.dismiss();finish();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {//取消按钮事件//dialog.dismiss();}}).show();}/** * 按返回键提示是否退出 */@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {   dialogShowOKCancel();}return false;}

Android:AlertDialog.Builder创造常见对话框

(3)多按钮
改变了对话框的图表,添加了三个按钮

/** * 多按钮 */private void dialogShowButton() {new AlertDialog.Builder(this).setIcon(R.drawable.down).setTitle("文件").setMessage("请选择您的操作:").setPositiveButton("保存", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(TestDialogActivity.this, "保存文件", Toast.LENGTH_LONG).show();}}).setNegativeButton("返回", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(TestDialogActivity.this, "返回继续编辑文件", Toast.LENGTH_LONG).show();}}).setNeutralButton("清空", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(TestDialogActivity.this, "清空文件", Toast.LENGTH_LONG).show();}}).show();}

Android:AlertDialog.Builder创造常见对话框

(4)输入框
通过setView()方法,为对话框传入了一个文本编辑框,当然,你可以传入任意视图对象,如图片框。

/** * 输入框 */private void dialogShowInput() {new AlertDialog.Builder(this).setTitle("请输入").setIcon(R.drawable.info).setView(new EditText(this)).setPositiveButton("确定", null).setNegativeButton("取消", null).show();}


上面的两个null参数,这里要放的其实是这两个按钮点击的监听程序,由于我们这里不需要监听这些动作,所以传入null值简单忽略掉,但是实际开发的时候一般都是需要传入监听器的,用来响应用户的操作。

Android:AlertDialog.Builder创造常见对话框

(5)图片框

/** * 图片框 */private void dialogShowImage() {ImageView imgView = new ImageView(this);imgView.setImageResource(R.drawable.image);new AlertDialog.Builder(this).setTitle("图片框").setView(imgView).setPositiveButton("确定", null).show();}

Android:AlertDialog.Builder创造常见对话框

(6)单选框

/** * 单选框 */private void dialogShowRadio() {String[] s = new String[] {"选项1", "选项2", "选项3", "选项4"};new AlertDialog.Builder(this).setTitle("单选框").setIcon(R.drawable.down).setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubdialog.dismiss();}}).setNegativeButton("取消", null).show();}

Android:AlertDialog.Builder创造常见对话框

(7)多选框

/** * 多选框 */private void dialogShowCheck() {String[] s = new String[] {"选项1", "选项2", "选项3", "选项4"};new AlertDialog.Builder(this).setTitle("多选框").setIcon(R.drawable.down).setMultiChoiceItems(s, null, null).setPositiveButton("确定", null).setNegativeButton("取消", null).show();}

Android:AlertDialog.Builder创造常见对话框

(8)列表框

/** * 列表框 */private void dialogShowList() {String[] s = new String[] {"列表1", "列表2", "列表3"};new AlertDialog.Builder(this).setTitle("列表框").setIcon(R.drawable.down).setItems(s, null).setPositiveButton("确定", null).show();}

Android:AlertDialog.Builder创造常见对话框

1楼lfmilaoshi昨天 22:27
一看就是在凑合博客。。。米老师

热点排行
Bad Request.