AutoCompleteTextView trong Android
AutoCompleteTextView trong Android hỗ trợ cho người dùng những gợi ý liên quan khi bạn nhập vào trường EditText. Những gợi ý đó sẽ được hiển thị trong một danh sách thả xuống từ đó người dùng có thể chọn một item để thay thế cho nội dung của mình vừa nhập vào.
Android AutoCompleteTextView là lớp con của lớp EditText. Do đó nó kế thừa tất cả các thuộc tính của EditText
AutoCompleteTextView code trong XML
<AutoCompleteTextView android:id="@+id/simpleAutoCompleteTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is an AutoCompleteTextView"/>
Để cung cấp dữ liệu cho AutoCompleteTextView trong Android chúng ta dùng đối tượng Adapter
Android Adapter (Tạm dịch là bộ tiếp nối) là một cầu nối giữa các View (ví dụ như ListView) và các dữ liệu cơ bản cho View đó. Mộ tAdapter quản lý dữ liệu và ghép nối với các dòng riêng lẻ (ListItem) của View.
Cú pháp
ArrayAdapter adapter = new ArrayAdapter<String>(this, int resource,T[] objects);
- Context context : Đây là Activity hiện tại thông thường dùng từ khóa this
- int resource : Mẫu layout cần hiển thị
- T[] objects: Nguồn dữ liệu
Lấy giá trị từ AutoCompleteTextView trong Java Class:
Code ví dụ sau lấy giá trị từ AutoCompleteTextView
AutoCompleteTextView simpleAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.simpleAutoCompleteTextView); String AutoCompleteTextViewValue = simpleAutoCompleteTextView.getText().toString();
Thuộc tính thường dùng của AutoCompleteTextView
Bây giờ chúng xem một số thuộc tính hay sử dụng trong AutoCompleteTextView trong tập tin XML
1. android:id: Là thuộc tính duy nhất của AutoCompleteTextView.
<AutoCompleteTextView android:id="@+id/simpleAutoCompleteTextView" android:layout_height="wrap_content" android:layout_width="match_parent"/>
Dựa vào Id ta sẽ lấy được control theo đúng Id này, xem code bên dưới để biết cách lấy control theo Id:
AutoCompleteTextView simpleAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.simpleAutoCompleteTextView);
2. android:text: thuộc tính này thiết lập nội dung mặc định cho 1 AutoCompleteTextView. Chúng ta có thể thiết lập trong tập tin xml hoặc code java. Ví dụ sau chúng ta thiết lập nội dung "Aspx" cho AutoCompleteTextView
<AutoCompleteTextView android:id="@+id/simpleAutoCompleteTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Apsx"/><!--display text "Aspx"-->
3. android:gravity: Thuộc tính này thường sử dụng để canh nội dung trên AutoCompleteTextView: left, right, center, top, bottom, center_vertical, center_horizontal
<AutoCompleteTextView android:id="@+id/simpleAutoCompleteTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Apsx" android:gravity="right"/><!--Thiết lập text bên phải -->
Thiết lập thuộc tính android:text trong java code
*Add in Oncreate() funtion after setContentView()*/ AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView); //display text Country autoCompleteTextView.setText("Aspx");
4. android:hint: Thuộc tính hint để hiển thị thông tin gợi ý trong vùng nhập dữ liệu khi bạn chưa nhập bất kỳ dữ liệu nào vào, chỉ cần có dữ liệu là phần hint sẽ tự động mất đi.
<AutoCompleteTextView android:id="@+id/simpleAutoCompleteTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:hint="Nhập ngôn ngữ "/><!--Thiết lập hint -->
Thiết lập thuộc tính android:hint trong java code
/*Add in Oncreate() funtion after setContentView()*/ AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView); autoCompleteTextView.setHint("Nhập ngôn ngữ");//display hint
5. android:textColor: Thuộc tính này dùng xác định màu chữ, dạng màu chữ: “#argb”, “#rgb”, “#rrggbb”, hoặc “#aarrggbb”. Vi dụ sau set chữ trong AutoCompleteTextView màu đỏ.
<AutoCompleteTextView android:id="@+id/simpleAutoCompleteTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Aspx" android:textColor="#f00"/><!--Thiết lập thuộc tính màu đỏ-->
Thiết lập thuộc tính android:textColor trong java code
/*Add in Oncreate() funtion after setContentView()*/ AutoCompleteTextView simpleAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView); //green color for displayed hint simpleAutoCompleteTextView.setHintTextColor(Color.green(0));
6. android:textSize: Thuộc tính textSize xác định kích thước văn bản của AutoCompleteTextView . Chúng ta có thể thiết lập kích thước văn bản theo sp(scale independent pixel) hoặc dp(density pixel). Trong ví dụ này chúng ta xác định kich thước cho văn bản là 40sp.
<AutoCompleteTextView android:id="@+id/simpleAutoCompleteTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Aspx" android:textSize="40sp" /><!--thiết lập kícch thước văn bản-->
Thiết lập thuộc tính android:textSize trong java code
/*Add in Oncreate() funtion after setContentView()*/ AutoCompleteTextView simpleAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView); //set the text size simpleAutoCompleteTextView.setTextSize(40);
7. android:textStyle: Thuộc tính xác định loại văn bản của AutoCompleteTextView, thông thường có các loại văn bản:bold, italic và normal. Nếu chúng ta muốn sử nhiều hơn một loại văn bản thì phải thêm phép toán hoặc "|" vào giữa các loại văn bản:
<AutoCompleteTextView android:id="@+id/simpleAutoCompleteTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Aspx" android:textSize="40sp" android:textStyle="bold|italic"/><!--bold and italic text style-->
8. androidbackground: Thuộc tính này xác định màu nền cho AutoCompleteTextView.
9. android:padding: Thuộc tính này xác định khoảng cách từ đường viền của AutoCompleteTextView với nội dung nó chứa: left, right, top or bottom. Cũng ví dụ trên bây giờ chúng ta xác định padding=15dp từ mọi phía của AutoCompleteTextView.
<AutoCompleteTextView android:id="@+id/simpleAutoCompleteTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#000" android:hint="Enter Your Name Here" android:padding="15dp" android:textColorHint="#fff" android:textStyle="bold|italic" />
Thiết lập thuộc tính Background trong Java class:
/*Add in Oncreate() funtion after setContentView()*/ AutoCompleteTextView simpleAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView); simpleAutoCompleteTextView.setBackgroundColor(Color.BLACK);//set black background color
Ví dụ: Trong ví dụ này chúng ta sẽ làm ứng dụn gồm có 1 AutoCompleteTextView và 1 TextView. Tiến hành tạo project, vào thư mục res /layout -> activity_main.xml thiết kế giao diện sau:
Bước 1: Tạo một project tên là AutoCompleteTextView : File->New->Android Application Project điền các thông tin ->Next ->Finish
Bước 2: Mở res -> layout -> xml (hoặc) activity_main.xml và thêm code, chúng ta sẽ tạo các đối tượng AutoCompleteTextView và TextView trong Relative Layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="hiepsiit.com.autocompletetextview.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="15dp" android:text="Chọn ngôn ngữ lập trình bạn thích" /> <AutoCompleteTextView android:id="@+id/autoCompleteTextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginLeft="36dp" android:layout_marginTop="17dp" android:ems="10" android:text=""> <requestFocus /> </AutoCompleteTextView> </RelativeLayout>
Bước 3: Mở app -> src ->MainActivity.java và thêm code.
Khai báo nguồn dữ liệu
String[] language ={"C","C++","Java",".NET","iPhone","Android","ASP.NET","PHP"};
Trong phương thức onCreate(Bundle savedInstanceState) thêm code :
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.autoCompleteTextView1); actv.setThreshold(1);//will start working from first character actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView actv.setTextColor(Color.RED); }
Toàn bộ code ứng dụng
package hiepsiit.com.autocompletetextview; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class MainActivity extends Activity { 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.autoCompleteTextView1); actv.setThreshold(1);//will start working from first character actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView actv.setTextColor(Color.RED); } }
Ứng dụng này được phát triển bởi adt bundle, android 4.2 sử dụng minimum sdk 11 and target sdk 21.
Kết quả khi chạy ứng dụng:
Sau đó nhập vào chữ "a":
Cập nhật công nghệ từ Youtube tại link: https://www.youtube.com/channel/UCOxeYcvZPGf-mGLYSl_1LuA/videos
Để tham gia khóa học công nghệ truy cập link: http://thuvien.hocviendaotao.com
Mọi hỗ trợ về công nghệ email: dinhanhtuan68@gmail.com