首先,要設置微調器,您需要一個適配器。然後你需要使用一個OnItemSelectedListener。使用此代碼:
europeListView = (ListView) findViewById(R.id.europeListView);
africaListView = (ListView) findViewById(R.id.africaListView);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(adapter.getItem(i) == "Europe"){
europeListView.setVisibility(View.VISIBLE);
africaListView.setVisibility(View.INVISIBLE);
} else if(adapter.getItem(i) == "Africa"){
africaListView.setVisibility(View.VISIBLE);
europeListView.setVisibility(View.INVISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
最後,創建一個名爲「國家」包括歐洲和非洲在strings.xml文件中,像這樣的字符串數組:
<string-array name="countries">
<item>Europe</item>
<item>Africa</item>
</string-array>
希望這有助於!