图1
图2
图3
图4
图5
图6
图7
package com.wjq.dia; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.Toast; public class Main extends Activity implements android.view.View.OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.btnicon).setOnClickListener(this); findViewById(R.id.btnsimpleview).setOnClickListener(this); findViewById(R.id.btnmultichoiceitems).setOnClickListener(this); findViewById(R.id.btnsinglechoiceitems).setOnClickListener(this); findViewById(R.id.btnsimpleitems).setOnClickListener(this); findViewById(R.id.btnviewlayout).setOnClickListener(this); } /** * 当按返回键时的提示对话框,采用默认样式 */ protected void dialog() { AlertDialog.Builder builder = new Builder(Main.this); builder.setMessage("确认退出吗?"); builder.setTitle("提示"); builder.setPositiveButton("确认", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Main.this.finish(); } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { dialog(); } return false; } @Override public void onClick(View v) { switch (v.getId()) { /** * 改变图表 */ case R.id.btnicon: Dialog dialog = new AlertDialog.Builder(this).setIcon( android.R.drawable.btn_star).setTitle("喜好调查").setMessage( "你喜欢李连杰的电影吗?").setPositiveButton("很喜欢", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(Main.this, "我很喜欢他的电影。", Toast.LENGTH_LONG).show(); } }).setNegativeButton("不喜欢", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(Main.this, "我不喜欢他的电影。", Toast.LENGTH_LONG) .show(); } }).setNeutralButton("一般", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(Main.this, "谈不上喜欢不喜欢。", Toast.LENGTH_LONG) .show(); } }).create(); dialog.show(); break; /** * 改变显示信息内容的类型,本例是一个编辑框EditText */ case R.id.btnsimpleview: new AlertDialog.Builder(this).setTitle("请输入").setIcon( android.R.drawable.ic_dialog_info).setView( new EditText(this)).setPositiveButton("确定", null) .setNegativeButton("取消", null).show(); break; case R.id.btnmultichoiceitems: new AlertDialog.Builder(this).setTitle("复选框").setMultiChoiceItems( new String[] { "Item1", "Item2"}, null, null) .setPositiveButton("确定", null) .setNegativeButton("取消", null).show(); break; case R.id.btnsinglechoiceitems: new AlertDialog.Builder(this).setTitle("单选框").setIcon( android.R.drawable.ic_dialog_info).setSingleChoiceItems( new String[] {"Item1", "Item2"}, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).setNegativeButton("取消", null).show(); break; case R.id.btnsimpleitems: new AlertDialog.Builder(this).setTitle("列表框").setItems( new String[] { "Item1", "Item2" }, null) .setNegativeButton("确定", null).show(); break; case R.id.btnviewlayout: LayoutInflater inflater=getLayoutInflater(); View layout=inflater.inflate(R.layout.dialog, (ViewGroup)findViewById(R.id.dialog)); new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout) .setPositiveButton("确定", null) .setNegativeButton("取消", null).show(); break; } } }
评论