Android弹出对话框
1、
new AlertDialog.Builder(Lesson_01_Pic.this).setTitle("提示标题").setMessage("这是提示内容").show();
2、
new AlertDialog.Builder(Lesson_01_Pic.this) .setTitle("这是标题") .setMessage("这是提示内容") .setPositiveButton("确定", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialoginterface, int i){ //按钮事件 Toast.makeText(Lesson_01_Pic.this, "确定",Toast.LENGTH_LONG).show(); } }).show();
3、
import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Lesson_02_Dia extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button)findViewById(R.id.b01); button.setText("对话框"); button.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { //选项数组 String[] choices={"Facebook","Twitter"}; //Check判断数组,与选项对应 boolean[] chsBool = {true,false}; //包含多个选项及复选框的对话框 AlertDialog dialog = new AlertDialog.Builder(Lesson_02_Dia.this) .setIcon(android.R.drawable.btn_star_big_on) .setTitle("调查") .setMultiChoiceItems(choices, chsBool, multiClick) .setPositiveButton("Yes", onclick) .setNegativeButton("No", onclick).create(); dialog.show(); } }); } /** * 对话框复选框事件监听器 */ OnMultiChoiceClickListener multiClick = new OnMultiChoiceClickListener(){ @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(Lesson_02_Dia.this, "第"+(which+1)+"项,选中结果:"+isChecked,Toast.LENGTH_SHORT).show(); } }; /** * 对话框按钮点击事件监听器 */ OnClickListener onclick = new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case Dialog.BUTTON_NEGATIVE: Toast.makeText(Lesson_02_Dia.this, "No..", Toast.LENGTH_LONG).show(); break; case Dialog.BUTTON_NEUTRAL: Toast.makeText(Lesson_02_Dia.this, "I don't know.", Toast.LENGTH_LONG).show(); break; case Dialog.BUTTON_POSITIVE: Toast.makeText(Lesson_02_Dia.this, "Yes!!", Toast.LENGTH_LONG).show(); break; } } }; }
4、
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button)findViewById(R.id.b01); button.setText("对话框"); button.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { //选项数组 String[] choices={"新浪微博","校内","街旁"}; //包含多个选项的对话框 AlertDialog dialog = new AlertDialog.Builder(Lesson_02_Dia.this) .setIcon(android.R.drawable.btn_star) .setTitle("分享") .setItems(choices, onselect).create(); dialog.show(); } }); } ** * 选项的事件监听器 */ OnClickListener onselect = new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub switch (which) { case 0: Toast.makeText(Lesson_02_Dia.this, "您选择了新浪微博.",Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(Lesson_02_Dia.this, "您选择了校内",Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(Lesson_02_Dia.this, "您选择了街旁",Toast.LENGTH_SHORT).show(); break; } } };