Android 类似PC下拉框Spinner

来源:互联网 发布:unity3d ugui对话框 编辑:程序博客网 时间:2024/06/05 01:06


Android原生自带的下拉框实在不太好看,只想产生一个类似于PC或网站上的那种自动完成类型的下拉框。
用到的控件为Spinner和AutoCompleteTextView。


在java文件中:

 

Java代码  收藏代码
  1.       String[] mListItems = {"item1""item2""item3"};          
  2.       ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, mListItems);   
  3.       final Spinner mSpinner = (Spinner) findViewById(R.id.type_Spinner);  
  4. mSpinner.setAdapter(mArrayAdapter);  
  5. mSpinner.setSelection(0);  
  6.   
  7. final AutoCompleteTextView mAutoCompleteTextView = (AutoCompleteTextView)this.findViewById(R.id.type);   
  8.       mAutoCompleteTextView.setAdapter(mArrayAdapter);   
  9.       mAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {  
  10.     @Override  
  11.     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  12.         // TODO Auto-generated method stub  
  13.         mSpinner.setSelection(position);  
  14.     }  
  15.       });  
  16.         
  17.       FrameLayout layout = (FrameLayout)findViewById(R.id.type_clicklayout);  
  18.       layout.setOnClickListener(new View.OnClickListener() {  
  19.     @Override  
  20.     public void onClick(View v) {  
  21.         // TODO Auto-generated method stub  
  22.         mAutoCompleteTextView.showDropDown();  
  23.     }  
  24.       });  

 

在xml文件中:

 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"   
  5.     android:layout_height="fill_parent">        
  6.     <FrameLayout  
  7.         android:layout_width="fill_parent"   
  8.         android:layout_height="50dip"  
  9.         android:layout_weight="1">         
  10.         <LinearLayout android:orientation="horizontal"  
  11.             android:layout_width="fill_parent"  
  12.             android:layout_height="fill_parent"  
  13.             android:gravity="center">  
  14.         <AutoCompleteTextView  
  15.             android:id="@+id/type"  
  16.             android:layout_width="fill_parent"  
  17.             android:layout_height="50dip"  
  18.             android:gravity="center"  
  19.             android:focusable="false"  
  20.             android:background="@null"  
  21.             android:cacheColorHint="#00000000"/>  
  22.         </LinearLayout>  
  23.           
  24.         <LinearLayout android:orientation="horizontal"  
  25.             android:layout_width="fill_parent"  
  26.             android:layout_height="fill_parent"  
  27.             android:gravity="center">  
  28.         <Spinner  
  29.             android:id="@+id/type_Spinner"  
  30.             android:layout_width="fill_parent"  
  31.             android:layout_height="50dip"  
  32.             android:gravity="center"                      
  33.             android:clickable="false"/>  
  34.         </LinearLayout>  
  35.           
  36.         <LinearLayout android:orientation="horizontal"  
  37.             android:layout_width="fill_parent"  
  38.             android:layout_height="fill_parent"  
  39.             android:gravity="center">  
  40.         <FrameLayout  
  41.             android:id="@+id/type_clicklayout"  
  42.             android:layout_width="fill_parent"   
  43.             android:layout_height="50dip"  
  44.             android:clickable="true"/>  
  45.         </LinearLayout>     
  46.     </FrameLayout>  
  47. </LinearLayout>  


效果图(2.2):

效果图(2.3):



原创链接:http://inuts.iteye.com/blog/1174321 转载请注明


0 0