首先看一下xml文件:
实现:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Spinner spinner1;
private Spinner spinner2;
private Spinner spinner3;
private ArrayAdapter arrayAdapter1;
private ArrayAdapter arrayAdapter2;
private ArrayAdapter arrayAdapter3;
private int position1 = 0;
private int position2 = 0;
//省级选项值
private String[] province = new String[]{"北京", "上海", "天津", "广东"};
//地级选项值
private String[][] city = new String[][]
{
{"东城区", "西城区", "崇文区", "宣武区", "朝阳区", "海淀区", "丰台区", "石景山区", "门头沟区",
"房山区", "通州区", "顺义区", "大兴区", "昌平区", "平谷区", "怀柔区", "密云县",
"延庆县"},
{"长宁区", "静安区", "普陀区", "闸北区", "虹口区"},
{"和平区", "河东区", "河西区", "南开区", "河北区", "红桥区", "塘沽区", "汉沽区", "大港区",
"东丽区"},
{"广州", "深圳", "韶关", "珠海", "汕头", "佛山", "湛江", "肇庆", "江门", "茂名", "惠州", "梅州",
"汕尾", "河源", "阳江", "清远", "东莞", "中山", "潮州", "揭阳", "云浮"
}
};
//县级选项值
private String[][][] county = new String[][][]
{
{ //北京
{"无"}
},
{ //上海
{"无"}
},
{ //天津
{"无"}
},
{ //广东
{"海珠区", "荔湾区", "越秀区", "白云区", "萝岗区", "天河区", "黄埔区", "花都区", "从化市", "增城市", "番禺区", "南沙区"}, //广州
{"宝安区", "福田区", "龙岗区", "罗湖区", "南山区", "盐田区"}, //深圳
{"武江区", "浈江区", "曲江区", "乐昌市", "南雄市", "始兴县", "仁化县", "翁源县", "新丰县", "乳源县"},//韶关
{"无"}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner3 = (Spinner) findViewById(R.id.spinner3);
setSupportActionBar(toolbar);
arrayAdapter1 = new ArrayAdapter(this, R.layout.item_view, R.id.text, province);
arrayAdapter2 = new ArrayAdapter(this, R.layout.item_view, R.id.text, city[0]);
arrayAdapter3 = new ArrayAdapter(this, R.layout.item_view, R.id.text, county[0][0]);
spinner1.setAdapter(arrayAdapter1);
spinner1.setSelection(0);
spinner2.setAdapter(arrayAdapter2);
spinner2.setSelection(0);
spinner3.setAdapter(arrayAdapter3);
spinner3.setSelection(0);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> adapterView, View view, int i, long l) {
position1 = i;
arrayAdapter2 = new ArrayAdapter(MainActivity.this, R.layout.item_view, R.id.text, city[position1]);
spinner2.setAdapter(arrayAdapter2);
}
@Override
public void onNothingSelected(AdapterView> adapterView) {
}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> adapterView, View view, int i, long l) {
if(position1 > 2 && i < 3) {
position2 = i;
}else if(position1 > 2 && i > 2){
position2 = 3;
}
arrayAdapter3 = new ArrayAdapter(MainActivity.this,R.layout.item_view,R.id.text,county[position1][position2]);
spinner3.setAdapter(arrayAdapter3);
}
@Override
public void onNothingSelected(AdapterView> adapterView) {
}
});
spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> adapterView, View view, int i, long l) {
}
@Override
public void onNothingSelected(AdapterView> adapterView) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_wx:
Toast.makeText(MainActivity.this,"微信",Toast.LENGTH_SHORT).show();
break;
case R.id.action_wb:
Toast.makeText(MainActivity.this,"微博",Toast.LENGTH_SHORT).show();
break;
case R.id.action_mm:
Toast.makeText(MainActivity.this,"陌陌",Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}