import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.*; public class AlertDialogTest extends Activity { private LinearLayout mainView=null; private Button button1=null; private Button button2=null; private Button button3=null; private Button button4=null; private AlertDialog ad1=null; private AlertDialog ad2=null; private AlertDialog ad3=null; private AlertDialog ad4=null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mainView_init(); setContentView(mainView); } /*button1初始化*/ void button1_init(){ button1=new Button(this); button1.setLayoutParams(new LinearLayout.LayoutParams(-2,-2)); button1.setTextSize(button1.getTextSize()+5); button1.setText("1个按键的对话框"); button1.setOnClickListener(new OnClickListener(){ public void onClick(View v) { if(ad1==null){ ad1_init(); } ad1.show(); } }); } /*button2初始化*/ void button2_init(){ button2=new Button(this); button2.setLayoutParams(new LinearLayout.LayoutParams(-2,-2)); button2.setTextSize(button1.getTextSize()+5); button2.setText("2个按键的对话框"); button2.setOnClickListener(new OnClickListener(){ public void onClick(View v) { if(ad2==null){ ad2_init(); } ad2.show(); } }); } /*button3初始化*/ void button3_init(){ button3=new Button(this); button3.setLayoutParams(new LinearLayout.LayoutParams(-2,-2)); button3.setTextSize(button1.getTextSize()+5); button3.setText("3个按键的对话框"); button3.setOnClickListener(new OnClickListener(){ public void onClick(View v) { if(ad3==null){ ad3_init(); } ad3.show(); } }); } /*button4初始化*/ void button4_init(){ button4=new Button(this); button4.setLayoutParams(new LinearLayout.LayoutParams(-2,-2)); button4.setTextSize(button1.getTextSize()+5); button4.setText("自定义View对话框"); button4.setOnClickListener(new OnClickListener(){ public void onClick(View v) { if(ad4==null){ ad4_init(); } ad4.show(); } }); } /*mainView初始化*/ void mainView_init(){ mainView=new LinearLayout(this); mainView.setLayoutParams(new LinearLayout.LayoutParams(-1,-1)); mainView.setOrientation(LinearLayout.VERTICAL); button1_init(); button2_init(); button3_init(); button4_init(); mainView.addView(button1); mainView.addView(button2); mainView.addView(button3); mainView.addView(button4); } /*ad1初始化*/ void ad1_init(){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setIcon(android.R.drawable.ic_menu_info_details); builder.setTitle("这是标题"); builder.setPositiveButton("知道了", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { ad1.dismiss();//此处可以添加你要执行的其他程序 } }); ad1=builder.create(); } /*ad2初始化*/ void ad2_init(){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setTitle("这是标题"); builder.setMessage("这是内容,带两个按键"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //此处可以添加你要执行的其他程序 ad1.dismiss();//关闭Dialog } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //此处可以添加你要执行的其他程序 ad2.dismiss();//关闭Dialog } }); ad2=builder.create(); } /*ad3初始化*/ void ad3_init(){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setIcon(android.R.drawable.ic_menu_help); builder.setTitle("这是标题"); builder.setMessage("这是内容,带三个按键。"); builder.setPositiveButton("Positive", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //此处可以添加你要执行的其他程序 ad3.dismiss();//关闭Dialog } }); builder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //此处可以添加你要执行的其他程序 ad3.dismiss();//关闭Dialog } }); builder.setNegativeButton("Negative", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //此处可以添加你要执行的其他程序 ad3.dismiss();//关闭Dialog } }); ad3=builder.create(); } /*ad1初始化*/ void ad4_init(){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setIcon(android.R.drawable.ic_menu_edit); builder.setTitle("请输入用户名和密码"); //自定义View——mainLayout LinearLayout mainLayout=new LinearLayout(this); mainLayout.setLayoutParams(new LinearLayout.LayoutParams(-1,-2)); mainLayout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2); lp.setMargins(20,0,20,0); TextView tv1=new TextView(this); tv1.setLayoutParams(lp); tv1.setText("用户名:"); EditText et1=new EditText(this); et1.setLayoutParams(lp); TextView tv2=new TextView(this); tv2.setLayoutParams(lp); tv2.setText("密码:"); EditText et2=new EditText(this); et2.setLayoutParams(lp); mainLayout.addView(tv1); mainLayout.addView(et1); mainLayout.addView(tv2); mainLayout.addView(et2); // builder.setView(mainLayout); builder.setPositiveButton("知道了", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //此处可以添加你要执行的其他程序 ad1.dismiss();//关闭Dialog } }); ad4=builder.create(); } }