Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我如何设置它,以便如果我点击一个按钮,值在数字选择器中改变,反之亦然?_Java_Android - Fatal编程技术网

Java 我如何设置它,以便如果我点击一个按钮,值在数字选择器中改变,反之亦然?

Java 我如何设置它,以便如果我点击一个按钮,值在数字选择器中改变,反之亦然?,java,android,Java,Android,我希望能够检查我目前设置的11个按钮中的一个,并在我制作的自定义数字选择器中更新按钮的值,反之亦然。因此,如果我在数字选择器中选择一个数字,我希望按下/单击按钮以获得相同的值 我在寻找最好/最合理的方法。我在想,也许我可以把所有的东西都放在if-else语句中,但我不确定这是怎么回事 以下是我目前的代码: int buttonValue = 0; //onClickListener method that returns an interface private View.On

我希望能够检查我目前设置的11个按钮中的一个,并在我制作的自定义数字选择器中更新按钮的值,反之亦然。因此,如果我在数字选择器中选择一个数字,我希望按下/单击按钮以获得相同的值

我在寻找最好/最合理的方法。我在想,也许我可以把所有的东西都放在if-else语句中,但我不确定这是怎么回事

以下是我目前的代码:

 int buttonValue = 0;

   //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;

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

                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) {

                final int delPosition = 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);

                //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);

                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));

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

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

                  plus_button.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View view, final int value) {

                            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());
                                }
                            }

                        }
                    });

                    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());
                                }
                            }
                        }
                    });

                parent.addView(view, 0);
                return view;
            }
int按钮值=0;
//返回接口的onClickListener方法
private View.OnClickListener createClickListener(最终int值){
返回新视图。OnClickListener(){
@凌驾
公共void onClick(视图){
按钮值=值;
ToggleButton clickedButton=(ToggleButton)视图;
RadioGroup RadioGroup=(RadioGroup)单击按钮。getParent();
对于(int i=0;i
以下是xml文件的一部分:

//Custom number picker


        <ImageView
                android:id="@+id/minus_sign"
                android:src="@drawable/minus"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_row="0"
                android:layout_column="0"
                />

        <TextView
                android:id="@+id/num"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_column="1"
                android:layout_row="0"
                />

        <ImageView

                android:id="@+id/plus_sign"
                android:src="@drawable/plus"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_row="0"
                android:layout_column="2"
                />


//The buttons are toggle buttons inside a radiogroup 

<RadioGroup>

        <ToggleButton

                    android:id="@+id/number_zero"
                    android:layout_width="25sp"
                    android:layout_height="35sp"
                    android:textOn="@string/number_zero"
                    android:textOff="@string/number_zero"
                    android:background="@drawable/number_color"
                    />

        <ToggleButton
                    android:id="@+id/number_one"
                    android:layout_width="25sp"
                    android:layout_height="35sp"
                    android:textOn="@string/number_one"
                    android:textOff="@string/number_one"
                    android:background="@drawable/number_color"
                />
</RadioGroup> 
//自定义数字选择器
//这些按钮是放射组内的切换按钮

您可以将所有切换按钮存储在一个数组中

为当前选定的切换按钮的索引值保留全局整数。(该值应始终介于0和10之间)

编写一个refreshUI函数。它应该移除切换按钮上的强光,然后转到位于数组中该全局索引值处的切换按钮并将其强光。它还应该在文本视图中写入切换按钮值

按下切换按钮时,将全局索引值更新为数组t中的位置
// Class fields.
private int lastButtonIndex = 0;
// In the onCreate method put all the toggle buttons in this array.
private ToggleButton[] toggleButtons=new ToggleButton[11]; 

private void refreshUI(){ 
   for(ToggleButton button : toggleButtons){
     //set each button background to white.
   }
   // set the background color of the toggleButtons[lastButtonIndex] to red.
   // set the textview text to the value of toggleButtons[lastButtonIndex].
}
  int buttonValue = 0;

   //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;

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

            // **Change your TextView here
            ...

            // **Keep these since you still want to update all buttons
            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) {

            final int delPosition = 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);

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

            // **Give your RadioGroup an id so that you can find it here
            ViewGroup group = view.findViewById(...);
            // **Loop through this group then for each child, set your listener
           for (...){
                      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);


              plus_button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view, final int value) {

                        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());
                                // **Use the loop here again to update all buttons
                                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);}
                                }
                            }
                        }

                    }
                });

                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());
                                // **Use the loop here again to update all buttons
                                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);}
                                }
                            }
                        }
                    }
                });

            parent.addView(view, 0);
            return view;
        }
<RadioGroup>

    <ToggleButton

                android:id="@+id/number_zero"
                android:layout_width="25sp"
                android:layout_height="35sp"
                android:textOn="@string/number_zero"
                android:textOff="@string/number_zero"
                android:background="@drawable/number_color"
                />

    <ToggleButton
                android:id="@+id/number_one"
                android:layout_width="25sp"
                android:layout_height="35sp"
                android:textOn="@string/number_one"
                android:textOff="@string/number_one"
                android:background="@drawable/number_color"
            />
  </RadioGroup>