Android中有四种比较常用的对话框
AlertDialog:对话框的父类;ProgressDialog:进度条对话框;Date/TimePickerDialog:日期/实践选择对话框
一.普通的AlertDialog:
1.AlertDialog的结构:
2.如何创建一个AlertDialog对象:
步骤:
3.代码实现:
布局只有一个按钮,比较简单,这里就略过了
MainActivity.java
- <span style="font-family:SimSun;">public class MainActivity extends Activity {
- private Button btnshow;
- private AlertDialog.Builder build = null;
- private AlertDialog alert = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btnshow = (Button) findViewById(R.id.btnshow);
- btnshow.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //1.创建一个AlertDialog.Builder对象
- alert = new AlertDialog.Builder(MainActivity.this).create();
- //当然这里也可以这样写:build = new AlertDialog.Builder(getApplicationContext());
- //这里的话要在后面.set***吧属性设好
- //2.设置图标,标题,内容
- alert.setIcon(R.drawable.kabi);
- alert.setTitle("系统提示:");
- alert.setMessage("这是一个普通的AlertDialog,\n依次有取消,中立,确定按钮");
- //3.设置三个按钮,可以两个或者一个,最多只能设置三个按钮哦
- //确定按钮:
- alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(getApplicationContext(), "你点击了确定按钮", Toast.LENGTH_SHORT).show();
- }
- });
- alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(getApplicationContext(), "你点击了取消按钮", Toast.LENGTH_SHORT).show();
- }
- });
- alert.setButton(DialogInterface.BUTTON_NEUTRAL, "中立", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(getApplicationContext(), "你点击了中立按钮", Toast.LENGTH_SHORT).show();
- }
- });
- //4.调用show()方法把AlertDialog显示出来