Android如何实现省市区三级联动-创新互联
小编给大家分享一下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 ArrayAdapterprovinceAdapter; 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如何实现省市区三级联动”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!
当前名称:Android如何实现省市区三级联动-创新互联
文章分享:http://scgulin.cn/article/gjpgd.html