不多说,先看效果图哈.
好了,上代码
MainActivity.java
public class MainActivity extends Activity {
private Button btn;
private List list = new ArrayList();
private AlertDialog.Builder builder;
private AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
list = initData();
btn = (Button) findViewById(R.id.btn_list);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ShowDialog();//弹框操作
}
});
}
//初始化数据
private ArrayList initData() {
ArrayList list = new ArrayList();
for(int i = 0;i
String name = "布丁布丁"+i;
list.add(name);
}
return list;
}
public void ShowDialog() {
Context context = MainActivity.this;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.formcommonlist, null);
ListView myListView = (ListView) layout.findViewById(R.id.formcustomspinner_list);
MyAdapter adapter = new MyAdapter(context, list);
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int positon, long id) {
//在这里面就是执行点击后要进行的操作,这里只是做一个显示
Toast.makeText(MainActivity.this, "您点击的是"+list.get(positon).toString(), 0).show();
if (alertDialog != null) {
alertDialog.dismiss();
}
}
});
builder = new AlertDialog.Builder(context);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
//自定义的适配器
class MyAdapter extends BaseAdapter {
private List mlist;
private Context mContext;
public MyAdapter(Context context, List list) {
this.mContext = context;
mlist = new ArrayList();
this.mlist = list;
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public Object getItem(int position) {
return mlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Person person = null;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.rtu_item,null);
person = new Person();
person.name = (TextView)convertView.findViewById(R.id.tv_name);
convertView.setTag(person);
}else{
person = (Person)convertView.getTag();
}
person.name.setText(list.get(position).toString());
return convertView;
}
class Person{
TextView name;
}
}
}
主界面
activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.example.dialogspinner.MainActivity" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/title_background">
android:id="@+id/btn_list"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dip"
android:background="@drawable/commom_title2"
android:gravity="left|center_vertical"
android:maxLines="1"
android:visibility="visible" />
android:id="@+id/tvDeviceName2"
android:layout_width="fill_parent"
android:layout_height="25dp"
android:gravity="center"
android:layout_toRightOf="@+id/btn_list"
android:text="弹框ListView"
android:textSize="17sp"
android:textColor="#FF8C00" />
弹框显示样式
formcommonlist.xml
android:layout_width="fill_parent"
android:id="@+id/layout_myview"
android:layout_height="fill_parent"
android:background="@android:color/background_light"
android:orientation="vertical"
android:padding="0.0dip" >
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#defafd"
android:padding="10.0dip" >
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/title"
android:text="请选择"
android:textColor="#0182af"
android:textSize="20.0dip" />
android:id="@+id/formcustomspinner_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:cacheColorHint="@null"
android:divider="@android:drawable/divider_horizontal_bright"
android:scrollbars="none" />
弹框里的ListView单个选项
rtu_item.xml
android:layout_width="match_parent"
android:layout_height="35dip"
android:orientation="horizontal" >
android:id="@+id/tv_name"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:gravity="center"
android:text="布丁"
android:textColor="#000000"
android:textSize="15sp" />
好了,这样就完成了,有需要源码的请点击