通过Spinner视图实现二级联动功能(省市)

1.确定我需要的数据放到res/values/arrays.xml文件(定义数组)

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="province">
<item>-省份-</item>
<item>河北省</item>
<item>山西省</item> 
<item>北京市</item> 
</string-array>
<string-array name="hb">
<item>-城市-</item>
<item>承德市</item>
<item>邯郸市</item> 
<item>廊坊市</item> 
</string-array>
<string-array name="bj">
<item>-城市-</item>
<item>海淀区</item>
<item>朝阳区</item> 
<item>崇文区</item> 
</string-array>
<string-array name="shx">
<item>-城市-</item>
<item>大同市</item>
<item>临汾市</item>
</string-array>
</resources>

2.编写xml界面res/values/main.xml文件(编写界面)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"

<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/province"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/province"
android:id="@+id/city"
/>
</RelativeLayout>
</LinearLayout>

3.在Activity中实现省市联动功能

/**
* 通过Spinner实现省市(两级)联动
@author 黑色天空
*/
public class MainActivity extends Activity {
private TextView textView ;
private Spinner province;
private Spinner city;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//String[] provinces = new String[]{"-省份-","河北省","山西省","北京"};
textView = (TextView)this.findViewById(R.id.textView);
province = (Spinner)this.findViewById(R.id.province);
city = (Spinner)this.findViewById(R.id.city);

//(处理省的显示)
//将可选内容与ArrayAdapter的连接(从资源数组文件中获取数据)
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.province, android.R.layout.simple_spinner_item);
//new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, provinces);
//设置下拉列表的风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

//将数据绑定到Spinner视图上
province.setAdapter(adapter);

//添加条目被选中监听器
province.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
//parent既是province对象
Spinner spinner = (Spinner)parent;
String pro = (String)spinner.getItemAtPosition(position);

//(处理省的市的显示)
//将默认值与ArrayAdapter连接(从资源数组文件中获取数据)
ArrayAdapter<CharSequence> cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.citydefault, android.R.layout.simple_spinner_item);
//new ArrayAdapter<CharSequence>
// (MainActivity.this,android.R.layout.simple_spinner_item, cities);
//获取所在省含有哪些市(从资源数组文件中获取数据)
if(pro.equals("河北省")){

cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.hb, android.R.layout.simple_spinner_item);
}else if(pro.equals("北京市")){

cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.bj, android.R.layout.simple_spinner_item);
}else if(pro.equals("山西省")){

cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.shx, android.R.layout.simple_spinner_item);
}
//绑定数据到Spinner(City)上
city.setAdapter(cityAdapter);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

});
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值