选择另一个微调器项目时更改微调器文本颜色

时间:2017-11-03 16:34:25

标签: android spinner android-appcompat textcolor

当从另一个微调器中选择一个项目时,我需要更改AppCompatSpinner的文本颜色。

enter image description here

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;
}
}

1 个答案:

答案 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) {}
        });