Android 可以输入的下拉框

将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     <AutoCompleteTextView
11         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     <ImageButton
26         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();
            }
        });
    }

这样基本几可以了。

posted @ 2014-06-24 15:34  安谧世界  阅读(4018)  评论(0编辑  收藏  举报