Adapter类型控件之AutoCompleteTextView(自动完成文本框)&MultiAutoCompleteTextView(多提示项的自动完成文本框)

(一)概述
这里写图片描述
(二)相关属性
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
比如,火星1号—>你输入“火”—->会提示火星1号,而你输入1号—–>却不糊提示火星1号
(三)代码实现案例
运行效果:
这里写图片描述

实现代码:
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android_autocompletetextview.MainActivity" >

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="AutoCompleteTextView" />

    <AutoCompleteTextView
        android:id="@+id/myAutoCompleteTextView"
         android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/myTextView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="MultiAutoCompleteTextView" />

    <MultiAutoCompleteTextView
        android:id="@+id/myMultiAutoCompleteTextView"
         android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    private AutoCompleteTextView attv;//输入内容后自动提示功能的控件
    private MultiAutoCompleteTextView mattv;//多个AutoCompleteTextView    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        attv = (AutoCompleteTextView) findViewById(R.id.myAutoCompleteTextView);
        //数据源
        String autoStrings[]= new String[]{"火星1号","火星2号","火星3号","火星4号","apple","Sunner","Sunning","Sun"};


        //第二个参数表示适配器的下拉风格

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, 
                android.R.layout.simple_dropdown_item_1line,autoStrings);
        //绑定适配器
        attv.setAdapter(adapter);
        mattv = (MultiAutoCompleteTextView) findViewById(R.id.myMultiAutoCompleteTextView);
        mattv.setAdapter(adapter);
        mattv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());// 完成对选项的拆分的功能,以逗号进行拆分
    }

}

一些可能会用到的布局属性:
这里写图片描述

这里写图片描述

<完>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值