<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.expandablelist_test.MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/bt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/imgselector"
/>
</LinearLayout>
package com.example.expandablelist_test;
import java.util.ArrayList;
import java.util.List;
import android.animation.AnimatorSet.Builder;
import android.app.*;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity {
private Button bt;
CharSequence[] items = { "1", "2", "3" };
boolean[] itemsIschecked = new boolean[items.length];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//调用showdialog方法时,将调用这个回调方法。
//showDialog()方法接受一个整形参数,用来标识要显示的对话框
showDialog(0);
}
});
}
/*
*要显示一个对话框需要Activity类中实现 onCreateDialog方法
*/
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
android.app.AlertDialog.Builder builder = new AlertDialog.Builder(
this);
builder.setTitle("Test");
builder.setIcon(R.drawable.ic_launcher);
//下面的代码分别设置了两个按钮
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "OK checked",
Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Cancel checked",
Toast.LENGTH_LONG).show();
}
});
//设置一个复选框列表。对于该方法而言需要传入两个数组,一个是要显示的列表项,一个是每个列表是否被选中的值。
builder.setMultiChoiceItems(items, itemsIschecked,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
Toast.makeText(
MainActivity.this,
items[which]
+ (isChecked ? "Checked"
: "unchecked"),
Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
return null;
}
}