自动联想和补全的textview


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"/>
    

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容