Button dialog1 = (Button) findViewById(R.id.dialog1); dialog1.setOnClickListener(new OnClickListener() { public void onClick(View v) { AlertDialog.Builder builder = new Builder(DialogActivity.this); builder.setTitle("标题"); builder.setPositiveButton("确定",null); builder.setIcon(android.R.drawable.ic_dialog_info); builder.setMessage("简单消息框"); builder.show(); } });
下面是有确认取消的对话框
Button dialog2 = (Button) findViewById(R.id.dialog2); dialog2.setOnClickListener(new OnClickListener() { public void onClick(View v) { AlertDialog.Builder builder = new Builder(DialogActivity.this); builder.setTitle("标题"); builder.setPositiveButton("确定",null); builder.setNegativeButton("取消",null); builder.setIcon(android.R.drawable.ic_dialog_info); builder.setMessage("简单消息框"); builder.show(); } });
下面是带有输入框的对话框
Button dialog3 = (Button) findViewById(R.id.dialog3); dialog3.setOnClickListener(new OnClickListener() { public void onClick(View v) { AlertDialog.Builder builder = new Builder(DialogActivity.this); builder.setTitle("标题"); builder.setView(new EditText(DialogActivity.this)); builder.setPositiveButton("确定",null); builder.setNegativeButton("取消",null); builder.setIcon(android.R.drawable.ic_dialog_info); builder.setMessage("简单消息框"); builder.show(); } });
带有单选择按钮的对话框
Button dialog4 = (Button) findViewById(R.id.dialog4); dialog4.setOnClickListener(new OnClickListener() { public void onClick(View v) { new AlertDialog.Builder(DialogActivity.this) .setTitle("请选择") .setIcon(android.R.drawable.ic_dialog_info) .setSingleChoiceItems(new String[] {"item1","item2","item3","item4"}, 0, new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which){ dialog.dismiss(); } } ) .setNegativeButton("取消", null) .show(); } }); Button dialog4 = (Button) findViewById(R.id.dialog4); dialog4.setOnClickListener(new OnClickListener() { public void onClick(View v) { new AlertDialog.Builder(DialogActivity.this) .setTitle("请选择") .setIcon(android.R.drawable.ic_dialog_info) .setSingleChoiceItems(new String[] {"item1","item2","item3","item4"}, 0, new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which){ dialog.dismiss(); } } ) .setNegativeButton("取消", null) .show(); } });
带有多选择按钮的对话框
Button dialog5 = (Button) findViewById(R.id.dialog5); dialog5.setOnClickListener(new OnClickListener() { public void onClick(View v) { new AlertDialog.Builder(DialogActivity.this) .setTitle("请选择") .setIcon(android.R.drawable.ic_dialog_info) .setMultiChoiceItems(new String[] {"item1","item2","item3","item4"}, null, null) .setNegativeButton("取消", null) .show(); } });