Android 可以輸入的下拉框

來源:互聯網
上載者:User

標籤: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();            }        });    }

這樣基本幾可以了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.