首页 > 代码库 > android基础之Dialog
android基础之Dialog
Builder类中还有一个SetView()方法:
可以在对话框中添加组件
builder.setView(new CheckBox(this));//添加CheckBox
Builder.setMultiChoiceItems(String[]arg0,Boolean[]arg1,OnMultiChoiceClickListener arg3)//添加复选框的方法
Builder.setSingleChoiceItems(String[]arg0,int arg1,OnClickListener arg3)//添加单选框
代码如下:
package com.example.dialog;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
public class MainActivity extends Activity {
private Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
builder=new Builder(MainActivity.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("Dialog");
//builder.setMessage("dialog对话框");
builder.setPositiveButton("yes",new OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
}});
builder.setNegativeButton("no",new OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
}
}
);
/*builder.setSingleChoiceItems(new String[]{"单选", "单选"},1,new OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
}
}
);
*/
/*
builder.setMultiChoiceItems(new String[]{"多选","多选"},new boolean[]{false,true},new OnMultiChoiceClickListener()
{
public void onClick(DialogInterface dialog,int which,boolean inChecked)
{
}
}
);*/
/*builder.setItems(new String[]{"列表项1","列表项2","列表项3"},new OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
}
}
);*/
LayoutInflater inflater=getLayoutInflater();
View layout=inflater.inflate(R.layout.dialog, (ViewGroup)findViewById(R.id.myLayout));
builder.setView(layout);
builder.show();
}
}
在对话框中 除了可以添加组件外 还可以添加布局。
增加一个xml布局文件。
运行如下:
本文出自 “hagar” 博客,请务必保留此出处http://7832308.blog.51cto.com/7822308/1429965