import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class Main extends Activity { /** Called when the activity is first created. */ private Button button1; private Button button2; private Button button3; private Button button4; private Button button5; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1=(Button)findViewById(R.id.btn1); button2=(Button)findViewById(R.id.btn2); button3=(Button)findViewById(R.id.btn3); button4=(Button)findViewById(R.id.btn4); button5=(Button)findViewById(R.id.btn5); button1.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { new AlertDialog.Builder(Main.this) .setTitle("标题") .setItems(R.array.items_dialog, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub CharSequence strDialogBody=getString(R.string.hello); String[] it = getResources().getStringArray(R.array.items_dialog); new AlertDialog.Builder(Main.this) .setMessage(strDialogBody + it[which]) .setNeutralButton(R.string.app_ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //在这里做要处理的事情 } }).show(); } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //在这里做要处理的事情 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }).show(); } }); button2.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { new AlertDialog.Builder(Main.this) .setTitle("标题") .setMessage("消息框") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //在这里做要处理的事情 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }).show(); } }); button3.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { new AlertDialog.Builder(Main.this) .setTitle("请输入") // .setIcon(R.drawable.app) .setIcon(android.R.drawable.ic_dialog_info) .setView(new EditText(Main.this)) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //在这里做要处理的事情 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }).show(); } }); button4.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { new AlertDialog.Builder(Main.this) .setTitle("请输入") .setIcon(android.R.drawable.ic_dialog_info) // .setView(new EditText(Main.this)) .setSingleChoiceItems(new String[] {"choice 1","choice 2","choice 3","choice 4"}, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }).show(); } }); button5.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { new AlertDialog.Builder(Main.this) .setTitle("请输入") .setIcon(android.R.drawable.ic_dialog_info) // .setView(new EditText(Main.this)) .setMultiChoiceItems(new String[] {"choice 1","choice 2","choice 3","choice 4"}, null,new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // TODO Auto-generated method stub } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }).show(); } }); } }
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, Main!</string> <string name="app_name">AlertDialogDemo</string> <string name="app_ok">Hello OK!</string> <array name="items_dialog"> <item>你好</item> <item>你好!</item> <item>你好!!</item> </array> </resources>
来自:http://blog.csdn.net/weiyidemaomao/article/details/7861723