简体   繁体   English

我如何知道在Android Studio中选择了哪个单选按钮

[英]How can I know which radio button is selected in Android studio

i have 3 radio buttons and once i click any one of them it should update 2 text views. 我有3个单选按钮,一旦单击其中任何一个,它应该更新2个文本视图。 One text view for the name and other text view for the price. 名称的一个文本视图和价格的其他文本视图。 When i click on the radio button the name gets updated but i am facing issue with how to get the item price from the array. 当我单击单选按钮时,名称会更新,但是我面临着如何从数组中获取商品价格的问题。 Each of the radio button has different pricing. 每个单选按钮都有不同的定价。

Code: 码:

     pizzaSizeRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        pizzaSizeRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                Radio button checked
                sizeSelect = (RadioButton) findViewById(checkedId);
                selectedSize.setText(sizeSelect.getTag().toString());

//                how to display the price from array
                sizePrice = array_price_list[].getItem_price();
                totalPriceDisplay.setText(sizePrice);

            }
        });

you can implement switch Radiogroup checkedId 您可以实现开关RadiogroupcheckedId

pizzaSizeRadioGroup = (RadioGroup) findViewById(R.id.radioGroup); pizzaSizeRadioGroup =(RadioGroup)findViewById(R.id.radioGroup);

    pizzaSizeRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch(checkedId){
                case R.id.radio0://Radio button id
                    // do operations specific to this selection
                    break;
                case R.id.radio1://Radio button id
                    // do operations specific to this selection
                    break;
                case R.id.radio2://Radio button id
                    // do operations specific to this selection
                    break;
            }   
        }
    });

or check the selected radio button 或检查选定的单选按钮

     int selectedId = pizzaSizeRadioGroup.getCheckedRadioButtonId();

It is not totally clear what is the values in the price array you have or what is the implementation of your getItem_price() method. 尚不清楚您拥有的价格数组中的值是什么,或者getItem_price()方法的实现是什么。

But it seems that you have set the name in the tag of the radio buttons and that is why you are able to get it and populate your selectedSize textview. 但是似乎您已经在单选按钮的标记中设置了名称,这就是为什么您能够获取它并填充selectedSize textview的原因。

As much as i understand your problem, you could follow two approaches 据我了解您的问题,您可以采取两种方法

Approach 1 : create a list of a pizza object with name and price and an id. 方法1:创建具有名称和价格以及ID的披萨对象的列表。 Set the id in the tag of the radio button. 在单选按钮的标签中设置ID。 that way you can get the name and price both using the id you will get form radioButton.getTag(). 这样,您既可以使用id来获取名称和价格,也可以通过radioButton.getTag()获得表单。

Approach 2: Concatenate the name and price with a |, eg, size1|25 and set that to the tag of radio button. 方法2:将名称和价格用|连接,例如size1 | 25,并将其设置为单选按钮的标签。 Then you can use split on getTag to get the name and price both. 然后,可以在getTag上使用split来获取名称和价格。

Hope this helps you with your problem. 希望这可以帮助您解决问题。

for this purpose you can use switch please follow this 为此,您可以使用开关,请遵循此

radiobuttoncheck.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
{
    public void onCheckedChanged(RadioGroup rgroup, int r_id) {
        switch(r_id){
            case R.id.rbid1:
                //show toast here to get idea
                break;
            case R.id.rbid2:

               //show toast here to get idea

                break;
        }   
    }
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM