安卓之高级控件Spinner下拉框

时间:2022-04-27 22:45:30

Spinner控件下拉框运用在地址的二级联动中


效果如图:

安卓之高级控件Spinner下拉框


XML关键代码:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.   
  7.     <Spinner  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:id="@+id/s_main_province"></Spinner>  
  11.     <Spinner  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:id="@+id/s_main_city"></Spinner>  
  15.   
  16.   
  17. </LinearLayout>  

Java关键代码:

[html] view plain copy
  1. public class MainActivity extends AppCompatActivity {  
  2.   
  3.     private Spinner s_main_province;  
  4.     private Spinner s_main_city;  
  5.     private List<String> province;  
  6.     int index=0;  
  7.     private SimpleAdapter sadapter;  
  8.     private ArrayAdapter cityAdapter;  
  9.   
  10.   
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.   
  16.         s_main_province = (Spinner) findViewById(R.id.s_main_province);  
  17.         s_main_city = (Spinner) findViewById(R.id.s_main_city);  
  18.         //设置省份下拉列表的适配器  
  19.         province=new ArrayList<>();  
  20.         province.add("湖南省")  ;  
  21.         province.add("湖北省");  
  22.         province.add("甘肃省");  
  23.         //设置图片集合  
  24.         List<Integer> images=new ArrayList<>();  
  25.         images.add(R.drawable.bird);  
  26.         images.add(R.drawable.cat);  
  27.         images.add(R.drawable.cow);  
  28.   
  29.          List<Map<String,Object>> lists=new ArrayList<>();  
  30.         for (int i = 0; i < province.size(); i++) {  
  31.             Map<String,Object> map=new HashMap<>();  
  32.            map.put("province",province.get(i));  
  33.             map.put("images",images.get(i));  
  34.             lists.add(map);  
  35.         }  
  36.   
  37.         //设置市的集合  
  38.         String city1[]={"长沙市","湘潭市","衡阳市"};  
  39.         String city2[]={"武汉市","黄石市","襄阳市"};  
  40.         String city3[]={"兰州","白银市","平凉市"};  
  41.   
  42.       final String str[][]={city1,city2,city3};  
  43.         //省域市集合  
  44.        final Map<String ,String[]> cityLists=new ArrayMap<>();  
  45.         for (int i = 0; i <province.size() ; i++) {  
  46.             cityLists.put(province.get(i),str[i]);  
  47.         }  
  48.         sadapter = new SimpleAdapter(this,lists,android.R.layout.activity_list_item,new String[]{"province","images"},new int[]{android.R.id.text1,android.R.id.icon});  
  49.         //给下拉列表设置适配器  
  50.         s_main_province.setAdapter(sadapter);  
  51.        s_main_province.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
  52.            @Override  
  53.            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
  54.                //给市的下拉框设置配置器  
  55.                index=position;  
  56.                cityAdapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,cityLists.get(province.get(index)));  
  57.                s_main_city.setAdapter(cityAdapter);  
  58.   
  59.            }  
  60.   
  61.            @Override  
  62.            public void onNothingSelected(AdapterView<?> parent) {  
  63.   
  64.            }  
  65.        });  
  66.   
  67.   
  68.   
  69.   
  70.   
  71.   
  72.     }