简体   繁体   English

如何设置此功能,以便在单击按钮时数字选​​择器中的值发生变化,反之亦然?

[英]How can I set this up so that if I click on a button, the value changes in a number picker and vice versa?

I want to be able to check one one of the eleven buttons I currently have set up and have the value of the button update inside a custom number picker I made and vice versa. 我希望能够检查当前设置的11个按钮之一,并在我制作的自定义数字选择器中更新按钮的值,反之亦然。 So that if I pick a number in the number picker, I want the button to be depressed/clicked on for that same value. 这样,如果我在数字选择器中选择一个数字,我希望按钮被按下/单击以获取相同的值。

I'm looking for the best/most reasonable approach. 我正在寻找最佳/最合理的方法。 I was thinking maybe if I put everything inside an if-else statement but I'm not sure how that would work. 我当时在想,是否可以将所有内容放在if-else语句中,但是我不确定该如何工作。

Here's my code so far: 到目前为止,这是我的代码:

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

Here's part of the xml file: 这是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> 

For your collection of toggle buttons, use LinearLayout or RelativeLayout or any other type that can be cast to ViewGroup . 对于切换按钮的集合,请使用LinearLayoutRelativeLayout或可以ViewGroupViewGroup任何其他类型。 Then after you inflated the view, cast it to ViewGroup , then use view.getChildAt(i) to get the corresponding ToggleButton . 然后,将视图放大后,将其转换为ViewGroup ,然后使用view.getChildAt(i)获得相应的ToggleButton Better than finding all buttons by id, wouldn't it? 比通过id查找所有按钮更好,不是吗?

This is similar to what you've done to the radio buttons. 这类似于您对单选按钮所做的操作。

-- -

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

-- -

You run your loop for setting click listeners on this RadioGroup in your xml only . 在xml中运行此循环以在此RadioGroup上设置单击侦听器。 Do not run your loop on the entire xml 不要在整个xml上运行循环

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

You could store all of your togglebuttons inside of an array. 您可以将所有切换按钮存储在数组中。

Keep a global int for the currently selected toggle button's index value. 为当前选定的切换按钮的索引值保留全局int。 (This should always be between 0 and 10) (该值应始终在0到10之间)

Write a refreshUI function. 编写refreshUI函数。 It should remove the high light from the toggle buttons, then it should go to the toggle button located at that global index value inside the array and high light it. 它应从切换按钮中删除突出显示的内容,然后转到位于阵列内该全局索引值处的切换按钮并将其突出显示。 It should also write that toggle buttons value in the text view. 它还应在文本视图中写入切换按钮的值。

When a toggle button is pressed, update the global index value to where in the array that toggle button is located and then refreshUI. 当按下切换按钮时,将全局索引值更新为切换按钮在数组中的位置,然后刷新UI。

When a plus/minus is pressed, make sure the global index value is within 0-10 range, change it (by +1 or -1) and refreshUI. 当按下加号/减号时,请确保全局索引值在0-10范围内,将其更改(通过+1或-1)并刷新UI。

// 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].
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中将RGB图像转换为CMYK,反之亦然? - how can I convert an RGB image to CMYK and vice versa in Java? Java:我可以将对象列表转换为String []列表,反之亦然吗? - Java: Can I convert List of Object to List of String[] and vice versa? 如何将Eclipse中的libgdx项目从Windows移植到Linux,反之亦然? - How can I transfter a libgdx project in eclipse from windows to linux and vice versa? 当数据写入协议缓冲区时,如何使用Java触发C ++程序,反之亦然? - How can I have Java trigger C++ programs and vice versa when data is written to protocol buffers? 如何从 @SessionScoped Bean 内部到达 JSF @Singleton Bean,反之亦然? - How can I reach JSF @Singleton Bean from inside @SessionScoped Bean and vice versa? 如何将 JLabel 转换为字符串,反之亦然? - How do I convert a JLabel into a String & vice versa? 如何将JSON解析为JTree,反之亦然 - How do I parse a JSON to JTree and vice versa 单击按钮时如何分配关键字的值 - how can I assign the value of keyword when I click the button 如何才能延迟单击按钮操作,以便存储JSON数组? - How can I delay the click button action just enough so i can store a JSON array? 单击按钮将JSP数据传递给Java类,反之亦然 - Passing JSP data to Java class and vice versa on button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM