Android原生自带的下拉框实在不太好看,只想产生一个类似于PC或网站上的那种自动完成类型的下拉框。
用到的控件为Spinner和AutoCompleteTextView。
在java文件中:
- String[] mListItems = {"item1", "item2", "item3"};
- ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, mListItems);
- final Spinner mSpinner = (Spinner) findViewById(R.id.type_Spinner);
- mSpinner.setAdapter(mArrayAdapter);
- mSpinner.setSelection(0);
- final AutoCompleteTextView mAutoCompleteTextView = (AutoCompleteTextView)this.findViewById(R.id.type);
- mAutoCompleteTextView.setAdapter(mArrayAdapter);
- mAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- // TODO Auto-generated method stub
- mSpinner.setSelection(position);
- }
- });
- FrameLayout layout = (FrameLayout)findViewById(R.id.type_clicklayout);
- layout.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- mAutoCompleteTextView.showDropDown();
- }
- });
在xml文件中:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <FrameLayout
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:layout_weight="1">
- <LinearLayout android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center">
- <AutoCompleteTextView
- android:id="@+id/type"
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:gravity="center"
- android:focusable="false"
- android:background="@null"
- android:cacheColorHint="#00000000"/>
- </LinearLayout>
- <LinearLayout android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center">
- <Spinner
- android:id="@+id/type_Spinner"
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:gravity="center"
- android:clickable="false"/>
- </LinearLayout>
- <LinearLayout android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center">
- <FrameLayout
- android:id="@+id/type_clicklayout"
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:clickable="true"/>
- </LinearLayout>
- </FrameLayout>
- </LinearLayout>
效果图(2.2):
效果图(2.3):
原创链接:http://inuts.iteye.com/blog/1174321 转载请注明