2011年10月13日星期四

含多項選擇(multiple choice)的列表視圖(ListView)

通過 setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) 以及 android.R.layout.simple_list_item_multiple_choice 佈局, 可以設定列表視圖(ListView)為多選項.

實例:

含多項選擇(multiple choice)的列表視圖(ListView)

package com.MultiChoiceList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MultiChoiceList extends Activity {

ListView list;

String[] options = {"Option 1", "Option 2", "Option 3",
"Option 4", "Option 5", "Option 6"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView)findViewById(R.id.list);

ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
options);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

list.setAdapter(adapter);
}
}


<?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"
/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>



相關文章:
- 讀取多選項列表視圖(Read multiple choice ListView)



沒有留言:

發佈留言