1. 有两个按钮的对话框
Builder builder=new AlertDialog.Builder(AlertDialogActivity.this); builder.setIcon(android.R.drawable.btn_plus); builder.setTitle("btnplus"); builder.setMessage("去不去?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { Toast.makeText(AlertDialogActivity.this,"你选择了确定按钮",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { Toast.makeText(AlertDialogActivity.this, "你选择了取消按钮",Toast.LENGTH_SHORT).show(); } } ); builder.show(); }
2. 带有三个按钮的对话框
public void onClick(View v) { // TODO Auto-generated method stub new AlertDialog.Builder(AlertDialogActivity.this) .setIcon(android.R.drawable.btn_star).setTitle("温馨提示").setMessage("提示内容,三个按钮"). setPositiveButton("确定",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(AlertDialogActivity.this, "你选择了确定按钮", Toast.LENGTH_SHORT).show(); } }) .setNeutralButton("菜单",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(AlertDialogActivity.this, "你选择了详细按钮", Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("取消",new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog,int which){ Toast.makeText(AlertDialogActivity.this,"你选择了取消按钮",Toast.LENGTH_SHORT).show(); } }) .show(); }
3. 能进行输入的对话框
public void onClick(View v) { // TODO Auto-generated method stub LayoutInflater inflater=LayoutInflater.from(AlertDialogActivity.this); final View textEntryView=inflater.inflate(R.layout.alert_dialog_text_entry,null); final EditText usernameET=(EditText)textEntryView.findViewById(R.id.username_value); final EditText passwordET=(EditText)textEntryView.findViewById(R.id.password_value); new AlertDialog.Builder(AlertDialogActivity.this) .setIcon(android.R.drawable.btn_star) .setTitle("温馨提示") .setView(textEntryView) .setPositiveButton("确定",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { Toast.makeText(AlertDialogActivity.this,"用户名="+usernameET.getText().toString()+"\n密码="+passwordET.getText().toString(),Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("取消",new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog,int which) { Toast.makeText(AlertDialogActivity.this,"你选择了取消按钮",Toast.LENGTH_SHORT).show(); } }) .show();
4. 进度条对话框
ProgressDialog dialog=new ProgressDialog(AlertDialogActivity.this); dialog.setTitle("处理中。。。"); dialog.setMessage("请稍等。。。"); dialog.show();