美文网首页
【Android】AutoCompleteTextView控件

【Android】AutoCompleteTextView控件

作者: 感同身受_ | 来源:发表于2019-05-22 16:25 被阅读0次
  1. 效果:实现类似于热门提示的效果
  2. 使用的方式类似于适配器的实现。
  3. arrayAdapter所对应的item必须是以TextView结尾的,否则会出现:
    如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/item_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="显示"
        android:padding="5dp"/>

</LinearLayout>

Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.view.ViewGroup
解决办法:
将布局文件从LinearLayout换成TextView

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_tv"
    android:textSize="20sp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="显示"
    android:padding="5dp">
</TextView>

AutoCompleteTextView常用属性:

  1. completionHint:设置出现在下拉菜单中的提示标题(和hint的效果不太一样)
    android:completionHint="请输入"

  2. completionThreshold:设置用户至少输入多少个字符才会显示提示(默认为两个)
    android:completionThreshold="1"

    捕获9.PNG
  3. dropDownHorizontalOffset:下拉菜单位于文本框之间的水平偏移。默认与文本框左对齐

  4. dropDownHeight:下拉菜单高度

  5. dropDownWidth:下拉菜单的宽度

  6. singleLine:单行显示

相关文章

网友评论

      本文标题:【Android】AutoCompleteTextView控件

      本文链接:https://www.haomeiwen.com/subject/zaduzqtx.html