Spinner下拉列表我们经常用到,一般我们在xml文件定义好,然后在代码中设置一下adapter即可。
<FrameLayout
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_weight="1.0"
android:paddingRight="8dp" >
<Spinner
android:id="@+id/sp_mode_ddjdevwrite"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:animationCache="true"
android:background="#FFFFFF"
android:drawSelectorOnTop="true"
android:prompt="@string/mode"
android:spinnerMode="dialog" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:src="@drawable/list" />
</FrameLayout>
spMode = (Spinner)findViewById(R.id.sp_mode_ddjdevwrite);
String[] modeArray = new String[]{"YR710-4", "YR800-4", "YR710-6/1180", "YR800-6/1180",
"YR900-6/1180", "YR1000-6/1180", "YR1120-6/1180", "YR1250-6/1430", "YR1400-6/1430",
"YR1600-6/1430", "YR1800-6/1430", "YR2000-6/1430", "YR2240-6/1730",
"YR2500-6/1730", "YR630-8/1180", "YR710-8/1180", "YR800-8/1180", "YR900-8/1430",
"YR1000-8/1430", "YR1120-8/1430", "YR1250-8/1430", "YR1400-8/1430",
"YR1600-8/1730", "YR1800-8/1730", "YR2000-8/1730", "YR2240-8/2150",
"YR2500-8/2150", "YR500-10/1180", "YR560-10/1180", "YR630-10/1180",
"YR710-10/1180", "YR800-10/1180", "YR900-10/1430", "YR1000-10/1430",
"YR1120-10/1430", "YR1250-10/1430", "YR1400-10/1430", "YR1600-10/1730",
"YR1800-10/1730", "YR2000-10/1730", "YR2240-10/2150", "YR2500-10/2150",
"YR450-12/1430", "YR500-12/1430", "YR560-12/1430", "YR630-12/1430",
"YR710-12/1430", "YR800-12/1430", "YR900-12/1730", "YR1000-12/1730",
"YR1120-12/1730", "YR1250-12/1730", "YR1400-12/1730", "YR1600-12/1730",
"YR1800-12/1730", "YR2000-12/2150", "YR2240-12/2150", "YR2500-12/2150",
"YR450-16/1430", "YR500-16/1430", "YR560-16/1430", "YR630-16/1430", "YR710-16/1430",
"YR800-16/1430", "YR900-16/1730", "YR1000-16/1730", "YR1120-16/1730", "YR1250-16/1730",
"YR1400-16/2150"};
modelAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, modeArray);
modelAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//为Spinner设置适配器
spMode.setAdapter(modelAdapter);
如果下拉的数据比较多,想要找到我们想要的数据就很不方便,如果有个筛选输入框(在输入的时候自动去匹配)就好,
有这样的需求那么接下来就实现这样的需求:
开始我是在想Spinner要是有个方法能设置它的布局就好,但是Spinner并没有提供这样的方法,于是就从Spinner的源码分析,发现没那么简单
点击Spinner时内部调用的是Spinner内部类DialogPopup的show()方法,而show()方法创建的是有一个AlertDialog。此时想扩展继承Spinner覆盖它的内部类DialogPopup同时覆盖它的show方法 那更是有难度(从JAVA的角度,java根本就没有提供内部类“覆盖”这么一种机制, 参考 http://blog.csdn.net/dksokil/article/details/45461