需要帮助添加单选按钮和文本框的值

时间:2019-06-12 03:14:01

标签: java android

我在Android Studio中创建了一个应用程序,该应用程序使用按单选按钮组和复选框分组的单选按钮,每个单选按钮都分配了一个值,该值在单击特定的单选按钮或复选框时存储。当前有一个计算按钮,当单击该按钮时会调用一个commitOrder()方法,将所有选定单选按钮和复选框的总和合计并在屏幕上显示一条消息。那很好。我想完全消除计算按钮,并在用户单击任何单选按钮或单选按钮和复选框的组合时实时自动计算和刷新总和。我在Stack Overflow和Google上进行了广泛的搜索,但是没有找到关于如何在Java代码本身中调用SubmitOrder()方法的任何有用的示例或建议。我尝试了各种迭代,但没有成功。任何建议将不胜感激。

我尝试在与每个单选按钮或复选框相关的代码中添加submitOrder()方法,但没有成功

//以下代码是必需的,以便使无线电组正常工作而不会引起错误

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();


}

public void submitOrder(View view) {

单选按钮组变量 * /

    RadioButton radio_one = (RadioButton) findViewById(R.id.radio_one);
    boolean hasRadioOne = radio_one.isChecked();


    RadioButton radio_two = (RadioButton) findViewById(R.id.radio_two);
    boolean hasRadioTwo = radio_two.isChecked();

    RadioButton radio_three = (RadioButton)  findViewById(R.id.radio_three);
    boolean hasRadioThree = radio_three.isChecked();

    RadioButton radio_four = (RadioButton) findViewById(R.id.radio_four);
    boolean hasRadioFour = radio_four.isChecked();


    RadioButton radio_five = (RadioButton) findViewById(R.id.radio_five);
    boolean hasRadioFive = radio_five.isChecked();



      // this is the starting value for assigning  the  score

    int basePrice = 0;

 // 5 values
    if (addRadioOne) {
        basePrice = basePrice + 3;





    } else if (addRadioTwo) {
        basePrice = basePrice + 1;



    } else if (addRadioThree) {
        basePrice = basePrice + 0;




    } else if (addRadioFour) basePrice = basePrice + 2;


    else if (addRadioFive) basePrice = basePrice + 3;

这将计算总计:

  private int calculatePrice(boolean addRadioOne, boolean addRadioTwo,                    boolean addRadioThree,boolean addRadioFour, boolean addRadioFive)

3 个答案:

答案 0 :(得分:0)

您必须执行以下操作:

  • 您不必通过submitOrder()方法传递每个 RadioButton CheckBox

  • 您可以使用 OnCheckedChangeListener 为每个RadioGroupCheckBox定义公共侦听器,在其中调用submitOrder

  • 您可以分别为每个RadioButtonCheckBox true或false

  • 获取所有值

希望您能将我的文字转换为编程语言。

谢谢

答案 1 :(得分:0)

您必须执行以下操作。为所有单选按钮设置checkedChangeListeners。选中/取消选中每个单选按钮时,侦听器将相应地更新总和。

        radio1 = findViewById(R.id.radio1);
        radio2 = findViewById(R.id.radio2);
        radio3 = findViewById(R.id.radio3);
        private int count=0;


        radio1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    count=count+3;
                else{
                    if(count>0){
                        count=count-3;
                    }
                }
                System.out.println(count);
            }
        });

        radio2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    count++;
                else{
                    if(count>0){
                        count--;
                    }
                }
                System.out.println(count);
            }
        });

        radio3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    count=count+0;
                else{
                    if(count>0){
                        count=count-0;
                    }
                }
                System.out.println(count);
            }
        });

答案 2 :(得分:0)

 //So after many attempts and much testing I found that all I had to do was add
 the submitOrder(view:null); in the onRadioButtonClicked method

// the following code is required in order for the radio groups to function 
//without causing an error

public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
submitOrder(null);
}                

And for the checkboxes similarly we add the submitOrder(view:null); in 
the onCheckBoxClicked method which is called when the CheckBox is clicked

public void onCheckBoxClicked(View view) {
// Is the Checkbox now checked?
boolean checked = ((CheckBox) view).isChecked();
submitOrder(null);
}                

您必须具有两种单独的方法,一种用于RadioButtons,一种用于    必须在每个radioButton和CheckBox XML代码中调用的CheckBox    对于每个对象。否则,应用程序将崩溃。