当前位置: 代码迷 >> Android >> Android用户界面 UI组件-自动提示输入框 AutoCompleteTextView跟MultiAutoCompleteTextView
  详细解决方案

Android用户界面 UI组件-自动提示输入框 AutoCompleteTextView跟MultiAutoCompleteTextView

热度:37   发布时间:2016-04-28 06:35:50.0
Android用户界面 UI组件--自动提示输入框 AutoCompleteTextView和MultiAutoCompleteTextView

AutoCompleteTextView: 就是一个带自动提示的EditText,当输入字符时,会出现提示.
android:completionThreshold?
输入几个字符时提示
android:completionHint/setCompletionHint(CharSequence)
设置出现下拉列表的提示标题
android:dropDownHeight/setDropHeight(int)
设置下拉列表的高度
android:dropDownWidth/setDropWidth(int)
设置下拉列表的宽度
android:popupBackground/setDropDownbackgroundResource(int)
设置下拉列表的背景
在Android程序中,为了展示数据,通常会用到一个Adapter的接口
setAdapter()设置提示内容的Adapter.

MultiAutoCompleteTextView是多选选项
使用MultiAutoCompleteTextView必须实现MultiAutoCompleteTextView.Tokenizer接口
用于声明用于选项与选项之间分隔的符号,一般如不特殊指定,可以使用Android为我们提供的实现类MultiAutoCompleteTextView.CommaTokenizer,它设定使用英文逗号","进行分隔选项.

例子:

?

[java]?view plaincopy
?
  1. <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  2. ????xmlns:tools="http://schemas.android.com/tools"??
  3. ????android:layout_width="match_parent"??
  4. ????android:layout_height="match_parent"??
  5. ????android:orientation="vertical"??
  6. ????tools:context=".MainActivity"?>??
  7. ??????
  8. ????<LinearLayout?android:layout_width="match_parent"??
  9. ??????????????????android:layout_height="wrap_content"??
  10. ??????????????????android:orientation="horizontal">??
  11. ??????
  12. ????<TextView??
  13. ????????android:layout_width="wrap_content"??
  14. ????????android:layout_height="wrap_content"??
  15. ????????android:text="单选搜索"??
  16. ????????tools:ignore="HardcodedText"?/>??
  17. ??
  18. ????<AutoCompleteTextView?android:id="@+id/autoText"??
  19. ??????????????????????????android:layout_width="match_parent"??
  20. ??????????????????????????android:layout_height="wrap_content"??
  21. ??????????????????????????/>??
  22. ??????
  23. ???</LinearLayout>??
  24. ???<LinearLayout?android:layout_width="match_parent"??
  25. ??????????????????android:layout_height="wrap_content">??
  26. ????<TextView??
  27. ????????android:layout_width="wrap_content"??
  28. ????????android:layout_height="wrap_content"??
  29. ????????android:text="多选搜索"??
  30. ????????tools:ignore="HardcodedText"?/>??
  31. ??????
  32. ???<MultiAutoCompleteTextView?android:id="@+id/autoMultiText"??
  33. ??????????????????????????????android:layout_width="match_parent"??
  34. ??????????????????????????????android:layout_height="wrap_content"??
  35. ??????????????????????????????/>??
  36. ??</LinearLayout>??
  37. ?????
  38. ??
  39. </LinearLayout>??
[java]?view plaincopy
?
  1. package?com.light.android.study;??
  2. ??
  3. import?android.app.Activity;??
  4. import?android.os.Bundle;??
  5. import?android.view.Gravity;??
  6. import?android.view.View;??
  7. import?android.view.View.OnClickListener;??
  8. import?android.widget.ArrayAdapter;??
  9. import?android.widget.AutoCompleteTextView;??
  10. import?android.widget.CheckBox;??
  11. import?android.widget.CompoundButton;??
  12. import?android.widget.MultiAutoCompleteTextView;??
  13. import?android.widget.MultiAutoCompleteTextView.CommaTokenizer;??
  14. import?android.widget.RadioGroup;??
  15. import?android.widget.RadioGroup.OnCheckedChangeListener;??
  16. import?android.widget.Toast;??
  17. import?android.widget.ToggleButton;??
  18. ??
  19. public?class?MainActivity?extends?Activity?{??
  20. ??
  21. ????private?AutoCompleteTextView?autoText;??
  22. ????private?MultiAutoCompleteTextView?autoMultiText;??
  23. ????private?String[]?keywords?=?{??
  24. ????????????"ab","abc","abdfdf","abnsd","abdfff"??
  25. ????};??
  26. ????@Override??
  27. ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  28. ????????super.onCreate(savedInstanceState);??
  29. ????????setContentView(R.layout.activity_main);??
  30. ????????init();??
  31. ????????initAdapter();??
  32. ????}??
  33. ??????
  34. ????private?void?init(){??
  35. ????????autoText?=?(AutoCompleteTextView)?findViewById(R.id.autoText);??
  36. ????????autoMultiText?=?(MultiAutoCompleteTextView)?findViewById(R.id.autoMultiText);??
  37. ????}??
  38. ??????
  39. ????private?void?initAdapter(){??
  40. ????????ArrayAdapter<String>?adapter?=?new?ArrayAdapter<String>(this,?android.R.layout.simple_dropdown_item_1line,?keywords);??
  41. ????????autoText.setAdapter(adapter);??
  42. ????????autoMultiText.setAdapter(adapter);??
  43. ????????autoMultiText.setTokenizer(new?MultiAutoCompleteTextView.CommaTokenizer());??
  44. ????}??
  45. ??????
  46. ??
  47. }??

?

?

效果:

?

  相关解决方案