標籤:android style class blog code http
將AutoCompleteTextView與一個button組合起來,做成一個可以輸入,也可以選擇的下拉框。
下面是定義的xml檔案,auto_spinner.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="fill_parent" 5 android:layout_height="50dip" 6 android:background="@android:drawable/edit_text" 7 android:paddingRight="10dip" 8 android:weightSum="4"> 9 10 <AutoCompleteTextView11 android:id="@+id/repeateMode"12 android:layout_width="0dip"13 android:layout_height="fill_parent"14 android:layout_weight="3"15 android:background="@null"16 android:cacheColorHint="#00000000"17 android:completionThreshold="1"18 android:dropDownHorizontalOffset="20dp"19 android:ems="10"20 android:gravity="center_vertical" >21 22 <requestFocus />23 </AutoCompleteTextView>24 25 <ImageButton26 android:id="@+id/repeateModeBtn"27 android:layout_width="0dp"28 android:layout_height="fill_parent"29 android:layout_weight="1.0"30 android:src="@android:drawable/btn_dropdown"31 />32 33 </LinearLayout>
這是部分代碼:
…… private void init(Context context) { // this.context = context; LayoutInflater.from(context).inflate(R.layout.auto_spinner, this); adapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, list); text = (AutoCompleteTextView) this.findViewById(R.id.repeateMode); text.setAdapter(adapter); button = (ImageButton) this.findViewById(R.id.repeateModeBtn); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 顯示下拉框 text.showDropDown(); } }); }
這樣基本幾可以了。