我想创建两个微调器。
例如: (这是Spinner1中的列表)
Category A
Category B
Category C
当我在spinner1中选择类别A时,spinner2将显示类别A的值。
答案 0 :(得分:1)
首先,您必须在XML中创建2个Spinner
。
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner1"
android:layout_margin="10dp"
android:padding="10dp"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner2"
android:layout_margin="10dp"
android:padding="10dp"/>
然后,您必须为Spinner
声明变量。
final Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
final Spinner spinner2 = (Spinner)findViewById(R.id.spinner2);
现在,每个String[]
需要一个Spinner
,但是您想更改一个微调器的值,所以必须为每种情况创建一个String[]
。
String[] categories = new String[] {
"Category A", "Category B", "Category C"
};
String[] category_a_values = new String[] {
"Value 1 from category a", "Value 2 from category a", "Value 3 from category a"
};
String[] category_b_values = new String[] {
"Value 1 from category b", "Value 2 from category b", "Value 3 from category b"
};
String[] category_c_values = new String[] {
"Value 1 from category c", "Value 2 from category c", "Value 3 from category c"
};
String[]
类别用于 spinner1 ,另一个用于 spinner2 。
现在我们每个ArrayAdapter
需要一个String[]
。
final ArrayAdapter<String> categories_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
final ArrayAdapter<String> category_a_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, category_a_values);
final ArrayAdapter<String> category_b_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, category_b_values);
final ArrayAdapter<String> category_c_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, category_c_values);
在这里,我们将ArrayAdapter
设置为 spinner1 。
spinner1.setAdapter(categories_adapter);
然后,如果选择了 spinner1 的另一个值,则更改 spinner2 的ArrayAdapter
。
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
spinner2.setAdapter(category_a_adapter);
break;
case 1:
spinner2.setAdapter(category_b_adapter);
break;
case 2:
spinner2.setAdapter(category_c_adapter);
break;
default:
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
final Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
final Spinner spinner2 = (Spinner)findViewById(R.id.spinner2);
String[] categories = new String[] {
"Category A", "Category B", "Category C"
};
String[] category_a_values = new String[] {
"Value 1 from category a", "Value 2 from category a", "Value 3 from category a"
};
String[] category_b_values = new String[] {
"Value 1 from category b", "Value 2 from category b", "Value 3 from category b"
};
String[] category_c_values = new String[] {
"Value 1 from category c", "Value 2 from category c", "Value 3 from category c"
};
final ArrayAdapter<String> categories_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
final ArrayAdapter<String> category_a_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, category_a_values);
final ArrayAdapter<String> category_b_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, category_b_values);
final ArrayAdapter<String> category_c_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, category_c_values);
spinner1.setAdapter(categories_adapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
spinner2.setAdapter(category_a_adapter);
break;
case 1:
spinner2.setAdapter(category_b_adapter);
break;
case 2:
spinner2.setAdapter(category_c_adapter);
break;
default:
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 1 :(得分:0)
Spinner spinner1 = findViewById(R.id.spinner_1);
Spinner spinner2 = findViewById(R.id.spinner_2);
String[] arraySpinner = new String[] {
"Category A", "Category B", "Category C"
};
String[] arraySpinner2 = new String[] {
"Category A value", "Category B value", "Category C value"
};
ArrayAdapter<String> adp1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adp1);
ArrayAdapter<String> adp2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner2);
adp2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adp2);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
spinner2.setSelection(spinner1.getSelectedItemPosition());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
我尚未测试此代码。有空的时候我会测试。