更改textview的颜色问题 - android片段

时间:2015-03-13 10:00:37

标签: android android-fragments textview

我的片段中有2个文本视图。打开片段时,第一个文本视图将显示为红色(#e92624)。按第二个文本视图时,第一个视图应为黑色(#D8000000),第二个文本视图应变为红色。

在我的onCreate中,

mQuickReturnViewPas.setBackgroundColor(Color.parseColor("#e92624"));

        if (selectedView != null)
            mQuickReturnViewPas.setBackgroundColor(Color
                    .parseColor("#D8000000"));

外面的onCreate我有,

                      mQuickReturnViewPas
                        .setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {

                                if (selectedView != null)
                                    selectedView.setBackgroundColor(Color
                                            .parseColor("#D8000000"));

                                // set selectedView to be current selected view
                                selectedView = (TextView) v;

                                // change selected view to be red
                                selectedView.setBackgroundColor(Color
                                        .parseColor("#e92624"));

                            }

                        });

                mQuickReturnViewRic
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                if (selectedView != null)
                                    selectedView.setBackgroundColor(Color
                                            .parseColor("#D8000000"));

                                // set selectedView to be current selected view
                                selectedView = (TextView) v;

                                // change selected view to be red
                                selectedView.setBackgroundColor(Color
                                        .parseColor("#e92624"));
                            }

                        });
            }

使用此代码,如果我按下第二个文本视图,它将变为红色,但第一个文本视图保持红色(不会变为黑色)。我应该在onCreate方法中做出哪些改变来完成这项任务?

1 个答案:

答案 0 :(得分:0)

像这样更新您的方法

    mQuickReturnViewPas
                            .setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {

                                    if (selectedView != null)
                                        selectedView.setBackgroundColor(Color
                                                .parseColor("#D8000000"));

                                    // set selectedView to be current selected view
                                    selectedView = (TextView) v;

                                    // change selected view to be red
                                    selectedView.setBackgroundColor(Color
                                            .parseColor("#e92624"));
                                   // change non-selected view to be black
                                            mQuickReturnViewRic.setBackgroundColor(Color
                                                .parseColor("#D8000000"));



                                }

                            });

                    mQuickReturnViewRic
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                    if (selectedView != null)
                                        selectedView.setBackgroundColor(Color
                                                .parseColor("#D8000000"));

                                    // set selectedView to be current selected view
                                    selectedView = (TextView) v;

                                    // change selected view to be red
                                    selectedView.setBackgroundColor(Color
                                            .parseColor("#e92624"));
                                  // change non-selected view to be black
                               mQuickReturnViewPas.setBackgroundColor(Color
                                                    .parseColor("#D8000000"));
                                }

                            });