WEB开发网
开发学院手机开发Android 开发 Android widget AutoCompleteTextView 学习笔记 阅读

Android widget AutoCompleteTextView 学习笔记

 2010-03-05 20:54:00 来源:WEB开发网   
核心提示:AutoCompleteTextView:An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a d
  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"?》  
《LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
   android:orientation="vertical"  
   android:layout_width="fill_parent"   
   android:layout_height="wrap_content"》  
 
   《TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="@string/autocomplete_1_instructions" /》  
 
   《LinearLayout  
     android:orientation="horizontal"  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"》  
 
     《TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="@string/autocomplete_1_country" /》  
 
     《AutoCompleteTextView android:id="@+id/edit"  
       android:layout_width="fill_parent"  
       android:layout_height="wrap_content"/》  
 
   《/LinearLayout》  
 
   《Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="@string/autocomplete_1_focus" /》  
 
《/LinearLayout》
 

  AutoComplete1.java

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

  示例2:

  autocomplete_4.xml

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

  AutoComplete4.java

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

Tags:Android widget AutoCompleteTextView

编辑录入:coldstar [复制链接] [打 印]
赞助商链接