您的位置:首页 > 移动开发 > Android开发

使用AlertDialog创建对话框

2014-03-26 21:34 411 查看
弹窗的制作有很多种方法,今天学习了AlertDialog的使用。代码如下:

View view;
Dialog alertDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

view = View.inflate(this, R.layout.exit, null);

alertDialog = new AlertDialog.Builder(this).
setTitle("对话框的标题").
setView(view).

create();

Button btnButton = (Button) findViewById(R.id.button1);

btnButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
/*Intent intent = new Intent(getApplicationContext(),Exit.class);
startActivity(intent);*/

alertDialog.show();
}
});

由于AlertDialog所以的方法都是protect的,所以我们无法直接new它的对象来使用它,我们使用它的方法就如代码所示,直接使用AlertDoalog.Builer然后用set来设置其属性。
上面的代码是一个简单的使用,其实我们可以用AlerDialog来实现很多功能,如退出程序的确认,这样的共能无非就是在弹窗上加上按钮并为其设置监听事件而已。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android