最近在做上面描述的功能,网上找了个几个例子,拼拼凑凑整出来了效果,首先多谢 代码实现差不多,特地做了个例子。附在最后
不多说,先看效果图哈.
好了,上代码
MainActivity.java
public class MainActivity extends Activity { private Button btn; private Listlist = 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 < 15;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
弹框显示样式
formcommonlist.xml
弹框里的ListView单个选项
rtu_item.xml
好了,这样就完成了,有需要源码的请点击