Hướng dẫn cách lập trình spinner Android

Thảo luận trong 'Mua Bán Tổng Hợp' bắt đầu bởi hongson1992, 22/8/16.

Lượt xem: 166

  1. Offline

    hongson1992 New Member

    Diễn Đàn SEO  Android là ngôn ngữ lập trình đang được rất nhiều lập trình viên trên thế giới và cả Việt Nam theo đuổi và làm việc với nó.Tại sao ngôn lập trình Androi lại là thu hút nhiều lập trình viên đến vậy? Bởi hệ điều hành Android đang được rất nhiều người dùng trên thế giới sử dụng.

    Hôm nay xin chia sẻ đến bạn đọc một kiến thức trong lập trình Android đó là control hay gọi là spinner Android.



    Giới thiệu về Spinner

    Spinner trong ngôn ngữ lập trình Android giúp người dùng có thể chọn 1 phần trong item từ danh sách lựa chọn kiểu drop-down có thể thả xuống khi người dùng chọn spinner.Trong lập trình C# thì loại này gọi là combobox hay là jcombobox trong ngôn ngữ java.Ở trạng thái mặc định thì Spinner được chọn mặc định giá trị đang được chọn.

    Sau đây là một bài demo hướng dẫn làm spinner trong Android mà Megafun.edu.vn muốn chia sẻ đến các bạn.Những bài học như này chỉ có trong các khóa học lập trình android nâng cao mà thôi.Nên các bạn chú ý và thức hành để hiệu nhé.

    Bước đầu tiên các bạn tạo ra mộ project tên là spinnerdemo

    Bước 2: Tạo ra một màn hình theo đoạn code sau:

    <LinearLayout xmlns:android="schemas.android.com/apk/res/android"

    xmlns:tools="schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:eek:rientation="vertical"

    tools:context=".MainActivity">

    <TextView

    android:textColor="[HASHTAG]#ff2796ff[/HASHTAG]"

    android:textSize="20sp"

    android:textstyle=""

    android:layout_marginTop="15dp"

    android:layout_gravity="center"

    android:text="Demo Spinner"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content" />

    <LinearLayout

    android:eek:rientation="horizontal"

    android:background="[HASHTAG]#ffd4ff9e[/HASHTAG]"

    android:layout_marginTop="10dp"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <TextView

    android:textstyle=""

    android:layout_marginTop="20dp"

    android:layout_marginLeft="15dp"

    android:textSize="15sp"

    android:gravity="center"

    android:text="Đăng ký khóa học:"

    android:layout_width="wrap_content"

    android:layout_height="50dp" />

    <Spinner

    android:background="[HASHTAG]#ffffbc4e[/HASHTAG]"

    android:id="@+id/spnKhoaHoc"

    android:layout_marginLeft="15dp"

    android:layout_marginTop="20dp"

    android:layout_width="fill_parent"

    android:layout_height="50dp"></Spinner>

    </LinearLayout>

    </LinearLayout>

    Bước 3 bạn vào thư mục MainActivity.java và làm theo đoạn code dưới đây:

    package devpro.com.spinnerdemo;

    import android.app.Activity;

    import android.support.v7.app.ActionBarActivity;

    import android.os.Bundle;

    import android.view.Menu;

    import android.view.MenuItem;

    import android.widget.ArrayAdapter;

    import android.widget.Spinner;




    import java.util.ArrayList;

    public class MainActivity extends Activity {

    Spinner spnKhoaHoc;

    ArrayList<String> lstData;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);




    //Thêm các phần tử vào danh sách cần hiển thị

    lstData = new ArrayList<>();

    lstData.add("Lập trình Android");

    lstData.add("Lập trình iOS");

    lstData.add("Lập trình Web");

    lstData.add("Lập trình Windows Phone");

    //Tìm spinner

    spnKhoaHoc = (Spinner) findViewById(R.id.spnKhoaHoc);

    //Khởi tạo adapter

    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, lstData);

    //SetDropDown cho adapter

    adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);

    //Gán adapter vào spinner

    spnKhoaHoc.setAdapter(adapter);

    }

    }

    Khi làm xong 2 đoạn code trên bạn có thể chạy chương trình của mình và sẽ hiện ra kết quả như thế này:

    [​IMG]​

    Trên đây là bài hướng dẫn của mình về cách lập trình spinner trong Android nếu các bạn gặp khó khăn có thể liên hệ với trung tâm đào tạo devpro.edu.vn để được tư vấn hỗ trợ.

    Gợi ý xem thêm:
     

Chia sẻ trang này

Đăng ký tài khoản VIP FORUM