Professional Documents
Culture Documents
NỘI DUNG
Sử dụng Visual Studio, tạo các ứng dụng Xamarin Android sau đây:
Spinner là đối tượng giao diện trong Android cho phép người dùng có thể chọn từ một
danh sách có sẵn.
Ví dụ sau đây sẽ tạo một Spinner bao gồm danh sách tên của các hành tinh. Khi người
dùng chọn một mục (item) nào đó, chương trình sẽ bắt sự kiện và hiển thị thông tin về
item đã được chọn trên Spinner.
Bài tập thực hành Phát triển ứng dụng trên thiết bị di động 2
Lưu ý:
Thuộc tính android:text của TextView và android:prompt của Spinner đều tham chiếu đến
thẻ có tên planet_prompt trong file tài nguyên Strings.xml.
namespace SpinnerSample
{
Bài tập thực hành Phát triển ứng dụng trên thiết bị di động 3
spinner = FindViewById<Spinner>(Resource.Id.spinner);
spinner.Adapter = adapter;
spinner.ItemSelected += Spinner_ItemSelected;
File này được dùng để định nghĩa cách một item được hiển thị như thế nào trên ListView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
namespace ListViewSimple
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher =
true)]
public class MainActivity : AppCompatActivity
{
ListView listView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
listView = FindViewById<ListView>(Resource.Id.listView);
string[] countries = new string[] {
"Afghanistan","Albania","Algeria","American Samoa","Andorra",
"Angola","Anguilla","Antarctica","Antigua and Barbuda","Argentina",
"Armenia","Aruba","Australia","Austria","Azerbaijan"
};
Nếu muốn sử dụng dữ liệu được load từ file Resources/Values/Strings.xml. Ta thực hiện như sau:
Sử lại cách khai báo biến adapter, lúc này ta sẽ nạp dữ liệu từ file Strings.XML vào
biến:
Resource.Array.countries_array, Resource.Layout.list_item);