下面列出了android.widget.RadioButton#setId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void bindSource(ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater2();
if (mFabLayout == null) {
return;
}
if (inflater == null) {
return;
}
for (MHPlugin source : MHPluginManager.Companion.getINSTANCE().livePlugin()) {
RadioButton item = (RadioButton) inflater.inflate(R.layout.item_source_bar, parent, false);
item.setText(source.getName().substring(0, 2));
item.setTag(source.getName());
item.setId(View.generateViewId());
parent.addView(item);
if (source.getName().equals(currentSource)) {
item.setChecked(true);
}
item.setOnClickListener(v -> {
switchSource((String) v.getTag());
updateTopCategory();
mHelper.refresh();
});
}
}
/**
* Adds radio buttons to the layout based on the texts in the radioButtons array.
* Adds them in the specified index, -1 for the last index.
* If radioButtons is null does nothing.
*
* @param index the index in which to insert the radio buttons
* @param radioButtons the texts of the buttons to add
* @throws IllegalArgumentException if index is less than -1 or greater than the number of radio buttons
*/
public void addButtons(int index, CharSequence... radioButtons) {
if (index < -1 || index > mRadioButtons.size())
throw new IllegalArgumentException("index must be between -1 to getRadioButtonCount() [" +
getRadioButtonCount() + "]: " + index);
if (radioButtons == null)
return;
// creates radio button array
RadioButton[] buttons = new RadioButton[radioButtons.length];
for (int i = 0; i < buttons.length; i++) {
RadioButton radioButton = getRadioButton();
radioButton.setText(radioButtons[i]);
radioButton.setId(generateId());
buttons[i] = radioButton;
}
addButtons(index, buttons);
}
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);
}
/**
* radioButtons 的item必须是以new RadioButton(context)的形式代码生成的
*
* @param radioButtons
*/
public void setSegmentedControl(List<RadioButton> radioButtons) {
segmentedGroup.removeAllViews();
for (int i = 0; i < radioButtons.size(); i++) {
RadioButton tempButton = radioButtons.get(i);
// tempButton.setBackgroundResource(R.drawable.xxx); // 设置RadioButton的背景图片
tempButton.setButtonDrawable(android.R.color.transparent); // 设置按钮的样式
int marginTopBottom = DensityUtils.dip2px(getContext(), 5);
int marginLeftRight = DensityUtils.dip2px(getContext(), 8);
tempButton.setPadding(marginLeftRight, marginTopBottom, marginLeftRight, marginTopBottom); // 设置文字距离按钮四周的距离
tempButton.setText(tempButton.getText());
tempButton.setId(tempButton.getId());
segmentedGroup.addView(tempButton, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
segmentedGroup.updateBackground();
segmentedGroup.invalidate();
}
/**
* Adds radio buttons to the layout based on the texts in the radioButtons array.
* Adds them in the specified index, -1 for the last index.
* If radioButtons is null does nothing.
*
* @param index the index in which to insert the radio buttons
* @param radioButtons the texts of the buttons to add
* @throws IllegalArgumentException if index is less than -1 or greater than the number of radio buttons
*/
public void addButtons(int index, CharSequence... radioButtons) {
if (index < -1 || index > mRadioButtons.size())
throw new IllegalArgumentException("index must be between -1 to getRadioButtonCount() [" +
getRadioButtonCount() + "]: " + index);
if (radioButtons == null)
return;
// creates radio button array
RadioButton[] buttons = new RadioButton[radioButtons.length];
for (int i = 0; i < buttons.length; i++) {
RadioButton radioButton = getRadioButton();
radioButton.setText(radioButtons[i]);
radioButton.setId(generateId());
buttons[i] = radioButton;
}
addButtons(index, buttons);
}
@Test
public void multiLineRadioGroupOnCheckChangeEmitOnlyCheckEvent() throws InterruptedException {
Context appContext = InstrumentationRegistry.getTargetContext();
MultiLineRadioGroup radioGroup = new MultiLineRadioGroup(appContext);
MultilineOnCheckedChangeListener listener = new MultilineOnCheckedChangeListener();
radioGroup.setOnCheckedChangeListener(listener);
final RadioButton firstButton = new RadioButton(appContext);
firstButton.setText("first");
firstButton.setId(R.id.multi_line_radio_group_default_radio_button);
radioGroup.addView(firstButton);
final RadioButton secondButton = new RadioButton(appContext);
secondButton.setText("second");
secondButton.setId(R.id.multi_line_radio_group_default_table_row);
radioGroup.addView(secondButton);
clickButton(secondButton);
clickButton(firstButton);
assertEquals(2, listener.count);
assertEquals(Arrays.asList("second", "first"), listener.buttonText);
}
@Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState);
mCancel = findViewById(R.id.cancel);
mRg = findViewById(R.id.rg);
mCancel.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
dismiss();
}
});
for (int i = 0, count = mRg.getChildCount(); i < count; i++) {
RadioButton rb = (RadioButton) mRg.getChildAt(i);
rb.setId(i);
}
mRg.setOnCheckedChangeListener(this);
}
/**
* This method is used to display all the rides available to specific destination
* @param rideInfos all the rides that are available
*/
private void addRadioButton(RideInfo[] rideInfos) {
RadioGroup radioGroup= (RadioGroup) findViewById(R.id.RadioButtonGroup);
RadioGroup.LayoutParams rprms;
radioGroup.removeAllViews();
if(rideInfos.length>0)
{
for(int i=0;i<(rideInfos.length);i++){
RadioButton radioButton = new RadioButton(this);
radioButton.setText("CAR NO- " + rideInfos[i].getCar_Num() + " -Source- " + rideInfos[i].getSource() + " -Destination- " + rideInfos[i].getDestination() + " -Time- " + rideInfos[i].getRideTime());
radioButton.setId(i);
rprms= new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
radioGroup.addView(radioButton, rprms);
}
}
else
{
Toast.makeText(SearchActivity.this, "No Rides available to destination.. Please try again", Toast.LENGTH_SHORT).show();
}
}
protected RadioButton getRadioButton(Integer id, String text, String tag, String value) {
RadioButton radioButton = new RadioButton(getActivity());
radioButton.setId(id);
radioButton.setText(text);
radioButton.setTag(tag);
radioButton.setTextAppearance(getActivity(), R.style.RadioButton_Full);
radioButton.setLayoutParams(getLayoutParams(labelDescription.getPaddingLeft(), labelDescription.getPaddingLeft() / 2, labelDescription.getPaddingRight(), labelDescription.getPaddingLeft() / 2));
if (value != null && value.equals(tag)) {
radioButton.setChecked(true);
}
else {
radioButton.setChecked(false);
}
return radioButton;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String[] renderingModes = getResources().getStringArray(R.array.pref_viewer_rendering_entries);
RadioGroup radioGrp = new RadioGroup(getContext());
for (int i = 0; i < renderingModes.length; i++) {
// No smooth mode for Android 5
if (1 == i && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) continue;
RadioButton radioButton = new RadioButton(getContext());
radioButton.setText(renderingModes[i]);
radioButton.setId(i);
radioGrp.addView(radioButton);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) radioGrp.check(0);
else radioGrp.check(renderingMode);
DialogInterface.OnClickListener onOkClick = (dialog, whichButton) -> {
Map<String, String> newPrefs = new HashMap<>();
newPrefs.put(Preferences.Key.PREF_VIEWER_RENDERING, radioGrp.getCheckedRadioButtonId() + "");
parent.onBookPreferenceChanged(newPrefs);
dismiss();
};
return new MaterialAlertDialogBuilder(requireContext())
.setView(radioGrp)
.setPositiveButton(android.R.string.ok, onOkClick)
.setNegativeButton(android.R.string.cancel, null)
.create();
}
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);
}
private void populateColorsRadioGroup(RadioGroup group, Set<RadioButtons.ColorsInfo> colorsInfos) {
for (RadioButtons.ColorsInfo colorsInfo : colorsInfos) {
final RadioButton button = new RadioButton(this);
final String text = getResources().getString(colorsInfo.colorTextResId);
final int color = getResources().getColor(colorsInfo.primaryColorResId);
final int textColor = color == getResources().getColor(R.color.fab_material_white) ?
getResources().getColor(R.color.fab_material_black) : color;
button.setId(IdGenerator.next());
button.setText(text);
button.setTextColor(textColor);
button.setTag(colorsInfo);
group.addView(button);
}
}
@Test
public void multiLineRadioGroupOnCheckedChangedEmitOnChecked() throws InterruptedException {
Context appContext = InstrumentationRegistry.getTargetContext();
MultiLineRadioGroup radioGroup = new MultiLineRadioGroup(appContext);
MultilineOnCheckedChangeListener listener = new MultilineOnCheckedChangeListener();
radioGroup.setOnCheckedChangeListener(listener);
final RadioButton firstButton = new RadioButton(appContext);
firstButton.setText("first");
firstButton.setId(R.id.multi_line_radio_group_default_radio_button);
radioGroup.addView(firstButton);
radioGroup.check(R.id.multi_line_radio_group_default_radio_button);
assertEquals(1, listener.count);
assertEquals(Collections.singletonList("first"), listener.buttonText);
radioGroup.clearCheck();
assertEquals(2, listener.count);
assertEquals(Arrays.asList("first", "first"), listener.buttonText);
radioGroup.check("first");
assertEquals(3, listener.count);
assertEquals(Arrays.asList("first", "first", "first"),
listener.buttonText);
radioGroup.clearCheck();
assertEquals(4, listener.count);
assertEquals(Arrays.asList("first", "first", "first", "first"),
listener.buttonText);
radioGroup.checkAt(0);
assertEquals(5, listener.count);
assertEquals(Arrays.asList("first", "first", "first", "first", "first"),
listener.buttonText);
}
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);
}
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);
}
public SelectOneWidget(Context context, FormEntryPrompt prompt) {
super(context, prompt);
int padding = (int)Math.floor(context.getResources().getDimension(R.dimen.select_padding));
mItems = getSelectChoices();
buttons = new Vector<>();
String s = null;
if (prompt.getAnswerValue() != null) {
s = prompt.getAnswerValue().uncast().getString();
}
//Is this safe enough from collisions?
buttonIdBase = Math.abs(prompt.getIndex().hashCode());
if (mItems != null) {
for (int i = 0; i < mItems.size(); i++) {
final RadioButton rb = new RadioButton(getContext());
String markdownText = prompt.getSelectItemMarkdownText(mItems.get(i));
if (markdownText != null) {
rb.setText(forceMarkdown(markdownText));
} else {
rb.setText(prompt.getSelectChoiceText(mItems.get(i)));
}
rb.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontSize);
rb.setId(i + buttonIdBase);
rb.setEnabled(!prompt.isReadOnly());
rb.setFocusable(!prompt.isReadOnly());
rb.setBackgroundResource(R.drawable.selector_button_press);
buttons.add(rb);
if (mItems.get(i).getValue().equals(s)) {
rb.setChecked(true);
}
//Move to be below the above setters. Not sure if that will cause
//problems, but I don't think it should.
rb.setOnCheckedChangeListener(this);
String audioURI =
prompt.getSpecialFormSelectChoiceText(mItems.get(i),
FormEntryCaption.TEXT_FORM_AUDIO);
String imageURI =
prompt.getSpecialFormSelectChoiceText(mItems.get(i),
FormEntryCaption.TEXT_FORM_IMAGE);
String videoURI = prompt.getSpecialFormSelectChoiceText(mItems.get(i), "video");
String bigImageURI = prompt.getSpecialFormSelectChoiceText(mItems.get(i), "big-image");
MediaLayout mediaLayout = MediaLayout.buildAudioImageVisualLayout(getContext(), rb, audioURI, imageURI, videoURI, bigImageURI);
mediaLayout.setPadding(0, padding, 0, padding);
mediaLayout.setEnabled(!mPrompt.isReadOnly());
mediaLayout.setOnClickListener(v -> rb.performClick());
addView(mediaLayout);
// Last, add the dividing line (except for the last element)
ImageView divider = new ImageView(getContext());
divider.setBackgroundResource(android.R.drawable.divider_horizontal_bright);
if (i != mItems.size() - 1) {
addView(divider);
}
}
addClearButton(context, s != null && !prompt.isReadOnly());
}
}
/**
* 设置笑脸被点击后的表情数据
*/
private void setSmilingfaceData() {
mV_myScrollView = new MyScrollView(this);
mList_emoji = FaceConversionUtil.getInstace().emojiLists;
// 添加表情页
mList_emojiAdapter = new ArrayList<EmojiAdapter>();
mV_myScrollView.removeAllViews();
for (int i = 0; i < mList_emoji.size(); i++) {
// GridView的一些特殊属性:
//
// 1.android:numColumns=”auto_fit” //GridView的列数设置为自动
// 2.android:columnWidth=”90dp " //每列的宽度,也就是Item的宽度
// 3.android:stretchMode=”columnWidth"//缩放与列宽大小同步
// 4.android:verticalSpacing=”10dp” //两行之间的边距
// 5.android:horizontalSpacing=”10dp” //两列之间的边距
// 6.android:cacheColorHint="#00000000" //去除拖动时默认的黑色背景
// 7.android:listSelector="#00000000" //去除选中时的黄色底色
// 8.android:scrollbars="none" //隐藏GridView的滚动条
// 9.android:fadeScrollbars="true" //设置为true就可以实现滚动条的自动隐藏和显示
// 10.android:fastScrollEnabled="true" //GridView出现快速滚动的按钮(至少滚动4页才会显示)
// 11.android:fadingEdge="none" //GridView衰落(褪去)边缘颜色为空,缺省值是vertical。(可以理解为上下边缘的提示色)
// 12.android:fadingEdgeLength="10dip" //定义的衰落(褪去)边缘的长度
// 13.android:stackFromBottom="true" //设置为true时,你做好的列表就会显示你列表的最下面
// 14.android:transcriptMode="alwaysScroll" //当你动态添加数据时,列表将自动往下滚动最新的条目可以自动滚动到可视范围内
// 15.android:drawSelectorOnTop="false" //点击某条记录不放,颜色会在记录的后面成为背景色,内容的文字可见(缺省为false)
//
GridView view = new GridView(this);
EmojiAdapter adapter = new EmojiAdapter(this, mList_emoji.get(i));
view.setAdapter(adapter);
mList_emojiAdapter.add(adapter);
view.setOnItemClickListener(this);
view.setNumColumns(7);
view.setBackgroundColor(Color.TRANSPARENT);
// view.setHorizontalSpacing(1); //两列之间的边距
// view.setVerticalSpacing(10);//两行之间的边距
view.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);//缩放与列宽大小同步
view.setCacheColorHint(0);//去除拖动时默认的黑色背景
// view.setPadding(5, 5, 5, 5);
view.setSelector(new ColorDrawable(Color.TRANSPARENT));
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
view.setLayoutParams(params);
view.setGravity(Gravity.CENTER);
mV_myScrollView.addView(view);
}
mLl_chat_smilingface_body.removeAllViews();
mLl_chat_smilingface_body.addView(mV_myScrollView);//将MyScrollView添加到内容显示区
RadioGroup.LayoutParams params_rb = new RadioGroup.LayoutParams(DensityUtil.dip2px(this, 8), DensityUtil.dip2px(this, 8));
int marginValue = DensityUtil.dip2px(this, 3);
params_rb.setMargins(marginValue, 0, marginValue, 0);
for (int i = 0; i < mV_myScrollView.getChildCount(); i++) {
RadioButton rbtn = new RadioButton(this);
rbtn.setButtonDrawable(R.drawable.cgt_selector_chat_radiobtn_bg);
rbtn.setId(i);
mRg_chat_smilingface_tab.addView(rbtn, params_rb);
if (i == 0) {
rbtn.setChecked(true);
}
}
/**
* 监听单选按钮是否被选中,
*/
mRg_chat_smilingface_tab.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
current = checkedId;
mV_myScrollView.moveToDest(checkedId);
}
});
/**
*
*/
mV_myScrollView.setChangedListener(new IPageChangedListener() {
@Override
public void changedTo(int pageId) {
current = pageId;
((RadioButton) mRg_chat_smilingface_tab.getChildAt(pageId)).setChecked(true);
}
});
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Context context = getActivity();
qualityForm = new SeekBarForm(false);
qualityForm.setConfiguration(1, 100, 1, 1);
qualityForm.setValueFormat(getString(R.string.text_quality_format));
qualityForm.setCurrentValue(savedInstanceState != null ? savedInstanceState.getInt(EXTRA_QUALITY) : 100);
reduceForm = new SeekBarForm(false);
reduceForm.setConfiguration(1, 8, 1, 1);
reduceForm.setValueFormat(getString(R.string.text_reduce_format));
reduceForm.setCurrentValue(savedInstanceState != null ? savedInstanceState.getInt(EXTRA_REDUCE) : 1);
int padding = getResources().getDimensionPixelSize(R.dimen.dialog_padding_view);
View qualityView = qualityForm.inflate(context);
qualityForm.getSeekBar().setSaveEnabled(false);
qualityView.setPadding(qualityView.getPaddingLeft(), 0, qualityView.getPaddingRight(), padding / 2);
View reduceView = reduceForm.inflate(context);
reduceForm.getSeekBar().setSaveEnabled(false);
reduceView.setPadding(reduceView.getPaddingLeft(), 0, reduceView.getPaddingRight(),
reduceView.getPaddingBottom());
radioGroup = new RadioGroup(context);
radioGroup.setOrientation(RadioGroup.VERTICAL);
radioGroup.setPadding(padding, padding, padding, padding / 2);
radioGroup.setOnCheckedChangeListener(this);
for (int i = 0; i < OPTIONS.length; i++) {
RadioButton radioButton = new RadioButton(context);
radioButton.setText(OPTIONS[i]);
radioButton.setId(IDS[i]);
radioGroup.addView(radioButton);
}
radioGroup.check(IDS[0]);
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
FrameLayout qualityLayout = new FrameLayout(context);
qualityLayout.setId(android.R.id.text1);
qualityLayout.addView(qualityView);
FrameLayout reduceLayout = new FrameLayout(context);
reduceLayout.setId(android.R.id.text2);
reduceLayout.addView(reduceView);
linearLayout.addView(radioGroup, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(qualityLayout, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(reduceLayout, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ScrollView scrollView = new ScrollView(context);
scrollView.addView(linearLayout, ScrollView.LayoutParams.MATCH_PARENT,
ScrollView.LayoutParams.WRAP_CONTENT);
return new AlertDialog.Builder(context).setTitle(R.string.text_reencode_image)
.setView(scrollView).setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok, this).create();
}