简体   繁体   English

如何在Android中以编程方式添加Checkbox和单选按钮时选择哪个

[英]how to get which is selected when Checkbox and Radio button added programmatically in android

private void parseProductOptionData(JSONObject response){
    try {
        boolean success = response.getBoolean("success");
        if(success){
            rlProductOption.setVisibility(View.GONE);
            //Get All the Categories
            JSONArray data = response.getJSONArray("data");
            if(data.length()!=0){
                rlProductOptionQuantityTxt.setVisibility(View.VISIBLE);
                for(int i= 0;i<data.length();i++){
                    //First Get Type
                    JSONObject optionQuantity = data.getJSONObject(i);
                    Log.d(TAG, "onResponse: option quantity"+optionQuantity);


                    //Create a Layout
                    llMainLayout = new LinearLayout(getActivity());
                    llMainLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
                    llMainLayout.setBackgroundResource(R.drawable.border_details);
                    llMainLayout.setOrientation(LinearLayout.VERTICAL);
                    llMainLayout.setPadding(10,0,10,5);
                    llProductOptionsSheet.addView(llMainLayout);



                    tvOptionTitleText= new TextView(getActivity());
                    tvOptionTitleText.setId(Integer.valueOf("1"+i));
                    tvOptionTitleText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
                    tvOptionTitleText.setTypeface(Typeface.SERIF);
                    llMainLayout.addView(tvOptionTitleText);
                    tvOptionTitleText.setText(optionQuantity.getString("title"));

                    if(optionQuantity.getString("min_selection").equals("1") && optionQuantity.getString("max_selection").equals("1")){
                        RadioGroup maxAndMin = new RadioGroup(getActivity());
                        maxAndMin.setOrientation(LinearLayout.VERTICAL);
                        maxAndMin.clearCheck();

                        //maxAndMin.setId(Integer.valueOf("1"));
                        maxAndMin.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
                        Log.d(TAG, "Option length1 "+optionQuantity.getJSONArray("options").length());
                        for (int btnCount =0;btnCount<optionQuantity.getJSONArray("options").length();btnCount++){
                            RadioButton rdbtn = new RadioButton(getActivity());
                            //rdbtn.setId(i);
                            //if (btnCount==0)
                            rdbtn.setText(optionQuantity.getJSONArray("options").getJSONObject(btnCount).getString("name")+" ("+optionQuantity.getJSONArray("options").getJSONObject(btnCount).getString("price")+")");
                            maxAndMin.addView(rdbtn);
                        }
                        llMainLayout.addView(maxAndMin);
                    }else if(optionQuantity.getString("min_selection").equals("0") && optionQuantity.getString("max_selection").equals("1")){
                        RadioGroup maxAndMin = new RadioGroup(getActivity());
                        maxAndMin.setOrientation(LinearLayout.VERTICAL);
                        maxAndMin.clearCheck();
                        //maxAndMin.setId(Integer.valueOf("1"));
                        maxAndMin.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
                        Log.d(TAG, "Option length1 "+optionQuantity.getJSONArray("options").length());
                        for (int btnCount =0;btnCount<optionQuantity.getJSONArray("options").length();btnCount++){
                            RadioButton rdbtn = new RadioButton(getActivity());
                            //rdbtn.setId(i);
                            rdbtn.setText(optionQuantity.getJSONArray("options").getJSONObject(btnCount).getString("name")+" ("+optionQuantity.getJSONArray("options").getJSONObject(btnCount).getString("price")+")");
                            maxAndMin.addView(rdbtn);
                        }
                        llMainLayout.addView(maxAndMin);
                    }else{
                        Log.d(TAG, "Option length2 "+optionQuantity.getJSONArray("options").length());
                        for (int btnCount =0;btnCount<optionQuantity.getJSONArray("options").length();btnCount++){
                            Log.d(TAG, "Option data fetch "+optionQuantity.getJSONArray("options").getJSONObject(btnCount).getString("name"));
                            CheckBox cbMaxOption = new CheckBox(getActivity());
                            cbMaxOption.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
                            cbMaxOption.setText(optionQuantity.getJSONArray("options").getJSONObject(btnCount).getString("name")+" ("+optionQuantity.getJSONArray("options").getJSONObject(btnCount).getString("price")+")");
                            llMainLayout.addView(cbMaxOption);
                        }


                    }
                }

            }else{
                Log.d(TAG, "No product option ");
                rlProductOptionQuantityTxt.setVisibility(View.VISIBLE);
            }

            btnAddToCart = new Button(getActivity());
            btnAddToCart.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
            btnAddToCart.setBackgroundColor(getResources().getColor(R.color.color_btn_login));
            btnAddToCart.setText(getActivity().getResources().getString(R.string.add_to_cart));
            btnAddToCart.setTypeface(Typeface.SERIF);
            btnAddToCart.setTextColor(Color.WHITE);
            llProductOptionsSheet.addView(btnAddToCart);
            btnAddToCart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tvOptionTitleText.setError("please fill all the text");
                }
            });

        }else{
            rlProductOption.setVisibility(View.GONE);
            Toast.makeText(getActivity(),response.getString("message"),Toast.LENGTH_SHORT).show();
        }
    } catch (JSONException e) {
        e.printStackTrace();
        Toast.makeText(getActivity(),"Sorry try Later!!!",Toast.LENGTH_SHORT).show();
        rlProductOption.setVisibility(View.GONE);
    }
}

