关闭

Android AlertDialog example

520人阅读 评论(0) 收藏 举报
package com.cjp.alertdialog;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class AlertDialogDemoActivity extends Activity {
final Context context = this;
private Button button;
 
public void onCreate(Bundle savedInstanceState) {
 
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 
button = (Button) findViewById(R.id.buttonAlert);
 
// add button listener
button.setOnClickListener(new OnClickListener() {
 
@Override
public void onClick(View arg0) {
 
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
 
// set title
alertDialogBuilder.setTitle("Your Title");
 
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
AlertDialogDemoActivity.this.finish();
}
 })
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
 
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
 
// show it
alertDialog.show();
}
});
}

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:239486次
    • 积分:2739
    • 等级:
    • 排名:第13123名
    • 原创:54篇
    • 转载:35篇
    • 译文:0篇
    • 评论:59条
    文章分类
    最新评论