• 011 Android AutoCompleteTextView(自动完成文本框)的基本使用


    1.XML布局

    android:completionThreshold="1":这里我们设置了输入一个字就显示提示

    (1)主界面布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        tools:context=".MainActivity">
    
        <AutoCompleteTextView
            android:id="@+id/actv_show"
            android:layout_marginRight="20dp"
            android:layout_marginLeft="20dp"
            android:completionThreshold="1"
            android:textColor="#000000"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </LinearLayout>

    (2)Arrayadapter布局

    <?xml version="1.0" encoding="utf-8"?>
    
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        style="?android:attr/dropDownItemStyle"
        android:textAppearance="?android:attr/textAppearanceLargePopupMenu"
        android:singleLine="true"
        android:layout_width="match_parent"
        android:textColor="#000000"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:ellipsize="marquee" />

    2.java后台

    package com.example.administrator.test59autocompletetextview;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.AutoCompleteTextView;
    
    public class MainActivity extends AppCompatActivity {
        private static final String[] data = new String[]{
                "小猪猪", "小狗狗", "小鸡鸡", "小猫猫", "小咪咪"
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //找到AutoCompleteTextView控件
            AutoCompleteTextView actv_show=findViewById(R.id.actv_show);
            //采用数组适配器,第一个参数是上下文,就是当前的Activity, 第二个参数是自己定义的一个布局,它里面只有一个TextView,第三个参数就是我们要显示的数据。
            ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getApplicationContext(), R.layout.actv_layout, data);
            actv_show.setAdapter(adapter2);
        }
    }

    3.效果图

  • 相关阅读:
    [原]Linux ssh远程连接断开问题处理办法
    【网摘】CURL常用命令
    【树莓派】在树莓派中进行截图
    【树莓派】树莓派移动网络连接(配置4G网卡)
    【树莓派】基于TinyProxy搭建HTTP代理服务器
    【树莓派】制作树莓派所使用的img镜像(二)
    【树莓派】制作树莓派所使用的img镜像(一)
    macaca运行报错之chrome-driver问题处理,关闭 Chrome 的自动更新
    Git使用
    Longest Consecutive Sequence leetcode java
  • 原文地址:https://www.cnblogs.com/luckyplj/p/10808049.html
Copyright © 2020-2023  润新知