国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現實時搜索框功能

2019-10-22 18:26:57
字體:
來源:轉載
供稿:網友

AutoCompleteTextView,自動完成文本框。

用于實現允許用戶輸入一定字符后,顯示一個下拉菜單,供用戶從中選擇,當用戶選擇某個選項后,按用戶選擇自動填寫該文本框。

該組件繼承EditText,所以它支持EditText組件提供的屬性,同時,該組件該支持如下功能。

Android實時搜索框,Android搜索框,Android實時搜索

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="com.amy.searchtest.MainActivity"> <LinearLayout  android:orientation="horizontal"  android:layout_width="fill_parent"  android:layout_height="fill_parent">   <AutoCompleteTextView  android:id="@+id/autoCompleteTextView1"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text=""  android:completionThreshold="2"  android:completionHint="請輸入搜索內容..."  android:layout_weight="7"/>   <Button  android:id="@+id/button1"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="搜索"  android:layout_weight="1"  android:layout_marginLeft="10px"/>   </LinearLayout>  </android.support.constraint.ConstraintLayout> 

MainActivity.java

package com.amy.searchtest;  import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.Toast;  public class MainActivity extends AppCompatActivity {   public static final String[] CONTENTS = new String[]{"zg陜西","zg海南","zg新疆","zg西藏"};  AutoCompleteTextView textView;  @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  textView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);  //創建一個ArrayAdapter適配器  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,CONTENTS);  textView.setAdapter(adapter);   Button button = (Button) findViewById(R.id.button1);  button.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View v) {   Toast.makeText(MainActivity.this, textView.getText().toString(),Toast.LENGTH_SHORT).show();   }  });  }  } 

效果圖

Android實時搜索框,Android搜索框,Android實時搜索

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高尔夫| 诏安县| 句容市| 哈尔滨市| 阜新| 绍兴市| 涞源县| 汉川市| 连州市| 搜索| 志丹县| 巍山| 突泉县| 久治县| 吉木萨尔县| 鹿泉市| 天峨县| 泗洪县| 田东县| 博湖县| 沂水县| 疏附县| 静海县| 文安县| 泸州市| 东阳市| 嵊泗县| 曲沃县| 炎陵县| 上思县| 谷城县| 讷河市| 辽宁省| 珲春市| 淄博市| 双牌县| 大连市| 威远县| 陇川县| 壤塘县| 丹凤县|