代码如下:
public class MainActivity extends ActionBarActivity implements OnClickListener {
private Dialog dialog2;
private Dialog dialog3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.btn);
Button btn_first=(Button) findViewById(R.id.btn_first);
Button btn_second=(Button) findViewById(R.id.btn_second);
btn.setOnClickListener(this);
btn_first.setOnClickListener(this);
btn_second.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btn:
//分步设置dialog属性
AlertDialog.Builder dialog= new AlertDialog.Builder(MainActivity.this);
//设置标题
dialog.setTitle("提示信息");
//设置logo
dialog.setIcon(R.drawable.ic_launcher);
dialog.setMessage("这是提示内容!!!!");
//create()可省略,但show()不可省略
dialog.create();
dialog.show();
break;
case R.id.btn_first:
dialog2 = new AlertDialog.Builder(MainActivity.this)
//设置标题
.setTitle("删除文件")
//设置对话框内容
.setMessage("确定要删除吗?")
//设置图标
.setIcon(R.drawable.ic_launcher)
//设置确认按钮
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "删除文件", 100).show();
}
})
.setNeutralButton("信息", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "详细信息", 100).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "取消删除文件", 100).show();
dialog2.dismiss();
}
})
//create()可省略,但show()不可省略
.create();
dialog2.show();
break;
//加载布局的dialog
case R.id.btn_second:
//加载布局
View view=View.inflate(MainActivity.this, R.layout.content, null);
Button btn_confirm =(Button) view.findViewById(R.id.btn_confirm);
Button btn_cancle =(Button) view.findViewById(R.id.btn_cancel);
btn_confirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "登录", 100).show();
//如果是AlertDialog ,没有dismiss()方法;Dialog有dismiss()方法
dialog3.dismiss();
}
});
btn_cancle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "取消", 100).show();
dialog3.dismiss();
}
});
dialog3 = new AlertDialog.Builder(MainActivity.this)
.setTitle("用户登录")
.setIcon(R.drawable.ic_launcher)
.setView(view)
.show();
break;
default:
break;
}
}