image.png
目录
自动完成文本框
AutoCompleteTextView控件是Android中实现自动为完成文本框的功能,常常和Adapter搭配使用.
MultiAutoCompleteTextView控件是多行自动完成文本框,常常用来制作本地邮箱联想.
AutoCompleteTextView常用属性
属性 | 描述 |
---|---|
android:completionHint | 设置出现在下拉菜单底部的提示信息 |
android:completionThreshold | 设置触发补全提示信息的字符个数 |
android:dropDownHorizontalOffset | 设置下拉菜单于文本框之间的水平偏移量 |
android:dropDownHeight | 设置下拉菜单的高度 |
android:dropDownWidth | 设置下拉菜单的宽度 |
android:singleLine | 设置单行显示文本内容 |
android:dropDownVerticalOffset | 设置下拉菜单于文本框之间的垂直偏移量 |
实战1(AutoCompleteTextView)
xml文件:
代码语言:javascript复制<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<AutoCompleteTextView
android:id="@ id/a_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="200dp"
android:completionHint="请选择你喜欢的人物"
android:completionThreshold="1"
android:dropDownHeight="200dp"
tools:layout_editor_absoluteX="269dp"
tools:layout_editor_absoluteY="0dp" />
</android.support.constraint.ConstraintLayout>
代码文件:
代码语言:javascript复制package com.example.user.listview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取资源
AutoCompleteTextView aView =(AutoCompleteTextView)findViewById(R.id.a_view);
// 设定下拉选项
String[] strName = new String[] {"hello","hi","hemmmm","","hahahaaha"};
ArrayAdapter<String> name = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, strName);
// 设定适配器
aView.setAdapter(name);
}
}
运行效果
image.png
实战2(MultiAutoCompleteTextView)
多文本选取.
xml文件:
代码语言:javascript复制<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<MultiAutoCompleteTextView
android:id="@ id/m_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MultiAutoCompleteTextView"
android:width="400dp"
android:completionThreshold="1"
android:dropDownHeight="200dp"
android:dropDownWidth="400dp"/>
</android.support.constraint.ConstraintLayout>
代码文件:
代码语言:javascript复制package com.example.user.listview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取资源
MultiAutoCompleteTextView mView = (MultiAutoCompleteTextView)findViewById(R.id.m_view);
// 设定下拉选项
String[] strName = new String[] {"hello","hi","hemmmm","","hahahaaha"};
ArrayAdapter<String> name = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, strName);
// 设定适配器
mView.setAdapter(name);
// 设置分割符
mView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
运行效果
image.png
参考
MultiAutoCompleteTextView多文本选取 Android中的AutoCompleteTextView的使用