Android widget——AutoCompleteTextView學習筆記

來源:互聯網
上載者:User

AutoCompleteTextView:

         An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.

The drop down can be dismissed at any time by pressing the back key or, if no item is selected in the drop down, by pressing the enter/dpad center key.

The list of suggestions is obtained from a data adapter and appears only after a given number of characters defined by the threshold.

   即,一個可輸入的TextView,但是,它綁定了一些初始的資料,當使用者輸入一部分字元(預設是2個字元)後,它會根據綁定的內容而自動匹配,並把符合的結果以下拉式功能表的形式顯示出來,使用者可以單擊某個下拉式功能表中的選項而完成輸入。

重要屬性:

 android:completionThreshold="1",Defines the number of characters that the user must type before completion suggestions are displayed in a drop down menu。即,設定使用者輸入多少個字元後,才匹配,並出現下拉框。

樣本1:

autocomplete_1.xml

 <?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"></p><p> <TextView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="@string/autocomplete_1_instructions" /></p><p> <LinearLayout<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"></p><p> <TextView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="@string/autocomplete_1_country" /></p><p> <AutoCompleteTextView android:id="@+id/edit"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"/></p><p> </LinearLayout></p><p> <Button<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="@string/autocomplete_1_focus" /></p><p></LinearLayout>

AutoComplete1.java

public class AutoComplete1 extends Activity {</p><p> @Override<br /> protected void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.autocomplete_1);</p><p> ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,<br /> android.R.layout.simple_dropdown_item_1line, COUNTRIES);<br /> AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit);<br /> textView.setAdapter(adapter);<br /> }</p><p> static final String[] COUNTRIES = new String[] {<br />"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",<br />"Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",<br />"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",<br />"Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",<br />"Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",<br />"Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",<br />"British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",<br />"Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",<br />"Cayman Islands", "Central African Republic", "Chad", "Chile", "China",<br />"Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",<br />"Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",<br />"Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",<br />"East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",<br />"Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland",<br />"Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia",<br />"French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar",<br />"Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",<br />"Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary",<br />"Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica",<br />"Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",<br />"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",<br />"Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",<br />"Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",<br />"Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",<br />"Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand",<br />"Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas",<br />"Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru",<br />"Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",<br />"Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena",<br />"Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon",<br />"Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal",<br />"Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",<br />"Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea",<br />"Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden",<br />"Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas",<br />"The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",<br />"Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",<br />"Ukraine", "United Arab Emirates", "United Kingdom",<br />"United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan",<br />"Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara",<br />"Yemen", "Yugoslavia", "Zambia", "Zimbabwe"<br /> };<br />}  

 

樣本2:

autocomplete_4.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"></p><p> <TextView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="@string/autocomplete_4_instructions" /></p><p> <LinearLayout<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"></p><p> <TextView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="@string/autocomplete_4_name" /></p><p> <AutoCompleteTextView android:id="@+id/edit"<br /> android:completionThreshold="1"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"/></p><p> </LinearLayout></p><p> <TextView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="@string/autocomplete_4_message" /></p><p></LinearLayout>

AutoComplete4.java

 public class AutoComplete4 extends Activity {<br /> @Override<br /> protected void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.autocomplete_4);</p><p> ContentResolver content = getContentResolver();<br /> Cursor cursor = content.query(Contacts.People.CONTENT_URI,<br /> PEOPLE_PROJECTION, null, null, Contacts.People.DEFAULT_SORT_ORDER);<br /> ContactListAdapter adapter = new ContactListAdapter(this, cursor);</p><p> AutoCompleteTextView textView = (AutoCompleteTextView)<br /> findViewById(R.id.edit);<br /> textView.setAdapter(adapter);<br /> }</p><p> // XXX compiler bug in javac 1.5.0_07-164, we need to implement Filterable<br /> // to make compilation work<br /> public static class ContactListAdapter extends CursorAdapter implements Filterable {<br /> public ContactListAdapter(Context context, Cursor c) {<br /> super(context, c);<br /> mContent = context.getContentResolver();<br /> }</p><p> @Override<br /> public View newView(Context context, Cursor cursor, ViewGroup parent) {<br /> final LayoutInflater inflater = LayoutInflater.from(context);<br /> final TextView view = (TextView) inflater.inflate(<br /> android.R.layout.simple_dropdown_item_1line, parent, false);<br /> view.setText(cursor.getString(5));<br /> return view;<br /> }</p><p> @Override<br /> public void bindView(View view, Context context, Cursor cursor) {<br /> ((TextView) view).setText(cursor.getString(5));<br /> }</p><p> @Override<br /> public String convertToString(Cursor cursor) {<br /> return cursor.getString(5);<br /> }</p><p> @Override<br /> public Cursor runQueryOnBackgroundThread(CharSequence constraint) {<br /> if (getFilterQueryProvider() != null) {<br /> return getFilterQueryProvider().runQuery(constraint);<br /> }</p><p> StringBuilder buffer = null;<br /> String[] args = null;<br /> if (constraint != null) {<br /> buffer = new StringBuilder();<br /> buffer.append("UPPER(");<br /> buffer.append(Contacts.ContactMethods.NAME);<br /> buffer.append(") GLOB ?");<br /> args = new String[] { constraint.toString().toUpperCase() + "*" };<br /> }</p><p> return mContent.query(Contacts.People.CONTENT_URI, PEOPLE_PROJECTION,<br /> buffer == null ? null : buffer.toString(), args,<br /> Contacts.People.DEFAULT_SORT_ORDER);<br /> }</p><p> private ContentResolver mContent;<br /> }</p><p> private static final String[] PEOPLE_PROJECTION = new String[] {<br /> Contacts.People._ID,<br /> Contacts.People.PRIMARY_PHONE_ID,<br /> Contacts.People.TYPE,<br /> Contacts.People.NUMBER,<br /> Contacts.People.LABEL,<br /> Contacts.People.NAME,<br /> };<br />}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.