AlertDialog 创建简单的 弹出对话框
效果图:
一个 java文件,一个 xml布局文件 只有一个button
java文件代码:
public class Main extends Activity {
private static final String tag = "==CrazyIt.org==";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Builder b = new AlertDialog.Builder(this);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
b.setIcon(R.drawable.z1);
b.setTitle("标题");
b.setMessage("进入了对话框!");
b.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
Toast.makeText(Main.this, "点击确定按钮",
Toast.LENGTH_SHORT).show();
}
});
b.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
Toast.makeText(Main.this, "点击取消按钮",
Toast.LENGTH_SHORT).show();
}
});
b.setNeutralButton("aaa",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
Toast.makeText(Main.this, "点击。。。按钮",
Toast.LENGTH_SHORT).show();
}
});
b.create().show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}