① 如何使用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) {
}
});
}
}
② android如何动态更新二级联动spinner
spinner有一个setSelection(),将spinner设为全局变量后用spinner.setSelection(GestureConstant.FILE_GESTURE, false);就可以了。
贴一部分代码,不完整。
Activity
[java] view plain
Intent fileIntent = new Intent(FileManager.this,
CreateGestureActivity.class);
fileIntent.putExtra("file", "file");
startActivity(fileIntent);