符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
小编给大家分享一下Android如何实现省市区三级联动,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
古塔网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联公司于2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司。
针对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如何实现省市区三级联动”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!