1 : 效果图
btnGeneral.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this); builder.setTitle("提示"); builder.setMessage("这是一个普通的对话框!"); builder.setIcon(R.drawable.ic_launcher); builder.setCancelable(false); //必须采取行动 builder.setPositiveButton("知道了!", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.create().show(); } });
2 : 效果图
/*而除了专门为按钮点击实现的DialogInterFace.OnClickListener事件外,DialogInterface还提供了一些其他的事件,供Dialog对象响应,这些事件只是对Dialog声明周期各个状态的响应,一看就明白了,就不再详细讲解了,下面是这几个事件的说明: interface DialogInterface.OnCancelListener:当对话框调用cancel()方法的时候触发。 interface DialogInterface.OnDismissListener:当对话框调用dismiss()方法的时候触发。 interface DialogInterface.OnShowListener:当对话框调用show()方法的时候触发。 interface DialogInterface.OnMultiChoiceListener:当对话框使用多选列表,并且选中的时候触发*/ btnButtons.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this); builder.setTitle("提示"); builder.setMessage("这是一个多按钮普通的对话框!"); builder.setIcon(R.drawable.ic_launcher); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "确定被点击", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); builder.setNegativeButton("否定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "否定被点击", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); builder.setNeutralButton("忽略", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "忽略被点击", Toast.LENGTH_SHORT).show(); dialog.cancel(); } }); builder.show(); } });
3. 效果图
btnListViewSingle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this); builder.setTitle("请选择一下城市"); builder.setSingleChoiceItems(items, 1, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub String select_item = items[which].toString(); Toast.makeText(MainActivity.this, "选择了--->>" + select_item, Toast.LENGTH_SHORT) .show(); } }); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } });
4: 效果图
btnListViewMulti.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this); builder.setTitle("请选择城市"); builder.setMultiChoiceItems(items, new boolean[] { true, false, true }, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // TODO Auto-generated method stub String select_item = items[which].toString(); Toast.makeText(MainActivity.this, "选择了--->>" + select_item, Toast.LENGTH_SHORT) .show(); } }); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } });
5 :效果图 : 自定义dialog
btnCustomDialog.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_signin,null); Button btn=(Button)view.findViewById(R.id.btnCustom); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub alertDialog.dismiss(); Toast.makeText(MainActivity.this, "表单填写完成", Toast.LENGTH_SHORT).show(); } }); builder.setView(view); // 设置上 LayoutInflater alertDialog=builder.show(); } });
6 : 效果图 ProgressDialog
btnProgressDialog.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 第一种方法,使用ProgressDialog构造函数 progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setIcon(R.drawable.ic_launcher); progressDialog.setTitle("等待"); progressDialog.setMessage("正在加载...."); progressDialog.show(); //第二种方法,使用静态的show方法 //progressDialog=ProgressDialog.show(MainActivity.this, "等待", "正在加载....", false, false); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5000); } catch (Exception e) { e.printStackTrace(); } finally{ progressDialog.dismiss(); } } }).start(); } });