Dialog
对话框在各种应用或者游戏开发都会用到,当然也会以各种不同的方式出现。
第一种:Toast(他不会打断当前正在操作的应用,在屏幕上显示一段时间(2秒左右)后消失)。
代码如下:
Toast.makeText(this, "", Toast.LENGTH_LONG).show(); //参数1:Context对象 参数2:对话框提示的内同 参数3:对象框展示的方式
第二种:Builder
private Builder builder; private TextView txtTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(new BitmapBodyView(this)); setContentView(R.layout.activity_main); txtTextView=(TextView)this.findViewById(R.id.lab); builder=new Builder(this); builder.setIcon(R.drawable.ic_launcher); //设置图标 builder.setTitle("Dialog");//设置标题 builder.setMessage("I'm Dialog");//设置标题内容 builder.setPositiveButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub txtTextView.setText("点击了取消"); } }); builder.setNegativeButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { txtTextView.setText("点击了确定"); } }); builder.show(); }
Builder设置单选对话框:
private Builder builder; private TextView txtTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(new BitmapBodyView(this)); setContentView(R.layout.activity_main); txtTextView = (TextView) this.findViewById(R.id.lab); builder = new Builder(this); builder.setIcon(R.drawable.ic_launcher); // 设置图标 builder.setTitle("Dialog");// 设置标题 builder.setSingleChoiceItems(new String[] { "单选一", "单选二" }, 1, //为对话框设置单选框 new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub txtTextView.setText("你选择了"+which); } }); builder.setPositiveButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub txtTextView.setText("点击了取消"+which); } }); builder.setNegativeButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { txtTextView.setText("点击了确定"+which); } }); builder.show(); }
Builder设置多选对话框:
private Builder builder; private TextView txtTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(new BitmapBodyView(this)); setContentView(R.layout.activity_main); txtTextView = (TextView) this.findViewById(R.id.lab); builder = new Builder(this); builder.setIcon(R.drawable.ic_launcher); // 设置图标 builder.setTitle("Dialog");// 设置标题 builder.setMultiChoiceItems(new String[] { "多选一", "多选二" }, new boolean[] { false, false }, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // TODO Auto-generated method stub String msg=isChecked?"有":"没有"; txtTextView.setText("你"+msg+"选择了" + which); } }); builder.show(); }
Builder设置列表选择
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(new BitmapBodyView(this)); setContentView(R.layout.activity_main); txtTextView = (TextView) this.findViewById(R.id.lab); builder = new Builder(this); builder.setIcon(R.drawable.ic_launcher); // 设置图标 builder.setTitle("Dialog");// 设置标题 builder.setItems(new String[]{"列表一","列表二"},new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); builder.show();