最近在做上面描述的功能,网上找了个几个例子,拼拼凑凑整出来了效果,首先多谢http://www.cnblogs.com/miya2012/archive/2012/08/23/2652224.html, 代码实现差不多,特地做了个例子。附在最后?
不多说,先看效果图哈.
好了,上代码
MainActivity.java
public class MainActivity extends Activity { private Button btn; private List<String> list = new ArrayList<String>(); 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<String> initData() { ArrayList<String> list = new ArrayList<String>(); 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<String> mlist; private Context mContext; public MyAdapter(Context context, List<String> list) { this.mContext = context; mlist = new ArrayList<String>(); 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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:context="com.example.dialogspinner.MainActivity" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/title_background"> <Button 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" /> <TextView 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" /> </RelativeLayout> </LinearLayout>
弹框显示样式
formcommonlist.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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" > <LinearLayout android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#defafd" android:padding="10.0dip" > <TextView 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" /> </LinearLayout> <ListView 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 的线为灰色 --> </LinearLayout>
弹框里的ListView单个选项
rtu_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="35dip" android:orientation="horizontal" > <TextView 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" /> </LinearLayout>
好了,这样就完成了,有需要源码的请点击
时间: 2024-11-05 06:24:03