Lập trình Android: Tạo bảng nhắc nhở docx

3 380 0
Lập trình Android: Tạo bảng nhắc nhở docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

Trung tâm Tin học – ĐH KHTN AutoCompleteTextView Trong bài viết này mình sẽ chia sẽ với các bạn cách thức làm cho khung EditText hiện các từ gần đúng với từ khóa mà mình đã nhập vào. Ví dụ như trong 1 ứng dụng từ điển, các bạn chỉ cần nhập ký tự “app” thì sẽ có các dòng nhắc nhở bên dưới dạng như “Apple”,”Application”… Hay cách thức hoạt động của danh bạ trên hầu hết các điện thoại hiện nay. Đây là 1 ứng dụng đơn giản nên các bạn chỉ cần làm theo các bước sau: + Các bạn tạo 1 Project như sau: Project name: NhacNho Build Target: Android 2.3.3 Application name: NhacNho Package name: com.dac.NhacNho Create Activity: NhacNhoActivity + Tiếp theo các bạn thiết kế giao diện như sau: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/selection" /> <AutoCompleteTextView android:id="@+id/edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:completionThreshold="1"/> </LinearLayout> Ở phần này các bạn cần lưu ý thuộc tính completionThreshold của AutoCompleteTextView. Thuộc tính này quy định số ký tự sẽ hiện ra nhắc nhở (Ví dụ như nếu bạn cho giá trị là 3 thì khi nhập vào “ap” thì ko có dòng nhắc nhở nào hiện ra vì bạn chỉ nhập vào 2 ký tự). Tùy theo các cơ sở dữ liệu và ý tưởng lập trình khác nhau thì các bạn sẽ chọn giá trị của thuộc tính đó khác nhau. + Và cuối cùng bạn viết code xử lý trong file NhacNhoActivity.java như sau: Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN package com.dac.NhacNho; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.TextView; public class NhacNhoActivity extends Activity implements TextWatcher { TextView selection; AutoCompleteTextView edit; String[] items={"Ngoc Anh", "Trang XH", "Hong", "Vo Yeu", "Dung DD", "Vinaphone", "Hoan Long", "Phong Vu", "CSKH", "Minh", "Giang TH", "Trung Tam", "Tuoi tre", "Cong An", "Phuong Doi", "Tran Tien", "Dat TT", "Hoang Dac", "Trung", "Yen", "Cua Hang", "Nha", "Co Giao", "Thay Giao", "Sinh Vien"}; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); selection=(TextView)findViewById(R.id.selection); edit=(AutoCompleteTextView)findViewById(R.id.edit); edit.addTextChangedListener((TextWatcher) this); edit.setAdapter(new ArrayAdapter<String>(this , android.R.layout.simple_dropdown_item_1line, items)); } public void onTextChanged(CharSequence s, int start, int before, int count) { selection.setText(edit.getText()); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { // needed for interface, but not used } public void afterTextChanged(Editable s) { // needed for interface, but not used } } Ở phần này vì không có cơ sở dữ liệu nên mình tạo 1 mảng items chứa 1 số tên danh bạ để làm cơ sở dữ liệu. Các bạn có thể sử dụng cơ sở dữ liệu hay các mảng khác. Sau khi hoàn tất code các bạn debug và test thử, kết quả sẽ tương tự trong hình: Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN Vậy khi ta nhập vào ký tự “d” thì nó sẽ hiện ra các từ bắt đầu với “d” để các bạn chọn. Các bạn có thể nhập vào 1 số ký tự nữa để lọc cho chính xác hơn. Như vậy các bạn ko cần nhập hết chuổi mà chỉ cần nhập 1 số ký tự và chọn từ phù hợp. Mọi ý kiến đóng góp các bạn vui lòn gữi bài vào forum trang web www.laptrinhdidong.vn . Rất mong nhận đc sự phản hồi của các bạn Lập trình Android – http://laptrinhdidong.vn Page 3 . hiện ra nhắc nhở (Ví dụ như nếu bạn cho giá trị là 3 thì khi nhập vào “ap” thì ko có dòng nhắc nhở nào hiện ra vì bạn chỉ nhập vào 2 ký tự). Tùy theo các cơ sở dữ liệu và ý tưởng lập trình khác. android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/selection" /> <AutoCompleteTextView android:id="@+id/edit" . xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent"

Ngày đăng: 08/08/2014, 01:22

Tài liệu cùng người dùng

Tài liệu liên quan