public class MainActivity extends Activity { private List<String> list = new ArrayList<String>(); private TextView myTextView; private Spinner mySpinner; private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 第一步:添加一个下拉列表项的list,这里添加的项就是下拉列表的菜单项 list.add("北京"); list.add("上海"); list.add("广州"); list.add("深圳"); myTextView = (TextView) findViewById(R.id.textView); mySpinner = (Spinner) findViewById(R.id.spinner); // 第二步:为下拉列表定义一个适配器,这里就用到里前面定义的list。 adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); // 第三步:为适配器设置下拉列表下拉时的菜单样式。 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 第四步:将适配器添加到下拉列表上 mySpinner.setAdapter(adapter); // 第五步:为下拉列表设置各种事件的响应,这个事响应菜单被选中 mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { /* 将所选mySpinner 的值带入myTextView 中 */ myTextView.setText("您选择的是:" + adapter.getItem(arg2)); } public void onNothingSelected(AdapterView<?> arg0) { myTextView.setText("NONE"); } }); } }
自定义Spinner下拉样式
public class MainActivity extends Activity { private TextView myTextView; private Spinner mySpinner; private SimpleAdapter adapter; private List<Map<String, Object>> dataList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 第一步:添加一个下拉列表项的list,这里添加的项就是下拉列表的菜单项 dataList = new ArrayList<Map<String, Object>>(); getData(); myTextView = (TextView) findViewById(R.id.textView); mySpinner = (Spinner) findViewById(R.id.spinner); // 第二步:为下拉列表定义一个适配器,这里就用到里前面定义的list。 adapter=new SimpleAdapter(this, dataList, R.layout.item, new String[]{"image","text"}, new int[]{R.id.image,R.id.text}); // 第三步:为适配器设置下拉列表下拉时的菜单样式。 adapter.setDropDownViewResource(R.layout.item); // 第四步:将适配器添加到下拉列表上 mySpinner.setAdapter(adapter); // 第五步:为下拉列表设置各种事件的响应,这个事响应菜单被选中 mySpinner .setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { /* 将所选mySpinner 的值带入myTextView 中 */ myTextView.setText("您选择的是:" + adapter.getItem(arg2)); } public void onNothingSelected(AdapterView<?> arg0) { myTextView.setText("NONE"); } }); } private void getData() { Map<String, Object> map = new HashMap<String, Object>(); map.put("image", R.drawable.ic_launcher); map.put("text", "北京"); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("image", R.drawable.ic_launcher); map2.put("text", "上海"); Map<String, Object> map3 = new HashMap<String, Object>(); map3.put("image", R.drawable.ic_launcher); map3.put("text", "广州"); Map<String, Object> map4 = new HashMap<String, Object>(); map4.put("image", R.drawable.ic_launcher); map4.put("text", "深圳"); dataList.add(map); dataList.add(map2); dataList.add(map3); dataList.add(map4); } }
时间: 2024-10-09 17:55:23