第二次加载片段时,TextView SetColor无法正常工作

发布于 2025-02-08 14:34:47 字数 1652 浏览 2 评论 0原文

我正在尝试根据选择哪个项目来更改我的自定义旋转器所选项目的颜色。

如果选择第一个元素,我想为文本视图设置灰色,因为我将第一个项目用作提示消息,其余项目必须是黑色的。

当我尝试该应用程序并第一次加载片段时,它以正确的方式工作,但是当我使用导航按钮转到另一个片段时,如果我再次切换到第一个片段,则setColor不是工作时,文本视图总是以黑色出现。

这是我用来更改片段中颜色的代码,

sexoSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
                if (v == null)
                {
                    LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.item_gender, null);
                }
                iSexoSelected = position;
                switch (position) {
                    case 0:
                        ((TextView) v.findViewById(R.id.genderText)).setTextColor(getResources().getColor(R.color.gris_hint_fuente));
                        break;
                    case 1:
                        ((TextView) v.findViewById(R.id.genderText)).setTextColor(getResources().getColor(R.color.black));
                        sexoSeleccion = "Femenino";
                        break;
                    case 2:
                        ((TextView) v.findViewById(R.id.genderText)).setTextColor(getResources().getColor(R.color.black));
                        sexoSeleccion = "Masculino";
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

我不知道为什么第二次加载片段的微调器颜色没有改变,有什么想法吗?

事先感谢您的所有帮助。

I'm trying to change the color for my Custom Spinner selected item depending on which item is selected.

if I select the first element i want to set a grey color for the TextView because I'm using the first item as a hint message and the rest of items must be black.

when I'm trying the app and i load the fragment for the first time, it works in the right way, but when i used the navigation buttons to go to another fragment, if i switch again to the first fragment, the SetColor is not working, the Textview always appear with the text in black.

here is the code that i'm using to change the color in my fragment

sexoSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
                if (v == null)
                {
                    LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.item_gender, null);
                }
                iSexoSelected = position;
                switch (position) {
                    case 0:
                        ((TextView) v.findViewById(R.id.genderText)).setTextColor(getResources().getColor(R.color.gris_hint_fuente));
                        break;
                    case 1:
                        ((TextView) v.findViewById(R.id.genderText)).setTextColor(getResources().getColor(R.color.black));
                        sexoSeleccion = "Femenino";
                        break;
                    case 2:
                        ((TextView) v.findViewById(R.id.genderText)).setTextColor(getResources().getColor(R.color.black));
                        sexoSeleccion = "Masculino";
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

i don't know why the second time i load the fragment the color for the spinner is not changing, any ideas ?

thanks in advance for all the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文