如何使用Spinner实现省市的二级联动功能
2014-12-28 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517193
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
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) {
}
});
}
}
<?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) {
}
});
}
}
网易云信
2023-12-06 广告
2023-12-06 广告
UIkit是一套轻量级、模块化且易于使用的开源UI组件库,由YOOtheme团队开发。它提供了丰富的界面元素,包括按钮、表单、表格、对话框、滑块、下拉菜单、选项卡等等,适用于各种类型的网站和应用程序。UIkit还支持响应式设计,可以根据不同...
点击进入详情页
本回答由网易云信提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询