This is my code in which i have add radio button and checkbox according to json data.So when i click on a button "btnAddToCart" then i want to know all the selected checkbox and radio button.beacuse i have to store all the selected data in my session. 这是我的代码,我根据json数据添加单选按钮和复选框。所以当我点击按钮“btnAddToCart”然后我想知道所有选中的复选框和单选按钮。我必须存储所有选定的数据在我的会议中。

Declare a global variable like: 声明一个全局变量,如:

int rlProductOptionQuantityTxt =0;// for default case int rlProductOptionQuantityTxt = 0; //默认情况下

Use Switch case, 使用Switch case,

@Override public void onClick(View view) { switch (view.getId()) @Override public void onClick(查看视图){switch(view.getId())

{ {

case R.id.yourRadioButton: 案例R.id.yourRadioButton:

           rlProductOptionQuantityTxt =1;

} }

} }

and then you can directly use the value of your global variable. 然后你可以直接使用全局变量的值。

SetGet.setProductOptionQuantityTxt(rlProductOptionQuantityTxt ); SetGet.setProductOptionQuantityTxt(rlProductOptionQuantityTxt);

int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);

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

相关问题 以编程方式使收音机组膨胀时,如何获取所选的单选按钮。 - How do I get the selected radio button when the radio group is inflated programmatically. 如何检查选择了单选组的哪个单选按钮? [安卓] - How to check which radio button of a radio group is selected? [ANDROID] 单击按钮时如何从android的Radio组获取选定的多个单选按钮值的数据 - how to get data of selected multiple radio button values from a Radio Group in android when a button is clicked 如何在android中获取所选单选按钮的ID? - How to get the id of selected radio button in android? 如何在Android中获取所选单选按钮的ID - How to get id of the selected Radio Button in Android Android:当存在3个单选组时,如何仅允许选择1个单选按钮? - Android: How to only allow 1 radio button to be selected when there are 3 radio groups? 我如何知道在Android Studio中选择了哪个单选按钮 - How can I know which radio button is selected in Android studio 使用回收站视图时,如何取回选择了单选按钮的数据 - How to get back the data of which radio button was selected, when using recycler view 如何在Android中获取单选按钮的选定(整数)ID - how to get selected (integer) id of radio button in android Android:如何获取用户选择的动态创建单选按钮的文本? - Android: How to get text of dynamically created radio button selected by the user?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM