android autocompletetextview例子

本文概述

  • Android AutoCompleteTextView示例
android autocompletetextview例子

文章图片
Android AutoCompleteTextView基于保留的单词来完成单词,因此无需编写单词的所有字符。
Android AutoCompleteTextView是可编辑的文本字段,它在下拉菜单中显示建议列表,用户只能从中选择一个建议或值。
Android AutoCompleteTextView是EditText类的子类。 MultiAutoCompleteTextView是AutoCompleteTextView类的子类。
Android AutoCompleteTextView示例在此示例中,我们在autocompletetextview中显示编程语言。所有的编程语言都存储在字符串数组中。我们正在使用ArrayAdapter类来显示数组内容。
让我们看看android中autocompletetextview的简单示例。
activity_main.xml从面板上拖动AutoCompleteTextView和TextView,现在activity_main.xml文件将如下所示:
< ?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="example.srcmini.com.autocompletetextview.MainActivity">< TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="What is your favourite programming language?" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.032" />< AutoCompleteTextView android:id="@+id/autoCompleteTextView" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginLeft="92dp" android:layout_marginTop="144dp" android:text="" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />< /android.support.constraint.ConstraintLayout>

活动类【android autocompletetextview例子】让我们编写AutoCompleteTextView的代码。
package example.srcmini.com.autocompletetextview; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class MainActivity extends AppCompatActivity { String[] language ={"C", "C++", "Java", ".NET", "iPhone", "Android", "ASP.NET", "PHP"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Creating the instance of ArrayAdapter containing list of language names ArrayAdapter< String> adapter = new ArrayAdapter< String> (this, android.R.layout.select_dialog_item, language); //Getting the instance of AutoCompleteTextView AutoCompleteTextView actv =(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView); actv.setThreshold(1); //will start working from first character actv.setAdapter(adapter); //setting the adapter data into the AutoCompleteTextView actv.setTextColor(Color.RED); } }

输出:
android autocompletetextview例子

文章图片
android autocompletetextview例子

文章图片

    推荐阅读