切换状态更改无效-Android

时间:2019-01-30 03:17:47

标签: android

我试图通过轻按另一个开关来更改开关的状态及其实现。我尝试了但是失败了。 有人可以帮忙吗?

这是我的Activity.java

//first switch

flw_Rate_switch.setOnCheckedChangeListener(new 

CompoundButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)  {

            if (isChecked) {

                    //lqd_followed_switch.setOnCheckedChangeListener(null);

          //lqd_followed_switch.setChecked(!lqd_followed_switch.isChecked());

                    flow_rate_switch_txt.setTextColor(Color.RED);

                    mlineData = generateLineData(rate);
                    data.setData(mlineData);
                    combinedChart.setData(data);

                    combinedChart.setVisibleXRangeMaximum(4); // allow 4 values to be displayed at once on the x-axis, not more
                    combinedChart.moveViewToX(4);
                    combinedChart.animateY(2000);
                    combinedChart.invalidate();


                } else {
                    //elseFlowRate();
                    flow_rate_switch_txt.setTextColor(Color.BLACK);
                    String label = "Flow Rate";

                    boolean indicator = data.removeDataSet(lineData1.getDataSetByLabel(label, true));
                    combinedChart.notifyDataSetChanged();
                    combinedChart.invalidate();
                    combinedChart.animateX(1000);
                }
            }

        });


//2nd switch


        lqd_followed_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {

                 lqd_followed_switch_txt.setTextColor(getColor(R.color.colorPrimary));



                    mlineData = generateLineData1(foll);
                    data.setData(mlineData);
                    combinedChart.setData(data);

                    combinedChart.setVisibleXRangeMaximum(4); // allow 4 values to be displayed at once on the x-axis, not more
                    combinedChart.moveViewToX(4);
                    combinedChart.animateY(2000);
                    combinedChart.invalidate();

                } else {

                    lqd_followed_switch_txt.setTextColor(Color.BLACK);

                    String label = "Liquid Followed";
                    boolean indicator = data.removeDataSet(lineData2.getDataSetByLabel(label, true));
                    combinedChart.notifyDataSetChanged();
                    combinedChart.invalidate();

                }
            }

        });

当我点击第一个开关时,它运行“ if”块,同时,当我点击第二个开关时,它运行第二个开关的“ if”块,但它不能一直运行开关第一个的“ if”子句块而不是它应该自动运行开关1st的“ else”块。

1 个答案:

答案 0 :(得分:0)

CompoundButton.OnCheckedChangeListener()

是事件侦听器,表示如果未发生(选中或未选中)事件,则不会触发它。

如果要更新其他UI组件,则应调用以下内容:

flw_Rate_switch.setChecked(false);

flw_Rate_switch.setChecked(true);
相关问题