Android 除了提供CheckBox與RadioButton的物件之外,還提供另一種更為方便以及多樣化的物件ListView
小試身手小範例:
java檔
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
publicclass TestExam002 extends Activity {
/** Called when the activity is first created. */
@SuppressWarnings("unchecked")
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] Data = {"紅茶", "奶茶", "綠茶"};
ListView listView = new ListView(this);
/*設定ListView選擇的方式,
多選:ListView.CHOICE_MODE_MULTIPLE
單選:ListView.CHOICE_MODE_SINGLE*/
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//陣列接收器
//RadioButton樣式:android.R.layout.simple_list_item_single_choice
//CheckButton樣式:android.R.layout.simple_list_item_multiple_choice
ArrayAdapter vArrayData = new ArrayAdapter(this, android.R.layout.simple_list_item_single_choice, Data);
//設adapter為array的接收器,做為選項來源
listView.setAdapter(vArrayData);
//設ListView為ContentView
setContentView(listView);
}
}
留言列表