1.提示
先来说一下“提示”(提示几秒后就会消失),也许你只是想提示一下,不需要对方反馈,甚至不需要对方一定看见,也许你需要的是这个:
[java] view plain copy print ?
- Toast.makeText(QueryCarInfoActivity.this, "上传数据成功", Toast.LENGTH_SHORT).show();
-
- Toast.makeText(QueryCarInfoActivity.this, getString(R.string.hello), Toast.LENGTH_SHORT).show();
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)确定框
点击时,弹出一个消息框
[java] view plain copy print ?
-
-
-
- private void dialogShowOK() {
- new AlertDialog.Builder(this)
- .setTitle("确定框")
- .setMessage("简单消息框")
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
-
- public void onClick(DialogInterface dialog, int which) {
-
- }
- })
- .show();
- }
/**
* 确定框
*/
private void dialogShowOK() {
new AlertDialog.Builder(this)
.setTitle("确定框")
.setMessage("简单消息框")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//按钮事件
}
})
.show();
}
(2)确定取消框
确定还是取消,it is a problem,当你做某项操作时,弹出一个提示,来确保操作无误。如删除文件时,提示“确认删除文件”;当按返回按钮时,提示“确认退出”等。
[java] view plain copy print ?
-
-
-
- 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);
-
- finish();
- }
- })
- .setNegativeButton("取消", new DialogInterface.OnClickListener() {
-
- public void onClick(DialogInterface dialog, int which) {
-
-
- }
- })
- .show();
- }
-
-
-
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
- dialogShowOKCancel();
- }
- return false;
- }
/**
* 确定取消框
*/
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();
}
/**
* 按返回键提示是否退出
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
dialogShowOKCancel();
}
return false;
}
(3)多按钮
改变了对话框的图表,添加了三个按钮
[java] view plain copy print ?
-
-
-
- private void dialogShowButton() {
- new AlertDialog.Builder(this)
- .setIcon(R.drawable.down)
- .setTitle("文件")
- .setMessage("请选择您的操作:")
- .setPositiveButton("保存", new DialogInterface.OnClickListener() {
-
- public void onClick(DialogInterface dialog, int which) {
-
- Toast.makeText(TestDialogActivity.this, "保存文件", Toast.LENGTH_LONG).show();
- }
- })
- .setNegativeButton("返回", new DialogInterface.OnClickListener() {
-
- public void onClick(DialogInterface dialog, int which) {
-
- Toast.makeText(TestDialogActivity.this, "返回继续编辑文件", Toast.LENGTH_LONG).show();
- }
- })
- .setNeutralButton("清空", new DialogInterface.OnClickListener() {
-
- public void onClick(DialogInterface dialog, int which) {
-
- Toast.makeText(TestDialogActivity.this, "清空文件", Toast.LENGTH_LONG).show();
- }
- })
- .show();
- }
/**
* 多按钮
*/
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 stub
Toast.makeText(TestDialogActivity.this, "保存文件", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("返回", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(TestDialogActivity.this, "返回继续编辑文件", Toast.LENGTH_LONG).show();
}
})
.setNeutralButton("清空", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(TestDialogActivity.this, "清空文件", Toast.LENGTH_LONG).show();
}
})
.show();
}
(4)输入框
通过setView()方法,为对话框传入了一个文本编辑框,当然,你可以传入任意视图对象,如图片框。
[java] view plain copy print ?
-
-
-
- private void dialogShowInput() {
- new AlertDialog.Builder(this)
- .setTitle("请输入")
- .setIcon(R.drawable.info)
- .setView(new EditText(this))
- .setPositiveButton("确定", null)
- .setNegativeButton("取消", null)
- .show();
- }
/**
* 输入框
*/
private void dialogShowInput() {
new AlertDialog.Builder(this)
.setTitle("请输入")
.setIcon(R.drawable.info)
.setView(new EditText(this))
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show();
}
上面的两个null参数,这里要放的其实是这两个按钮点击的监听程序,由于我们这里不需要监听这些动作,所以传入null值简单忽略掉,但是实际开发的时候一般都是需要传入监听器的,用来响应用户的操作。
(5)图片框
[java] view plain copy print ?
-
-
-
- private void dialogShowImage() {
- ImageView imgView = new ImageView(this);
- imgView.setImageResource(R.drawable.image);
- new AlertDialog.Builder(this)
- .setTitle("图片框")
- .setView(imgView)
- .setPositiveButton("确定", null)
- .show();
- }
/**
* 图片框
*/
private void dialogShowImage() {
ImageView imgView = new ImageView(this);
imgView.setImageResource(R.drawable.image);
new AlertDialog.Builder(this)
.setTitle("图片框")
.setView(imgView)
.setPositiveButton("确定", null)
.show();
}
(6)单选框
[java] view plain copy print ?
-
-
-
- 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) {
-
- dialog.dismiss();
- }
- })
- .setNegativeButton("取消", null)
- .show();
- }
/**
* 单选框
*/
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 stub
dialog.dismiss();
}
})
.setNegativeButton("取消", null)
.show();
}
(7)多选框
[java] view plain copy print ?
-
-
-
- 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();
- }
/**
* 多选框
*/
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();
}
(8)列表框
[java] view plain copy print ?
-
-
-
- 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();
- }
/**
* 列表框
*/
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();
}