可以使用AlertDialog.Builder 才产生一个提示框
首先最简单的是弹出一个消息框:
new AlertDialog.Builder(self) .setTitle("标题") .setMessage("简单消息框") .setPositiveButton("确定", null) .show();
效果如下:
带确认和取消按钮的对话框:
new AlertDialog.Builder(self) .setTitle("是吗") .setMessage("是吗?") .setPositiveButton("是", null) .setNegativeButton("否", null) .show();
可以输入文本的对话框:
new AlertDialog.Builder(self) .setTitle("请输入文本:") .setIcon(android.R.drawable.ic_dialog_info) .setView(new EditText(self)) .setPositiveButton("确定", null) .setNegativeButton("取消", null) .show();
单选框:
new AlertDialog.Builder(self) .setTitle("请选择:") .setIcon(android.R.drawable.ic_dialog_info) .setSingleChoiceItems(new String[] {"选项1","选项2","选项3","选项4"}, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } } ) .setNegativeButton("取消", null) .show();
多选框:
new AlertDialog.Builder(self) .setTitle("多选框") .setMultiChoiceItems(new String[] {"选项1","选项2","选项3","选项4"}, null, null) .setPositiveButton("确定", null) .setNegativeButton("取消", null) .show();
列表对话框:
new AlertDialog.Builder(self) .setTitle("列表框") .setItems(new String[] {"列表项1","列表项2","列表项3"}, null) .setNegativeButton("确定", null) .show();
在对话框中显示图片:
ImageView img = new ImageView(self); img.setImageResource(R.drawable.icon); new AlertDialog.Builder(self) .setTitle("图片框") .setView(img) .setPositiveButton("确定", null) .show();
事件处理:
带一个确定的对话框
new AlertDialog.Builder(this) .setMessage("这是第二个提示") .setPositiveButton("确定", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialoginterface, int i){ //按钮事件 } }) .show();
有确定和取消的对话框
new AlertDialog.Builder(this) .setTitle("提示") .setMessage("确定退出?") .setIcon(R.drawable.quit) .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setResult(RESULT_OK);//确定按钮事件 finish(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //取消按钮事件 } }) .show();
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。