当从另一个微调器中选择一个项目时,我需要更改AppCompatSpinner的文本颜色。
AppCompatSpinner(categorySpinner)
AppCompatSpinner(fromSpinner)
categorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
//Setting the upper adapter according to the category.
lengthAdapter = new SpinnerAdapter(getApplicationContext(), lengthList);
lengthAdapter.setDropDownViewResource(R.layout.item_dd_r);
fromSpinner.setAdapter(lengthAdapter);
//Need to add code to change the Text Color of fromSpinner.
...
}
CategorySpinnerAdapter adapter = new CategorySpinnerAdapter(this, categoriesList);
adapter.setDropDownViewResource(R.layout.cat_dd_r);
categorySpinner.setAdapter(adapter);
类别适配器代码 -
public class CategorySpinnerAdapter extends ArrayAdapter {
private Context context;
private List<String> categoryItemList;
public CategorySpinnerAdapter(Context context, List<String> categoryItemList) {
super(context, 0, categoryItemList);
this.context = context;
this.categoryItemList = categoryItemList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
String item = categoryItemList.get(position);
View view = convertView;
if (view == null) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.spinner_item_convert_cat, parent, false);
}
TextView spinnerText = view.findViewById(R.id.textView_spinnerItem_cat);
spinnerText.setText(item);
return view;
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
TextView dropDownText = (TextView) super.getDropDownView(position, convertView, parent);
return dropDownText;
}
}
答案 0 :(得分:0)
以下是在secondSpinner
选择项目时更改firstSpinner
的文字颜色
boolean selected = false;
firstSpinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long l) {
if (position != 0) {
if (!selected) selected = true;
secondSpinner.post(new Runnable() {
@Override
public void run() {
((TextView) ((ViewGroup) secondSpinner.getSelectedView()).getChildAt(0))
.setTextColor(Color.BLACK);
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {}
});
secondSpinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long l) {
if (selected) {
secondSpinner.post(new Runnable() {
@Override
public void run() {
((TextView) ((ViewGroup) secondSpinner.getSelectedView()).getChildAt(0))
.setTextColor(Color.BLACK);
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {}
});