美文网首页
自动联想和补全的textview

自动联想和补全的textview

作者: _蘇芳_ | 来源:发表于2017-01-19 17:51 被阅读99次

title: 自动联想和补全的textview
date: 2016-11-28 10:33:27
tags: tips


  • 自动联想和补全的textview在需求中还是很常见的,首先在布局中

    <AutoCompleteTextView
            android:id="@+id/et_phone"
            android:hint="@string/please_input_phonenum"
            android:drawableStart="@mipmap/phone_num_icon"
            android:drawablePadding="15dp"
            android:paddingStart="23dp"
            android:maxLength="11"
            android:inputType="phone"
            android:background="@drawable/shape_roundrec_fff"
            android:textColor="@color/addclock_right_text"
            android:textSize="18sp"
            android:textColorHint="@color/addclock_right_text"
            android:layout_width="match_parent"
            android:gravity="center_vertical"
            android:layout_height="43dp"
            android:layout_marginTop="35dp"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            />
    

那些什么颜色、背景的引用什么的随意更换好了,这是我的

  • java代码中

    AutoCompleteTextView etPhone = findViewById(R.id.et_phone);
    etPhone.setThreshold(1); //这个是设置输入一个字符就开始联想,默认是从第二个字符开始的
    //        etPhone.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_dropdown_item_1line,phoneList));   //这个是默认的下拉菜单
    
    //要想自己设置下拉的布局,可以自己设置,但要注意一点,详见下面的布局;phoneList可以从网络、sp、数据库等获取
    etPhone.setAdapter(new ArrayAdapter<>(this,R.layout.tv_white_bg,phoneList)); 
    
    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawableStart="@mipmap/hudong_add"
        android:drawablePadding="15dp"
        style="?android:attr/dropDownItemStyle"   //这个很重要,不然那个下拉框会出现在各种奇怪的位置
        android:paddingStart="23dp"
        android:maxLength="11"
        android:gravity="center_vertical"
         <!--这个可以控制下拉菜单出现在谁的下面,不写的话默认就是在AutoCompleteTextView的下方-->
        android:dropDownAnchor="@id/btn_next"    
        <!--android:inputType="phone"  
      注意!!没有这玩意儿!!手滑给textview加了个结果导致联想出来的popup无法点击-->
        android:background="@color/white"
        android:textColor="@color/addclock_right_text"
        android:textSize="18sp"
        android:layout_width="match_parent"
        android:layout_height="43dp"/>
    

相关文章

网友评论

      本文标题:自动联想和补全的textview

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