![](/img/trans.png)
[英]How do I get the selected radio button when the radio group is inflated programmatically.
[英]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);
}
}
這是我的代碼,我根據json數據添加單選按鈕和復選框。所以當我點擊按鈕“btnAddToCart”然后我想知道所有選中的復選框和單選按鈕。我必須存儲所有選定的數據在我的會議中。
聲明一個全局變量,如:
int rlProductOptionQuantityTxt = 0; //默認情況下
使用Switch case,
@Override public void onClick(查看視圖){switch(view.getId())
{
案例R.id.yourRadioButton:
rlProductOptionQuantityTxt =1;
}
}
然后你可以直接使用全局變量的值。
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.