切换按钮可同时更改textview

时间:2014-11-06 19:57:53

标签: java android

我正在设置我的切换按钮,例如单选按钮,这样当我在按钮之间单击时,我的textview会更新。当我单击textview两侧的加号或减号按钮时,按钮会随textview一起更新。

我有一个错误的视频显示:https://www.dropbox.com/s/cfx2r3t3umhgc4p/VIDEO0048.mp4?dl=0以反映我想要做的事情。

我对下面的内容有两个问题:

1)我认为我的for循环逻辑是有缺陷的:这有意义吗? :

// **Loop through this group then for each child, set your listener
                    for (int i = 0; i < group.getChildCount(); ++i) {
                        View child = group.getChildAt(i);
                        child.setOnClickListener(createClickListener(i));
                    }

2)如何让这段代码更干净?我怎么能重构它?

//onClickListener method that returns an interface
private View.OnClickListener createClickListener(final int value) {
    return new View.OnClickListener()  {

        @Override
        public void onClick(View view) {

            buttonValue = value;
            buttonState = true;

            ToggleButton clickedButton = (ToggleButton) view;
            RadioGroup radioGroup= (RadioGroup) clickedButton.getParent();

            if (clickedButton.isChecked()) {
                System.out.println("Button Value: " + Integer.toString(buttonValue));
                num.setText( Integer.toString(buttonValue));
                System.out.println(buttonState);
            } else {
                // The toggle is disabled
                System.out.println(buttonState);
                System.out.println("Is this called?");
            }
            System.out.println("Check changed listener called");


           for (int i = 0; i < radioGroup.getChildCount(); ++i) {
                View nextChild = radioGroup.getChildAt(i);
                if (!(nextChild instanceof ToggleButton)) {
                    continue;
                }
                if (nextChild.getId() != clickedButton.getId() || !clickedButton.isChecked()) {
                    ToggleButton tb2 = (ToggleButton) nextChild;
                    tb2.setChecked(false);
                }
            }
        }

    };
}

            @Override
            public Object instantiateItem(ViewGroup parent, final int position) {

                //Get the inflater
                LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                //inflate the root layout
                View view = inflater.inflate(R.layout.collection, null);

                final int[] counter = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
                num = (TextView) view.findViewById(R.id.num);
                num.setText(Integer.toString(counter[0]));

                //scores toggle buttons
                zero = (ToggleButton) view.findViewById(R.id.number_zero);
                one = (ToggleButton) view.findViewById(R.id.number_one);
                two = (ToggleButton) view.findViewById(R.id.number_two);
                three = (ToggleButton) view.findViewById(R.id.number_three);
                four = (ToggleButton) view.findViewById(R.id.number_four);
                five = (ToggleButton) view.findViewById(R.id.number_five);
                six = (ToggleButton) view.findViewById(R.id.number_six);
                seven = (ToggleButton) view.findViewById(R.id.number_seven);
                eight = (ToggleButton) view.findViewById(R.id.number_eight);
                nine = (ToggleButton) view.findViewById(R.id.number_nine);
                ten = (ToggleButton) view.findViewById(R.id.number_ten);


                final ViewGroup group = (ViewGroup) view.findViewById(R.id.toggleGroup);

                // **Loop through this group then for each child, set your listener
                for (int i = 0; i < group.getChildCount(); ++i) {
                    View child = group.getChildAt(i);
                    child.setOnClickListener(createClickListener(i));
                }

                final ImageView plus_button = (ImageView) view.findViewById(R.id.plus_sign);
                final ImageView minus_button = (ImageView) view.findViewById(R.id.minus_sign);

                zero.setOnClickListener(createClickListener(0));
                one.setOnClickListener(createClickListener(1));
                two.setOnClickListener(createClickListener(2));
                three.setOnClickListener(createClickListener(3));
                four.setOnClickListener(createClickListener(4));
                five.setOnClickListener(createClickListener(5));
                six.setOnClickListener(createClickListener(6));
                seven.setOnClickListener(createClickListener(7));
                eight.setOnClickListener(createClickListener(8));
                nine.setOnClickListener(createClickListener(9));
                ten.setOnClickListener(createClickListener(10));

                plus_button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {

                        if (plus_button.isPressed()) {

                            if ((counter[0] <= 9) && (counter[0] >= 0)) {

                                counter[0]++;
                                num.setText(Integer.toString(counter[0]));
                                buttonValue = Integer.parseInt(num.getText().toString());
                                for (int i = 0; i < group.getChildCount(); ++i) {
                                    View nextChild = group.getChildAt(i);
                                    ToggleButton tb2 = (ToggleButton) nextChild;
                                    if (i == buttonValue) {
                                        tb2.setChecked(true);
                                    } else {
                                        tb2.setChecked(false);
                                    }
                                }
                                System.out.println(num.getText().toString());
                            }
                        }
                    }
                });

                minus_button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {

                        if (minus_button.isPressed()) {

                            if ((counter[0] <= 10) && (counter[0] > 0)) {
                                counter[0]--;
                                num.setText(Integer.toString(counter[0]));
                                buttonValue = Integer.parseInt(num.getText().toString());
                                for (int i = 0; i < group.getChildCount(); ++i) {
                                    View nextChild = group.getChildAt(i);
                                    ToggleButton tb2 = (ToggleButton) nextChild;
                                    if (i == buttonValue) {
                                        tb2.setChecked(true);
                                    } else {
                                        tb2.setChecked(false);
                                    }
                                }
                                System.out.println(num.getText().toString());
                            }
                        }
                    }
                });

1 个答案:

答案 0 :(得分:-1)

查看Android doc for the ToggleButton。特别是关于OnCheckedChangeListener的底部部分对你来说应该很有趣。