[Tự học lập trình Android] Bài 12: Tìm hiểu về Spinner trong Android

Tìm hiểu về Spinner trong Android

- Spinner tương tự như ComboBox trong C#, tương tự như JComboBox trong Java.
- Nếu bạn đã hiểu về ListView thì việc hiểu Spinner cũng không có gì là khó.
- Cách đổ dữ liệu lên Spinner là giống như đổ lên ListView, nó chỉ khác một chỗ duy nhất trong ArrayAdapter đó là ta phải gọi setDropDownViewResource.
- Ví dụ về Spinner:
Hình 1
- Kéo 2 control: TextView và Spinner vào ứng dụng (xem activity_spinner.xml)

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SpinnerActivity">

<TextView
android:id="@+id/selection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#007380"
android:hint="selected here"
android:textColor="#ff003c"/>

<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

- Ở đây ta đặt Id cho spinner là spinner1 (nhìn dòng lệnh 16).

- Coding SpinnerActivity.java:

importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.View;
importandroid.widget.AdapterView;
importandroid.widget.AdapterView.OnItemSelectedListener;
importandroid.widget.ArrayAdapter;
importandroid.widget.Spinner;
importandroid.widget.TextView;

publicclassSpinnerActivity extendsActivity {
//Tạo một mảng dữ liệu giả
String arr[]={ "Hàng điện tử", "Hàng hóa chất", "Hàng gia dụng"};
TextView selection;

@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
selection =(TextView) findViewById(R.id.selection);
//Lấy đối tượng Spinner ra
Spinner spin=(Spinner) findViewById(R.id.spinner1);
//Gán Data source (arr) vào Adapter
ArrayAdapter<String> adapter=newArrayAdapter<String>(this,android.R.layout.simple_spinner_item,arr);
//phải gọi lệnh này để hiển thị danh sách cho Spinner
adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
//Thiết lập adapter cho Spinner
spin.setAdapter(adapter);
//thiết lập sự kiện chọn phần tử cho Spinner
spin.setOnItemSelectedListener(newMyProcessEvent());
}

//Class tạo sự kiện
privateclassMyProcessEvent implements
OnItemSelectedListener
{
//Khi có chọn lựa thì vào hàm này
publicvoidonItemSelected(AdapterView<?> arg0,View arg1,intarg2,longarg3) {
//arg2 là phần tử được chọn trong data source
selection.setText(arr[arg2]);
}
//Nếu không chọn gì cả
publicvoidonNothingSelected(AdapterView<?> arg0) {
selection.setText("");
}
}
}


- Bạn xem Tôi giải thích dưới này:
Hình 2
- android.R.layout.simple_spinner_item dùng để hiển thị phần tử bạn chọn lên spinner. Tương tự như trong ListView bạn có thể custom lại

- Dòng lệnh dưới: android.R.layout.simple_list_item_single_choice để hiện thị danh sách các phần tử trong Spinner khi bạn nhấn vào xem Spinner. Gọi hàm setDropDownViewResource nếu không nó sẽ lỗi chương trình khi bạn nhấn vào xem. Bạn có thể dùng layout khác nhau, chẳng hạn bạn có thể thay thế bằng : android.R.layout.simple_spinner_dropdown_item

Tham khảo: duythanhcse
-----------------------------------------

*******

Một số tài liệu và khoá học bổ ích dành cho bạn: 

# Giáo trình: Lập Trình Android [Click để xem]

# Khoá học online:  Lập trình Android toàn tập [Click để xem]

-----------------------------------------
Xem thêm bài và ví dụ khác:

 

Categories

AI (13) AI programming (1) ASP (1) Android (31) App Honeygain (4) Assembly (17) Biểu diễn thuật toán (1) Bubble-Sort (1) Bài giảng (2) Bài giảng lập trình C và Cpp (21) Bài viết hay (104) Bản đồ tư duy (1) C Plus Plus (103) C/C++ (16) CDSL phân tán (1) CSS (2) Cơ sở dữ liệu (11) Danh ngôn lập trình (1) Datamining (4) Genetic Algorithm (1) Giáo trình (2) Giải thuật tiến hóa - thuật toán di truyền (2) Google App Engine (2) Góc học tập (34) HTML (1) Hướng dẫn kiếm tiền online tại nhà (6) Hướng dẫn sử dụng Emu8086 (1) Học lập trình (131) Học lập trình C và CPP qua ví dụ (17) Java (54) Java Căn bản (6) JavaScript (5) Kỹ năng đọc hiệu quả (1) Kỹ thuật lập trình (16) Kỹ thuật đồ họa máy tính (10) Lý thuyết Cơ sở dữ liệu (2) Lý thuyết đồ thị (11) Lập trình Cơ sở dữ liệu (2) Lập trình Python (2) Lập trình căn bản (8) Lập trình hướng đối tượng với Java (7) Lập trình mobile (7) Lập trình mạng (6) Lập trình nhúng (1) Lập trình trí tuệ nhân tạo (2) ML (1) MMO (6) MS Access (1) Machine learning (2) Mạng máy tính (1) Mẹo tìm kiếm trên Google (1) Nghiên cứu khoa học (2) Ngôn ngữ lập trình (2) Những cuốn sách hay mà bạn nên đọc khi còn trẻ (1) Pascal (3) Phương pháp tính toán tối ưu (2) Phương pháp tối ưu (2) Quản lý dự án CNTT (1) SEO (1) SQL (5) Swift (9) Sách hay (4) Thiết kế Web (2) Thuật toán (51) Thuật toán Sắp Xếp -Sort (9) Thuật toán Tìm kiếm - Search (5) Thuật toán di truyền (4) Thực hành Android (2) Tin học văn phòng (5) Tiện ích máy tính (3) Toán rời rạc (13) Treo máy kiếm tiền (3) Trí tuệ nhân tạo (18) Tài liệu tham khảo (4) Tìm hiểu Blockchain (2) Tự học Android (3) Tự học Android qua ví dụ (1) Tự học JavaScript (1) Tự học lập trình (7) Tự học lập trình Android (17) Tự học lập trình C và CPP (14) Tự học lập trình java qua các ví dụ (8) XML (1) blockchain (2) bài giảng quản lý dự án CNTT (1) bài tập java (3) bài tập lập trình (4) cấu trúc dữ liệu giải thuật (15) hướng dẫn viết báo (1) học lập trình Java (11) học máy (5) hợp ngữ (8) lập trình viên (3) phưng pháp đơn hình (2) thuật toán AI (2) tài liệu CNTT miễn phí (3) tính toán tối ưu (1) tự học lập trình iOS (8) tự học lập trình python (1) ví dụ Assembly (1) Đại số gia tử và ứng dụng (1) Đồ họa (4)