Lập trình Android: Tạo và sử dụng Snipper pot

4 370 1
Lập trình Android: Tạo và sử dụng Snipper pot

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

Thông tin tài liệu

Trung tâm Tin học – ĐH KHTN Tạo Spinner Sau đây mình sẽ demo Spinner chọn tháng trong năm như sau: 1/ Tạo 1 project như sau: Project name: SpinnerExample Build Target: Android 2.3.3 Application name: SpinnerExample Package name: org.example.SpinnerExample Create Activity: SpinnerExample 2/ Tiếp theo các bạn code file main.xml như sau: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="10dip" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="Select Month" /> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:prompt="@string/mnth_picker" /> </LinearLayout> 3/ Các bạn tạo nội dung và các Item cho Snipper trong file strings.xml như sau: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, SpinnerExample!</string> <string name="app_name">SpinnerExample</string> <string name="mnth_picker">Select a Month</string> <string-array name="mnth_array"> Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN <item>January</item> <item>February</item> <item>March</item> <item>April</item> <item>May</item> <item>June</item> <item>July</item> <item>August</item> <item>September</item> <item>Octomber</item> <item>November</item> <item>December</item> </string-array> </resources> 4/ Cuối cùng các bạn thêm code vào phần SpinnerExample.java như sau: package org.example.SpinnerExample; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.Spinner; public class SpinnerExample extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.mnth_array, android.R.layout.simple_spinner_item); Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); } } Cuối cùng các bạn debug project sẽ đc kết quả tương tự: Ban đầu: Khi bấm vào Spinner thì : Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN Bạn nào có ý kiến đóng góp vui lòng post bài vào forum trang web www.laptrinhdidong.vn . Mình sẽ theo dõi forum thường xuyên ^^ Lập trình Android – http://laptrinhdidong.vn Page 4 . xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="10dip" android:layout_width="fill_parent" android:layout_height="wrap_content" . android:layout_height="wrap_content" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="Select. Month" /> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:prompt="@string/mnth_picker"

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

Từ khóa liên quan

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

Tài liệu liên quan