基本视图——AutoCompleteTextView

作者:追风剑情 发布于:2015-8-29 19:10 分类:Android

视图

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. tools:context="${relativePackage}.${activityClass}" >
  7.  
  8. <TextView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="Name of President" />
  12. <AutoCompleteTextView android:id="@+id/txtCountries"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"/>
  15.  
  16. </LinearLayout>

代码

  1. package com.example.androidtest;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.widget.ArrayAdapter;
  6. import android.widget.AutoCompleteTextView;
  7.  
  8. public class AutoCompleteTextViewActivity extends Activity {
  9.  
  10. String[] presidents = {
  11. "Dwight D. Eisenhowe",
  12. "John F. Kennedy",
  13. "Lyndon B. Johnson",
  14. "Richard Nixon",
  15. "Gerald Ford",
  16. "Jimmy Catrter",
  17. "Ronald Reagan",
  18. "George H. W. Bush",
  19. "Bill Clinton",
  20. "George W. Bush",
  21. "Barack Obama"
  22. };
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_auto_complete_text_view);
  27. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
  28. android.R.layout.simple_dropdown_item_1line, presidents);
  29. AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountries);
  30. textView.setThreshold(3);//用户必须输入最少3个字符才自动显示建议列表。
  31. textView.setAdapter(adapter);
  32. }
  33. }

运行效果

aaaaaaaaaaaaaaaa.png

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号