AutoCompleteTextView的使用,autocompletetextview

來源:互聯網
上載者:User

AutoCompleteTextView的使用,autocompletetextview
AutoCompleteTextView的使用

一、簡介

1、AutoCompleteTextView的作用

 

2、AutoCompleteTextView的類結構圖

也就是擁有EditText的各種功能

 

3、AutoCompleteTextView工作原理

AutoCompleteTextView的自動提示功能肯定需要適配器提供資料

 

4、Android裡的適配器

 

5、適合AutoCompleteTextView的適配器

ArrayAdapter

 

二、AutoCompleteTextView實現自動提示的方法

 1)AutoCompleteTextView實現自動提示的方法

第一步、建立適配器

String[] arr={"凱撒","凱撒廣場","凱撒大帝"};
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, R.layout.textview, arr);


第二步、AutoCompleteTextView對象應用適配器

autoCompleteTextView1.setAdapter(adapter);

 

說明:

提示文本是用textview實現的,提示文本裡面的提示資料就是String[] arr。

 

三、代碼執行個體

代碼:

fry.Activity01

 1 package fry; 2  3 import com.example.AutoCompleteTextViewDemo1.R; 4  5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.widget.ArrayAdapter; 8 import android.widget.AutoCompleteTextView; 9 10 public class Activity01 extends Activity{11     private AutoCompleteTextView autoCompleteTextView1;12     @Override13     protected void onCreate(Bundle savedInstanceState) {14         // TODO Auto-generated method stub15         super.onCreate(savedInstanceState);16         setContentView(R.layout.activity01);17         autoCompleteTextView1=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);18         /*19          * AutoCompleteTextView實現自動提示的方法20          * 第一步、建立適配器21          * 第二步、AutoCompleteTextView對象應用適配器22          * 23          */24         25         String[] arr={"凱撒","凱撒廣場","凱撒大帝"};26         ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, R.layout.textview, arr);27         autoCompleteTextView1.setAdapter(adapter);28     }29 }

fry.Activity01

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6  7     <AutoCompleteTextView  8         android:id="@+id/autoCompleteTextView1" 9         android:layout_width="match_parent"10         android:layout_height="wrap_content"11         />12 13 </LinearLayout>

/AutoCompleteTextViewDemo1/res/layout/textview.xml

1 <?xml version="1.0" encoding="utf-8"?>2 <TextView  xmlns:android="http://schemas.android.com/apk/res/android"3     android:layout_width="match_parent"4     android:layout_height="match_parent"5     android:orientation="vertical" >6     <!-- 這裡是直接TextView,而不是layout下的TextView -->7 </TextView >

 

 

 

四、易錯點

1、這裡是直接TextView,而不是layout下的TextView

/AutoCompleteTextViewDemo1/res/layout/textview.xml

1 <?xml version="1.0" encoding="utf-8"?>2 <TextView  xmlns:android="http://schemas.android.com/apk/res/android"3     android:layout_width="match_parent"4     android:layout_height="match_parent"5     android:orientation="vertical" >6     <!-- 這裡是直接TextView,而不是layout下的TextView -->7 </TextView >

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.