final RadioButton radioButton = new RadioButton(getActivity()); radioButton.setText(string); radioButton.setId(ViewUtils.generateViewId()); radioGroup.addView(radioButton);
radioButton.setCompoundDrawablesWithIntrinsicBounds(item.icon, 0, 0, 0); radioButton.setChecked(false); radioButton.setId(id++); radioButton.setLayoutParams(new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); radioButton.setOnClickListener(radioButtonsClickListener);
@Test public void shouldInformRadioGroupThatItIsChecked() throws Exception { RadioButton radioButton1 = new RadioButton(context); radioButton1.setId(99); RadioButton radioButton2 = new RadioButton(context); radioButton2.setId(100); RadioGroup radioGroup = new RadioGroup(context); radioGroup.addView(radioButton1); radioGroup.addView(radioButton2); radioButton1.setChecked(true); assertThat(radioGroup.getCheckedRadioButtonId()).isEqualTo(radioButton1.getId()); radioButton2.setChecked(true); assertThat(radioGroup.getCheckedRadioButtonId()).isEqualTo(radioButton2.getId()); }
for (int i = 0; i < address.length; i++) { RadioButton rdbtn = new RadioButton(view.getContext()); rdbtn.setId(i); rdbtn.setText(address[i]); if (h.equals(address[i])) {
final Context context; /* get Context from somewhere */ final LinearLayout layout = (LinearLayout)findViewById(R.id.your_layout); final RadioGroup group = new RadioGroup(context); final RadioButton button1 = new RadioButton(context); button1.setId(button1_id); // this id can be generated as you like. group.addView(button1, new RadioGroup.LayoutParams( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT)); final RadioButton button2 = new RadioButton(context); button1.setId(button2_id); // this id can be generated as you like. button2.setChecked(true); group.addView(button2, new RadioGroup.LayoutParams( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT)); layout.addView(group, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
public static void showRadioButtonDialog(Context mContext, String[] options, String title, final RadioGroup.OnCheckedChangeListener listener) { LayoutInflater inflater = (LayoutInflater)mContext.getSystemService (Context.LAYOUT_INFLATER_SERVICE); View v = inflater.inflate(R.layout.filter_options, null); // custom dialog final Dialog dialog = new AlertDialog.Builder(mContext) .setTitle(title) .setView(v) .create(); RadioGroup rg = (RadioGroup) v.findViewById(R.id.filter_group); BootstrapButton okButton = (BootstrapButton) v.findViewById(R.id.okbutton); for(int i=0;i<options.length;i++){ RadioButton rb=new RadioButton(mContext); // dynamically creating RadioButton and adding to RadioGroup. rb.setText(options[i]); rb.setId(i); rg.addView(rb); } rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { listener.onCheckedChanged(radioGroup,i); } }); okButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } }); dialog.show(); }
private void initData() { try { JSONArray choiceArray = new JSONArray(choices); for (int i = 0; i < choiceArray.length(); i++) { RadioButton radioButton = (RadioButton) getLayoutInflater().inflate(R.layout.mq_item_form_radio_btn, null); radioButton.setText(choiceArray.getString(i)); radioButton.setTag(choiceArray.get(i)); radioButton.setId(View.NO_ID); radioButton.setOnCheckedChangeListener(this); MQUtils.tintCompoundButton(radioButton, R.drawable.mq_radio_btn_uncheck, R.drawable.mq_radio_btn_checked); radioGroup.addView(radioButton, LinearLayout.LayoutParams.MATCH_PARENT, MQUtils.dip2px(MQCollectInfoActivity.this, 48)); } } catch (JSONException e) { rootView.setVisibility(View.GONE); e.printStackTrace(); } }
private void init() { mFm = getSupportFragmentManager(); mNavigationBar = (RadioGroup) findViewById(R.id.navigation); int size = (int) getResources().getDimension(R.dimen.navigation_top_icon_size); for (int i = 0, count = mNavigationBar.getChildCount(); i < count; i++) { RadioButton rb = (RadioButton) mNavigationBar.getChildAt(i); Drawable topIcon = getResources().getDrawable(R.drawable.selector_navigation_bg); topIcon.setBounds(0, 0, size, size); rb.setCompoundDrawables(null, topIcon, null, null); rb.setId(i); } mNavigationBar.setOnCheckedChangeListener(this); ((RadioButton) mNavigationBar.getChildAt(0)).setChecked(true); }
@SuppressLint("NewApi") public void onChildViewAdded(View parent, View child) { if (parent == KeyRadioGroupV1.this ) { List<RadioButton> allRadioButton = getAllRadioButton(child); if(allRadioButton != null && allRadioButton.size() > 0){ for(RadioButton button : allRadioButton){ int id = button.getId(); // generates an id if it's missing if (id == View.NO_ID) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { // generateViewId() 这个方法最低需要API17 这个方法用于生成ID , 这里做一下向下兼容 id = View.generateViewId(); } else { id = child.hashCode(); } button.setId(id); } // 不知道为什么setOnCheckedChangeWidgetListener方法不能调 RadioButton 的父类CompoundButton中是有这个方法的 button.setOnCheckedChangeListener( mChildOnCheckedChangeListener); } } } if (mOnHierarchyChangeListener != null) { mOnHierarchyChangeListener.onChildViewAdded(parent, child); } }
private void AddButton(String text, int id) { RadioButton newRadioButton = new RadioButton(this); newRadioButton.setText(text); newRadioButton.setId(id); LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT); radioGroup.addView(newRadioButton); }
private void AddButton(String text, int id) { RadioButton newRadioButton = new RadioButton(this); newRadioButton.setText(text); newRadioButton.setId(id); LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT); radioGroup.addView(newRadioButton); }
/** * {@inheritDoc} */ public void onChildViewAdded(View parent, View child) { if (parent == MultiRadioGroup.this ) { List<RadioButton> btns = getAllRadioButton(child); if(btns != null && btns.size() > 0){ for(RadioButton btn : btns){ int id = btn.getId(); // generates an id if it's missing if (id == View.NO_ID && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { id = View.generateViewId(); btn.setId(id); } btn.setOnCheckedChangeListener( mChildOnCheckedChangeListener); } } } if (mOnHierarchyChangeListener != null) { mOnHierarchyChangeListener.onChildViewAdded(parent, child); } }
private void createTipTextLocationChoices(LinearLayout linearLayout){ RadioGroup radioGroup = new RadioGroup(this); radioGroup.setOrientation(LinearLayout.HORIZONTAL); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.radio_below: cameraMask.setTextLocation(CameraMask.LOCATION_BELOW_CAMERA_LENS); break; case R.id.radio_above: cameraMask.setTextLocation(CameraMask.LOCATION_ABOVE_CAMERA_LENS); break; } } }); linearLayout.addView(radioGroup, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); RadioButton button1 = new RadioButton(this); button1.setId(R.id.radio_below); button1.setTextColor(Color.WHITE); button1.setChecked(true); button1.setText("BelowCameraLens"); RadioButton button2 = new RadioButton(this); button2.setId(R.id.radio_above); button2.setTextColor(Color.WHITE); button2.setText("AboveCameraLens"); radioGroup.addView(button1); radioGroup.addView(button2); }
private void pairedBluetooth() { viewPairedDevices.removeAllViews(); Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); if (null != pairedDevices && pairedDevices.size() > 0) { StringBuilder msgBuilder = new StringBuilder(); int index = 0; for (BluetoothDevice device : pairedDevices) { RadioButton radioButton = new RadioButton(this); radioButton.setId(index++); radioButton.setText(String.format("%s\n%s", device.getName(), device.getAddress())); radioButton.setTag(device); viewPairedDevices.addView(radioButton); msgBuilder.append(String.format("%s %s\n", device.getName(), device.getAddress())); } tvContent.setText(msgBuilder.toString()); viewPairedDevices.setVisibility(View.VISIBLE); } else { viewPairedDevices.setVisibility(View.GONE); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.radio_group_1); mRadioGroup = (RadioGroup) findViewById(R.id.menu); // test adding a radio button programmatically RadioButton newRadioButton = new RadioButton(this); newRadioButton.setText(R.string.radio_group_snack); newRadioButton.setId(R.id.snack); LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT); mRadioGroup.addView(newRadioButton, 0, layoutParams); // test listening to checked change events String selection = getString(R.string.radio_group_selection); mRadioGroup.setOnCheckedChangeListener(this); mChoice = (TextView) findViewById(R.id.choice); mChoice.setText(selection + mRadioGroup.getCheckedRadioButtonId()); // test clearing the selection Button clearButton = (Button) findViewById(R.id.clear); clearButton.setOnClickListener(this); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.radio_group_1); mRadioGroup = (RadioGroup) findViewById(R.id.menu); // 测试以编程方式添加一个单选按钮 test adding a radio button programmatically RadioButton newRadioButton = new RadioButton(this); newRadioButton.setText(R.string.radio_group_snack); newRadioButton.setId(R.id.snack); LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT); mRadioGroup.addView(newRadioButton, 0, layoutParams); // 测试听检查更改事件 test listening to checked change events String selection = getString(R.string.radio_group_selection); mRadioGroup.setOnCheckedChangeListener(this); mChoice = (TextView) findViewById(R.id.choice); mChoice.setText(selection + mRadioGroup.getCheckedRadioButtonId()); // 选择测试清理 test clearing the selection Button clearButton = (Button) findViewById(R.id.clear); clearButton.setOnClickListener(this); }
/** * 创建子 tab * * @param context * @param id */ public static RadioButton creatTabItem(Context context, int id, String text) { final int width = DensityUtil.dip2px(context, 80); final int height = DensityUtil.dip2px(context, 46); final int padding = DensityUtil.dip2px(context, 3); RadioButton button; button = new RadioButton(context); button.setId(id); // button.setWidth(width); // button.setHeight(height); button.setText(text); button.setLayoutParams(new LinearLayout.LayoutParams(width, height)); button.setHeight(height); button.setBackgroundResource(R.drawable.tab_item_bg_selector); button.setButtonDrawable(context.getResources().getDrawable(android.R.color.transparent)); button.setGravity(Gravity.CENTER); button.setTextColor(context.getResources().getColorStateList(R.color.tab_item_font_color_selector)); button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); button.setPadding(0, 0, 0, padding); return button; } }
@Override RadioGroup createWidgetView(AppCompatActivity activity) { radioGroup = new RadioGroup(activity); radioGroup.setPadding(16, 16, 16, 16); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); layoutParams.topMargin = 32; layoutParams.bottomMargin = 32; String[] values = getInputValues(activity.getIntent()); for (int j = 0; j < values.length; ++j) { String value = values[j]; RadioButton button = new RadioButton(activity); button.setText(value); button.setId(j); button.setTextSize(18); button.setPadding(16, 16, 16, 16); button.setLayoutParams(layoutParams); radioGroup.addView(button); } return radioGroup; }
private void refreshTrezorsList() { ImmutableList<InitializedTrezor> trezors = gct.getCommonDb().getTrezors(); if (radioGroupTrezors.getChildCount() > trezors.size()) { radioGroupTrezors.removeViews(trezors.size(), radioGroupTrezors.getChildCount() - trezors.size()); } LayoutInflater inflater = LayoutInflater.from(radioGroupTrezors.getContext()); for (int i = radioGroupTrezors.getChildCount(); i < trezors.size(); i++) { RadioButton btn = (RadioButton)inflater.inflate(R.layout.nav_drawer_trezor_radiobutton, radioGroupTrezors, false); btn.setCompoundDrawables(createNavDrawerButtonIcon(R.drawable.ic_trezor), null, null, null); btn.setOnClickListener(onTrezorClickListener); radioGroupTrezors.addView(btn); } for (int i = 0; i < trezors.size(); i++) { InitializedTrezor t = trezors.get(i); RadioButton btn = (RadioButton)radioGroupTrezors.getChildAt(i); btn.setText(TextUtils.isEmpty(t.getFeatures().getLabel()) ? btn.getContext().getString(R.string.init_trezor_device_label_default) : t.getFeatures().getLabel()); int id = getTrezorButtonId(t.getFeatures().getDeviceId()); btn.setId(id); } refreshCurrentSelectedTrezor(gct.getCommonDb().getLastSelectedDeviceId()); radioGroupTrezors.setVisibility(trezors.isEmpty() ? View.GONE : View.VISIBLE); dividerTrezors.setVisibility(trezors.isEmpty() ? View.GONE : View.VISIBLE); }