android 自動匹配文本 AutoCompleteTextView /MultiAutoCompleteTextView

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   java   io   資料   

AutoCompleteTextView:每個文字框只能匹配一次

MultiAutoCompleteTextView:能以分隔字元為分割點多次匹配

 

 

 1         //AutoComplete 的 XML樣本 2      <AutoCompleteTextView 3          android:completionThreshold="3"      //輸入數達到3時開始顯示提示 4          android:id="@+id/autoCompleteTextView1" 5          android:layout_width="match_parent" 6          android:layout_height="wrap_content" 7          android:layout_alignParentLeft="true" 8          android:layout_alignParentTop="true" 9          android:ems="10"10          android:hint="請輸入關鍵字" >11      </AutoCompleteTextView>    
 1         //AutoComplete java樣本 2  3 public class MainActivity extends Activity { 4     private AutoCompleteTextView acTextView; 5     private String[] res = {"beijing1","beijing2","beijing3",          //提示資源 6                             "shanghai1","shanghai2"}; 7      8     @Override 9     protected void onCreate(Bundle savedInstanceState) {10         super.onCreate(savedInstanceState);11         setContentView(R.layout.fragment_main);12         /**13          * 1、初始化控制項14          * 2、需要一個適配器,ArrayAdapter<String>15          * 3、初始化資料來源---匹配文字框輸入的內容16          * 4、將adapter與AutoCompleteTextView綁定17          */18         19         acTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);20         ArrayAdapter<String> adapter = new ArrayAdapter<>(this, 21                 android.R.layout.simple_list_item_1, res);    //3個參數:上下文,布局,資源22         acTextView.setAdapter(adapter);    //綁定23     }24 }

 

 

 

 

 

 1          //MultiAutoCompleteTextView XML樣本 2  3     <MultiAutoCompleteTextView 4         android:id="@+id/multiAutoCompleteTextView1" 5         android:layout_width="match_parent" 6         android:layout_height="wrap_content" 7         android:layout_alignParentLeft="true" 8         android:layout_alignParentTop="true" 9         android:ems="10"10         android:hint="請輸入關鍵字" />

 

 

 1        //MultiAutoCompleteTextView  的java樣本 2  3 public class MainActivity extends Activity { 4     private MultiAutoCompleteTextView macTextView; 5     private String[] res = {"beijing1","beijing2","beijing3", 6                             "shanghai1","shanghai2"}; 7      8     @Override 9     protected void onCreate(Bundle savedInstanceState) {10         super.onCreate(savedInstanceState);11         setContentView(R.layout.fragment_main);12         /**13          * 1、初始化控制項14          * 2、需要一個適配器,ArrayAdapter<String>15          * 3、初始化資料來源---匹配文字框輸入的內容16          * 4、將adapter與AutoCompleteTextView綁定17          * 5、設定分割符18          */19         20         macTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);21         ArrayAdapter<String> adapter = new ArrayAdapter<>(this, 22                 android.R.layout.simple_list_item_1, res);23         macTextView.setAdapter(adapter);24         //設定以 逗號為分割符25         macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); 26                                                     //在系統自訂中分隔字元只有 逗號,其他需要自設27     }28 }

 

 

 

 

                                                                                                                2014-08-16

 

聯繫我們

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