Android UI系列-----EditText和AutoCompleteTextView


在這篇隨筆里將主要講解一下EditText和AutoCompleteTextView這個控件

1.EditText

首先我們先簡單來說說EditText這個控件,這個就相當於我們平常web開發中的文本輸入框,我們如果要使用EditText,可以在布局文件中聲明一個<EditText>這個元素即可,下面就是一個簡單的EditText的控件聲明:

<EditText 
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="username"/>

對於EditText來說,其最重要的一個屬性是 android:inputType,這個屬性不僅可以指定鍵盤的顯示類型,還能控制一些其他的操作,具體可以參考android的官方API,其默認屬性是 android:inputType="text",也就是普通的鍵盤框,如果我們設置其屬性為以下這些,那么其鍵盤的類型會有所不同:

<EditText 
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="password"
        android:inputType="textPassword"/>  這個是我們的密碼框
    
    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="email"
        android:inputType="textEmailAddress"/>  當設置為textEmailAddress時,鍵盤會多出來一個 @ 符號
    
    <EditText
        android:id="@+id/blog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="blog url"
        android:inputType="textUri"/>  設置為textUri時,鍵盤會多出一個 / 符號
    
    <EditText
        android:id="@+id/phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="phone"
        android:inputType="phone"/>  設置為phone時,鍵盤就會變成一個打電話時的鍵盤
    
    <EditText
        android:id="@+id/counts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="counts"
        android:inputType="number"/>  設置為number時,鍵盤上全部都是數字鍵

對於輸入框來說,我們通常都要對其輸入的數據進行判斷,inputType這個屬性不會對我們輸入的內容進行校驗,如果我們要對輸入的內容進行校驗,我們需要在Activity里面進行操作

EditText有一個setError的方法,當調用這個方法時,則表示輸入的數據不合法,我們來看看官方的API對該方法的解釋:

void android.widget.TextView.setError(CharSequence error)

Sets the right-hand compound drawable of the TextView to the "error" icon and sets an error message that will be displayed in a popup when the TextView has focus. The icon and error message will be reset to null when any key events cause changes to the TextView's text. If the error is null, the error message and icon will be cleared.

這個方法會給我們一個錯誤的小圖標以及彈出的一段錯誤提示信息,當我們的這個EditText控件獲得焦點的時候,當我們在文本框中輸入了任何的值后,這個icon和message都會消失,例如:

EditText還有許多其他的方法,這個在以后實際用的的時候再闡述。

2.AutoCompleteTextView

AutoCompleteTextView這個是一個自動提示內容的文本框,其是EditText的一個子類,

An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.

The drop down can be dismissed at any time by pressing the back key or, if no item is selected in the drop down, by pressing the enter/dpad center key.

我們通常都是自己定義了一組數據集合,可以是array,可以是list,還可以是網絡傳過來的數據,這組數據是以下拉菜單的方式根據我們輸入的關鍵字來匹配我們數據集合中滿足條件的數據項,通過下拉菜單,我們可以enter來選中我們需要的數據,而為AutoCompleteTextView這個控件提供內容的就是我們的 Adapter ,這個叫做適配器,Adapter這個類的作用就是在我們的Data和我們的View之間架設一座橋梁,我們將我們的數據放置到一個Adapter當中,然后通過指定我們對這些數據的布局方式,再將這個Adapter賦給我們的View。

Adapter是一個接口,其擁有許多的實現類,例如:

android.widget.Adapter
Known Indirect Subclasses:
ArrayAdapter
<T>, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, SpinnerAdapter, WrapperListAdapter

我們看到,Android為我們提供了許多的Adapter,這是因為我們的數據可能來自不同的途徑,而且對於一些特殊的控件,例如Spinner,我們也要有指定的SpinnerAdapter才行,接下來我們就通過一個例子來實現我們的 AutoCompleteTextView 文本提示功能:

首先我們在我們的布局文件中定義一個 <AutoCompleteTextView>標簽,例如:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="所在國家:" />

    <AutoCompleteTextView
        android:id="@+id/auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/country"/>
    
</RelativeLayout>

接下來我們看看Activity里面的內容:

public class AutoCompleteActivity extends Activity
{
    private AutoCompleteTextView auto;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.auto_complete);
    
        auto = (AutoCompleteTextView)findViewById(R.id.auto);
        
        List<String> countries = new ArrayList<String>();
        countries.add("Afghanistan");
        countries.add("Albania");
        countries.add("Algeria");
        countries.add("American");
        countries.add("Andorra");
        countries.add("Anguilla");
        countries.add("Angola");
        countries.add("Antarctica");
        countries.add("China");
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
        
        auto.setAdapter(adapter);
        
    }
}

我們看到,在Activity里面我們定義了一個ArrayAdapter這個類,其有許多的構造方法,我們來看看我們用的這個:

public ArrayAdapter (Context context, int resource, List<T> objects)

Parameters
context    The current context.
resource    The resource ID for a layout file containing a TextView to use when instantiating views.
objects    The objects to represent in the ListView.

第一個參數Context類型的對象,是我們的上下文對象,我們的Activity是Context類的子類,所以可以將當前的這個Activity傳進去,第二個參數是一個包含了TextView控件的布局文件的ID,當Adapter加載的時候,就會將我們的數據集合的每一個數據(item)綁定為我們這個布局文件中的每一個TextView控件上,android系統本身給我們提供了許多的默認的布局文件,我們這里使用的是  android.R.layout.simple_list_item_1 這個布局文件(this is a layout provided by Android that provides a standard appearance for text in a list),第三個參數就是我們的數據集合,這里我們傳入一個List進去,最后通過setAdapter(adapter)方法將其綁定到我們的AutoCompleteTextView控件上即可。

對於我們的數據集,我們除了可以在Activity中聲明一個List或者是Array以為,我們還可以寫在android的資源文件中,通過資源文件來得到我們的數據集,例如,我們在

res-->values-->strings.xml 這個文件中指定我們的數據集:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Android_01</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    
    <string-array name="countries">
        <item>Bahrain</item>
        <item>Bangladesh</item>
        <item>Belarus</item>
        <item>Belize</item>
        <item>Brazil</item>
        <item>Cameroon</item>
        <item>Japan</item>
        <item>Hongkong</item>
        <item>Greece</item>
        <item>Germany</item>
        <item>France</item>
        <item>Djibouti</item>
        <item>Denmark</item>
        <item>Canada</item>
    </string-array>

</resources>

然后在代碼中,我們可以通過下面這種方式來創建我們的Adapter對象:

String[] countries2 = getResources().getStringArray(R.array.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries2);
        

最后我們來看看效果圖:

當我們輸入兩個字母時,就會有內容提示了,我們可以選擇我們需要的選項,點擊enter即可。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM