Android实现省市区三级联动

针对AdapterView的拓展使用,Spinner实现省市区的三级联动,具体内容如下

其主要是通过使用Spinner的setOnItemSelectListener来实现。

代码示例:

activity_main.xml



  
  

    
    
    

  
  

MainActivity.java

public class MainActivity extends AppCompatActivity {
  private TextView show;
  private Spinner provinceSpinner;//省级
  private Spinner citySpinner;//市级
  private Spinner countySpinner;//区级

  private String[] province = new String[]{"北京","上海","天津","广东"};
  private String[][] city = new String[][]{
      {"东城区","西城区","崇文区","宣武区","朝阳区","海淀区","丰台区","石景山区","门头沟区",
          "房山区","通州区","顺义区","大兴区","昌平区","平谷区","怀柔区","密云县","延庆县"},
  {"长宁区","静安区","普陀区","闸北区","虹口区"},
  {"和平区","河东区","河西区","南开区","河北区","红桥区","塘沽区","汉沽区","大港区","东丽区"},
  {"广州","深圳","韶关"}};
  private String[][][] county = new String[][][]{{
      //北京
      {"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},
      {"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},},{
      //上海
      {"无"},{"无"},{"无"},{"无"},{"无"},},{
      //天津
      {"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},},{
      //广东
      {"海珠区","荔湾区","越秀区","白云区","萝岗区","天河区","黄浦区","花都区","从化市","增城市"
          ,"番禺区","南沙区"},
      {"宝安区","福田区","龙岗区","罗湖区","南山区","盐田区"},
      {"武江区","浈江区","曲江区","乐昌市","南雄市","始兴县","仁化县","翁源县","新丰县","乳源县"}
  }
  };

  //Spinner想要填充肯定需要适配器
  private ArrayAdapter provinceAdapter;
  private ArrayAdapter cityAdapter;
  private ArrayAdapter countyAdapter;
  private int provicePosition;//省级选中索引
  private int cityPosition;//市级选中索引

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initViews();//初始化
  }


  //控件初始化
  private void initViews() {
    provinceSpinner = (Spinner) findViewById(R.id.provinceSpinner);
    citySpinner = (Spinner) findViewById(R.id.citySpinner);
    countySpinner = (Spinner) findViewById(R.id.countySpinner);
    show = (TextView) findViewById(R.id.show);

    //初始化适配器及显示的内容
    provinceAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,province);
    provinceSpinner.setAdapter(provinceAdapter);


    //对省级下拉实现监听,市级下拉需要根据不同的省级显示不同内容。
    provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView parent, View view, int position, long id) {
        //需要在item选中省级的时候,动态的改变市级对应的显示
        cityAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,city[position]);
        //设置二级下拉列表当中选项内容适配器
        citySpinner.setAdapter(cityAdapter);
        //记录当前的省级索引位置,留给下面修改县级进行适配使用
        provicePosition = position;
      }

      @Override
      public void onNothingSelected(AdapterView parent) {

      }
    });


    //市级下拉监听
    citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView parent, View view, int position, long id) {
        //需要在item选中市级的时候,动态的改变县级对应的显示
        countyAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,county[provicePosition][position]);//首先确定是哪个省的。
        //设置二级下拉列表当中选项内容适配器
        countySpinner.setAdapter(countyAdapter);
        show.setText("选中的城市为:"+ province[provicePosition] + city[provicePosition][position]);
        cityPosition = position;
      }

      @Override
      public void onNothingSelected(AdapterView parent) {

      }
    });


    //区级的下拉
    countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView parent, View view, int position, long id) {
        show.setText("选中的城市为:"+ province[provicePosition] + city[provicePosition][cityPosition]
            + county[provicePosition][cityPosition][position]);
      }

      @Override
      public void onNothingSelected(AdapterView parent) {

      }
    });
  }


}

运行结果:

 Android实现省市区三级联动_第1张图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Android实现省市区三级联动)