使用Textwatcher更改微调框的文本颜色

时间:2018-09-25 10:33:01

标签: android android-spinner android-textwatcher

嗨,我想使用文本观察器更改Spinner文本的颜色。Spinner文本应根据在edittext或空edittext中输入的字符来更改颜色。

    <Spinner
                android:id="@+id/displacement"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight=".15"
                android:entries="@array/array_metric_piston"
                android:gravity="center"
                android:paddingEnd="1dp"
                android:paddingStart="1dp"
                android:background="@drawable/editext_green"

 />

尝试如下但不起作用

 if(charSequence.length() >0 ) {


                    spin_motor_displacement.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            ((TextView) spin_motor_displacement.getRootView()).setTextColor(Color.RED);
                        }
                    });

}

1 个答案:

答案 0 :(得分:0)

将此自定义适配器添加到您的代码中

mymodule: myModule = new myModule(3,'hi!', true)

别忘了称呼它:

public class adapter_spinner_steuer extends ArrayAdapter<String> {

    ArrayList<String> arrayList = new ArrayList<String>();

    public adapter_spinner_steuer(Context context, int textViewResourceId, ArrayList<String> objects) {
        super(context, textViewResourceId, objects);
        arrayList = objects;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View view = super.getDropDownView(position, convertView, parent);
        TextView tv = (TextView) view;
        if (position == 0) {
            tv.setTextColor(Color.parseColor("#0015ff"));
            ;
        } else {
            tv.setTextColor(Color.parseColor("#7178c6"));
        }
        return view;
    }

    @Override
    public boolean isEnabled(int position) {
        if (position == 0) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(URL.this).inflate(R.layout.public_spinner_item, parent, false);
        TextView label =  view.findViewById(R.id.spinner_textView);
        label.setText(arrayList.get(position));
        if (this.isEnabled(position)) {
            label.setTextColor(Color.parseColor("#7178c6"));
        }
        return view;
    }
}