各种对话测试布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/user_dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="自定义对话框测试" />
<Button
android:id="@+id/general_list_dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="普通列表对话框测试" />
<Button
android:id="@+id/multic_list_dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="多选列表对话框测试" />
<Button
android:id="@+id/radio_list_dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="单选列表对话框测试" />
<Button
android:id="@+id/adapter_list_dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="提供列表项的列表对话框测试" />
</LinearLayout>
主程序代码测试:
/*
* 对话框测试主入口
* @author Administrator
*
*/
public class DialogMainActivity extends Activity {
private RadioGroup color,size;
private RadioButton c1,c2,c3,c4,c5,s1,s2,s3,s4,s5;
private TextView textView;
private Button general,multic,radio,adapter,user;
// 初始化
public void init(){
user = (Button) findViewById(R.id.user_dialog);
general = (Button) findViewById(R.id.general_list_dialog);
multic = (Button) findViewById(R.id.multic_list_dialog);
radio = (Button) findViewById(R.id.radio_list_dialog);
adapter = (Button) findViewById(R.id.adapter_list_dialog);
}
// 程序入口
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_main_layout);
init(); // 初始化
user.setOnClickListener(listener);
general.setOnClickListener(listener);
multic.setOnClickListener(listener);
radio.setOnClickListener(listener);
adapter.setOnClickListener(listener);
}
/**
* 普通列表对话框测试
*/
public void generalListDialog(){
Builder b = new Builder(this);
b.setTitle("普通列表对话框测试");
b.setIcon(R.drawable.ic_launcher);
b.setItems(new String[]{"西方","南方","北方","西北方"},new OnClickListener() {
public void onClick(DialogInterface dialog, int which) { // which 代表被点击的项
Toast.makeText(DialogMainActivity.this, "选中了:"+new String[]{"西方","南方","北方","西北方"}[which],Toast.LENGTH_LONG).show();
}
});
b.create();
b.show();
}
/**
* 单选对话框测试
*/
public void radioListDialog(){
Builder b = new AlertDialog.Builder(this);
b.setTitle("单选列表对话框测试");
b.setIcon(R.drawable.ic_launcher);
final String[] data = new String[]{"男","女","中"};
/*
* 参数: 数据 默认选项 事件
*/
b.setSingleChoiceItems(data, 0, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogMainActivity.this, "选中了:"+data[which],Toast.LENGTH_LONG).show();
}
});
// 确定
b.setPositiveButton("确定", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// 取消
b.setNegativeButton("取消",new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
b.create();
b.show();
}
/**
* 创建多选列表对话框
*/
public void multicListDialog(){
Builder b = new AlertDialog.Builder(this);
b.setTitle("多选列表对话框测试");
b.setIcon(R.drawable.ic_launcher);
final String[] data = new String[]{"吃饭","冲浪","散步"};
/*
* 参数: 数据 默认选项 事件
*/
b.setMultiChoiceItems(data, null, new OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
/* which 索引 - isChecked 是否被选中 */
if(isChecked){
Toast.makeText(DialogMainActivity.this, "选中了:"+data[which],Toast.LENGTH_LONG).show();
}
}
});
// 确定
b.setPositiveButton("确定", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// 取消
b.setNegativeButton("取消",new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
b.create();
b.show();
}
/**
* 提供列表项的对话框测试
*/
public void adapterListDialog(){
Builder b = new AlertDialog.Builder(this);
b.setTitle("提供列表对话框测试"); // 标题
b.setIcon(R.drawable.ic_launcher); // 图标
/*
* 参数: 数据 默认选项 事件
*/
// 确定
b.setPositiveButton("确定", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// 取消
b.setNegativeButton("取消",new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
b.create();
b.show();
}
// 事件 -- 测试各种动作
private android.view.View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
if(v.getId() == R.id.general_list_dialog){ // 普通测试
generalListDialog();
return;
}
if(v.getId() == R.id.user_dialog){ // 自定义测试
userDialog();
return;
}
if(v.getId() == R.id.radio_list_dialog){ // 单选测试
radioListDialog();
return;
}
if(v.getId() == R.id.multic_list_dialog){ // 多选测试
multicListDialog();
return;
}
if(v.getId() == R.id.adapter_list_dialog){ // 多选测试
adapterListDialog();
return;
}
}
};
/**
* 自定义对话框测试
*/
public void userDialog(){
// 创建对话框对象
Builder builder = new Builder(this);
builder.setTitle("字体样式选择"); // 设置标题
builder.setIcon(R.drawable.ic_launcher); // 设置图标
// 确定按钮 -- 执行预期动作
builder.setPositiveButton("确定",new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
/* 载入自定义布局文件 -- 达成自定义的效果 */
LayoutInflater inflater = LayoutInflater.from(this); // 获取视图容器对象
// 根据视图容器获取布局对象
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.user_dialog_item,null);
builder.setView(layout); // 将布局视图显示在对话框中
// 获取自定义布局中的组件对象
color = (RadioGroup) layout.findViewById(R.id.colorRadioGroup);
size = (RadioGroup) layout.findViewById(R.id.sizeRadioGroup);
c1 = (RadioButton) layout.findViewById(R.id.c1);
c2 = (RadioButton) layout.findViewById(R.id.c2);
c3 = (RadioButton) layout.findViewById(R.id.c3);
c4 = (RadioButton) layout.findViewById(R.id.c4);
s1 = (RadioButton) layout.findViewById(R.id.s1);
s2 = (RadioButton) layout.findViewById(R.id.s2);
s3 = (RadioButton) layout.findViewById(R.id.s3);
s4 = (RadioButton) layout.findViewById(R.id.s4);
// 设置监听器
color.setOnCheckedChangeListener(radioListener);
size.setOnCheckedChangeListener(radioListener);
/* ------------------------------------------------------------------ */
// 取消按钮,点击取消对话框
builder.setNegativeButton("取消",new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.create(); // 创建
builder.show(); // 显示
}
// RadioGroup 事件改变监听器
private OnCheckedChangeListener radioListener = new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == c1.getId()){ //
Toast.makeText(DialogMainActivity.this,"字体颜色", 1000).show();
}
if(checkedId == s1.getId()){
Toast.makeText(DialogMainActivity.this,"字体大小", 1000).show();
}
}
};
}
=============================自定义对话框======================================
自定义对话框布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/user_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="请看这里" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="颜色选择:" />
<RadioGroup
android:id="@+id/colorRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/c2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/c3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/c4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="颜色选择:" />
<RadioGroup
android:id="@+id/sizeRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/s1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/s2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/s3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/s4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
主程序入口: