① 如何使用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);