下面列出了怎么用android.widget.RadioButton的API类实例代码及写法,或者点击链接到github查看源代码。
private void initViews() {
mStoreLocText = (TextView)findViewById(R.id.store_loc_txt);
mStoreSizeText = (TextView)findViewById(R.id.store_size);
mOpenFileText = (TextView)findViewById(R.id.open_file_txt);
mClearDownloadText = (TextView)findViewById(R.id.clear_download_cache);
mBtn1 = (RadioButton)findViewById(R.id.btn1);
mBtn2 = (RadioButton)findViewById(R.id.btn2);
mBtn3 = (RadioButton)findViewById(R.id.btn3);
mBtn4 = (RadioButton)findViewById(R.id.btn4);
mBtn5 = (RadioButton)findViewById(R.id.btn5);
mBtn11 = (RadioButton)findViewById(R.id.btn11);
mBtn12 = (RadioButton)findViewById(R.id.btn12);
mStoreLocText.setText(
VideoDownloadManager.getInstance().getCacheFilePath());
mOpenFileText.setOnClickListener(this);
mClearDownloadText.setOnClickListener(this);
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this);
mBtn3.setOnClickListener(this);
mBtn4.setOnClickListener(this);
mBtn5.setOnClickListener(this);
mBtn11.setOnClickListener(this);
mBtn12.setOnClickListener(this);
}
private void initView() {
selectableTextView = (SelectableTextView) findViewById(R.id.ctv_content);
selectableTextView.setText(Html.fromHtml(StringContentUtil.str_hanzi).toString());
selectableTextView.clearFocus();
selectableTextView.setTextJustify(true);
selectableTextView.setForbiddenActionMenu(false);
selectableTextView.setCustomActionMenuCallBack(this);
selectableTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(HoriActivity.this, "SelectableTextView 的onClick事件", Toast.LENGTH_SHORT).show();
}
});
rg_text_gravity = (RadioGroup) findViewById(R.id.rg_text_gravity);
rg_text_content = (RadioGroup) findViewById(R.id.rg_text_content);
((RadioButton) findViewById(R.id.rb_justify)).setChecked(true);
((RadioButton) findViewById(R.id.rb_hanzi)).setChecked(true);
rg_text_gravity.setOnCheckedChangeListener(this);
rg_text_content.setOnCheckedChangeListener(this);
}
@SuppressLint("NewApi")
public static void setTint(RadioButton radioButton, int color) {
ColorStateList sl = new ColorStateList(new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
}, new int[]{
DialogUtils.resolveColor(radioButton.getContext(), R.attr.colorControlNormal),
color
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
radioButton.setButtonTintList(sl);
} else {
Drawable d = DrawableCompat.wrap(ContextCompat.getDrawable(radioButton.getContext(), R.drawable.abc_btn_radio_material));
DrawableCompat.setTintList(d, sl);
radioButton.setButtonDrawable(d);
}
}
private void initTab() {
int padding = Tools.dip2px(this, 10);
for (int i = 0; i < 20; i++) {
RadioButton rb = new RadioButton(this);
rb.setPadding(padding, 0, padding, 0);
rb.setButtonDrawable(null);
rb.setGravity(Gravity.CENTER);
rb.setTag(i * 5);
rb.setText("Group " + i);
rb.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
try {
rb.setTextColor(getResources().getColorStateList(R.color.bg_tab_text));
} catch (Exception e) {
e.printStackTrace();
}
rb.setCompoundDrawablesWithIntrinsicBounds(null, null, null, getResources().getDrawable(R.drawable.bg_block_tab));
rb.setOnCheckedChangeListener(onCheckedChangeListener);
rg_tab.addView(rb);
}
((RadioButton) rg_tab.getChildAt(0)).setChecked(true);
}
static boolean isTrackEvent(View view, boolean isFromUser) {
if (view instanceof CheckBox) {
if (!isFromUser) {
return false;
}
} else if (view instanceof RadioButton) {
if (!isFromUser) {
return false;
}
} else if (view instanceof ToggleButton) {
if (!isFromUser) {
return false;
}
} else if (view instanceof CompoundButton) {
if (!isFromUser) {
return false;
}
}
if (view instanceof RatingBar) {
if (!isFromUser) {
return false;
}
}
return true;
}
/**
* Tint the radio button
*
* @param radioButton the radio button
* @param color the color
*/
public static void tint(@NonNull RadioButton radioButton, @ColorInt int color) {
final int disabledColor = getDisabledColor(radioButton.getContext());
ColorStateList sl = new ColorStateList(new int[][]{
new int[]{android.R.attr.state_enabled, -android.R.attr.state_checked},
new int[]{android.R.attr.state_enabled, android.R.attr.state_checked},
new int[]{-android.R.attr.state_enabled, -android.R.attr.state_checked},
new int[]{-android.R.attr.state_enabled, android.R.attr.state_checked}
}, new int[]{
getThemeAttrColor(radioButton.getContext(), R.attr.colorControlNormal),
color,
disabledColor,
disabledColor
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
radioButton.setButtonTintList(sl);
} else {
Drawable radioDrawable = ContextCompat.getDrawable(radioButton.getContext(), R.drawable.abc_btn_radio_material);
Drawable d = DrawableCompat.wrap(radioDrawable);
DrawableCompat.setTintList(d, sl);
radioButton.setButtonDrawable(d);
}
}
@Override
protected void initViews() {
mIvAvater = (ImageView) findViewById(R.id.setting_my_avater_img);
mEtNickname = (EditText) findViewById(R.id.setting_my_nickname);
mRgGender = (RadioGroup) findViewById(R.id.setting_baseinfo_rg_gender);
mHtvConstellation = (TextView) findViewById(R.id.setting_birthday_htv_constellation);
mHtvAge = (TextView) findViewById(R.id.setting_birthday_htv_age);
mDpBirthday = (DatePicker) findViewById(R.id.setting_birthday_dp_birthday);
mRbBoy = (RadioButton) findViewById(R.id.setting_baseinfo_rb_male);
mRbGirl = (RadioButton) findViewById(R.id.setting_baseinfo_rb_female);
mBtnBack = (Button) findViewById(R.id.setting_btn_back);
mBtnNext = (Button) findViewById(R.id.setting_btn_next);
}
public void clear() {
switch(questionDef.getControlType()) {
case org.javarosa.core.model.Constants.CONTROL_INPUT:
((EditText) answerHolder).setText("");
break;
case org.javarosa.core.model.Constants.CONTROL_SELECT_ONE:
RadioGroup rg = ((RadioGroup) answerHolder);
for(int r = 0; r < selectChoiceText.size(); r++) {
((RadioButton) rg.getChildAt(r)).setChecked(false);
}
break;
case org.javarosa.core.model.Constants.CONTROL_SELECT_MULTI:
LinearLayout ll = ((LinearLayout) answerHolder);
for(int c = 0; c < selectChoiceText.size(); c++) {
((CheckBox) ll.getChildAt(c)).setChecked(false);
}
answerHolder = ll;
break;
case org.javarosa.core.model.Constants.CONTROL_AUDIO_CAPTURE:
((ODKSeekBar) answerHolder).rawAudioData = null;
break;
}
}
@OnCheckedChanged({ R.id.target_align_start, R.id.target_align_center, R.id.target_align_end })
void onTargetAlignmentChanged(RadioButton button, boolean isChecked) {
if (isChecked) {
int alignment = Gravity.NO_GRAVITY;
switch (button.getId()) {
case R.id.target_align_start:
alignment = Gravity.START;
break;
case R.id.target_align_center:
alignment = Gravity.CENTER_HORIZONTAL;
break;
case R.id.target_align_end:
alignment = Gravity.END;
break;
}
onUpdateListener.updateTargetTextAlignment(alignment);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = convertView != null ? convertView
: mInflater.inflate(mTextViewResourceId, null);
if (position < 0 || position >= mItemsList.size()) return view;
final ImeSubtypeListItem item = mItemsList.get(position);
final CharSequence imeName = item.mImeName;
final CharSequence subtypeName = item.mSubtypeName;
final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
if (TextUtils.isEmpty(subtypeName)) {
firstTextView.setText(imeName);
secondTextView.setVisibility(View.GONE);
} else {
firstTextView.setText(subtypeName);
secondTextView.setText(imeName);
secondTextView.setVisibility(View.VISIBLE);
}
final RadioButton radioButton =
(RadioButton)view.findViewById(com.android.internal.R.id.radio);
radioButton.setChecked(position == mCheckedItem);
return view;
}
public void init(Context mContext, OnEmoticonTapListener mTapListener, Resources resources) {
this.mContext = mContext;
this.mTapListener = mTapListener;
this.resources = resources;
parser = EmojiParser.getInstance(mContext);
mEmojiGridView = (GridView) this.findViewById(R.id.message_facebar_gv_emotes);
adapter = new EmojiAdapter();
mEmojiGridView.setAdapter(adapter);
mEmojiGridView.setOnItemClickListener(this);
mEmojiGroup = (RadioGroup) findViewById(R.id.message_facebar_radiobutton_type);
mEmojiGroup.setOnCheckedChangeListener(this);
mEmojiRadio1 = (RadioButton) this.findViewById(R.id.emote_radio_1);
mEmojiRadio1.setOnClickListener(this);
mEmojiRadio1.setSelected(true);
mEmojiRadio1.setChecked(true);
mEmojiRadio2 = (RadioButton) this.findViewById(R.id.emote_radio_2);
mEmojiRadio2.setOnClickListener(this);
mEmojiRadio3 = (RadioButton) this.findViewById(R.id.emote_radio_3);
mEmojiRadio3.setOnClickListener(this);
mEmojiRadio4 = (RadioButton) this.findViewById(R.id.emote_radio_4);
mEmojiRadio4.setOnClickListener(this);
mEmojiRadio5 = (RadioButton) this.findViewById(R.id.emote_radio_5);
mEmojiRadio5.setOnClickListener(this);
mEmojiDelte = (ImageView) this.findViewById(R.id.emote_radio_delete);
mEmojiDelte.setOnClickListener(this);
}
private void initView() {
pickerTitleName = (TextView) view.findViewById(R.id.pickerTitleName);
pickerConfirm = (TextView) view.findViewById(R.id.pickerConfirm);
groupSelect = (RadioGroup) view.findViewById(R.id.groupSelect);
mTextFirst = (RadioButton) view.findViewById(R.id.mTextFirst);
mTextSecond = (RadioButton) view.findViewById(R.id.mTextSecond);
mTextThird = (RadioButton) view.findViewById(R.id.mTextThird);
mTextFourth = (RadioButton) view.findViewById(R.id.mTextFourth);
pickerList = (ListView) view.findViewById(R.id.pickerList);
emptyView = (TextView) view.findViewById(R.id.empty_data_hints);
pickerList.setEmptyView(view.findViewById(R.id.picker_list_empty_data));
mTextFirst.setOnClickListener(this);
mTextSecond.setOnClickListener(this);
mTextThird.setOnClickListener(this);
pickerConfirm.setOnClickListener(this);
if (!TextUtils.isEmpty(pickerData.getPickerTitleName())){
pickerTitleName.setText(pickerData.getPickerTitleName());
}
}
protected void siteChanged(RadioGroup group, int checkedId) {
if (checkedId == -1) {
return;
}
RadioButton button = (RadioButton) group.findViewById(checkedId);
Site site = (Site) button.getTag();
if (site != null) {
App.setSiteUrl(site.getUrl());
}
if (site == null) {
group.clearCheck();
openSettingsActivity();
} else if (!site.getUrl().equals(mCurrentSiteUrl)) { // TODO 第一次启动 加载上次查看的url。
mDrawerPosition = ListView.INVALID_POSITION;
mCurrentSite = site;
mCurrentSiteUrl = site.getUrl();
PrefsUtils.setCurrentSiteUrl(mCurrentSiteUrl);
App.setLogin(false);
clearDatabase();
// 登陆完成后,再加载其他信息
loadUserInfo(site, false);
} else {
setupUserInfo(mUser);
}
getActionBar().setSubtitle(mCurrentSiteUrl);
}
private CameraConfig buildCameraConfig() {
CameraConfig cameraConfig = new CameraConfig();
View root = getView();
cameraConfig.mFrontFacing = ((RadioButton) root.findViewById(R.id.facing_front)).isChecked();
Spinner sizeLevelSpinner = (Spinner) root.findViewById(R.id.preview_size_level_spinner);
cameraConfig.mSizeLevel = PREVIEW_SIZE_LEVEL_PRESETS_MAPPING[sizeLevelSpinner.getSelectedItemPosition()];
Spinner sizeRatioSpinner = (Spinner) root.findViewById(R.id.preview_size_ratio_spinner);
cameraConfig.mSizeRatio = PREVIEW_SIZE_RATIO_PRESETS_MAPPING[sizeRatioSpinner.getSelectedItemPosition()];
Spinner focusModeSpinner = (Spinner) root.findViewById(R.id.focus_mode_spinner);
cameraConfig.mFocusMode = FOCUS_MODE_PRESETS_MAPPING[focusModeSpinner.getSelectedItemPosition()];
cameraConfig.mIsFaceBeautyEnabled = ((CheckBox) root.findViewById(R.id.face_beauty)).isChecked();
cameraConfig.mIsCustomFaceBeauty = ((CheckBox) root.findViewById(R.id.external_face_beauty)).isChecked();
cameraConfig.mContinuousAutoFocus = ((CheckBox) root.findViewById(R.id.continuous_auto_focus)).isChecked();
cameraConfig.mPreviewMirror = ((CheckBox) root.findViewById(R.id.preview_mirror)).isChecked();
cameraConfig.mEncodingMirror = ((CheckBox) root.findViewById(R.id.encoding_mirror)).isChecked();
return cameraConfig;
}
@OnClick({ R.id.rd_home, R.id.rd_features,R.id.rd_me }) public void onRadioButtonClicked(RadioButton radioButton) {
boolean checked = radioButton.isChecked();
switch (radioButton.getId()) {
case R.id.rd_home:
if (checked) {
fUtil.show(mFragments.get(0));break;
}
case R.id.rd_features:
if (checked) {
fUtil.show(mFragments.get(1));break;
}
case R.id.rd_social:
if (checked) {
fUtil.show(mFragments.get(2));break;
}
case R.id.rd_me:
if (checked) {
if(isAlpha){
fUtil.show(mFragments.get(3));break;
}else{
fUtil.show(mFragments.get(2));break;
}
}
}
}
private void initView() {
gravityBt = (Button) findViewById(R.id.gravity_bt);
alignLeftRb = (RadioButton) findViewById(R.id.align_left_rb);
alignRightRb = (RadioButton) findViewById(R.id.align_right_rb);
centerHoriRb = (RadioButton) findViewById(R.id.center_hori_rb);
toRightRb = (RadioButton) findViewById(R.id.to_right_rb);
toLeftRb = (RadioButton) findViewById(R.id.to_left_rb);
alignAboveRb = (RadioButton) findViewById(R.id.align_above_rb);
alignBottomRb = (RadioButton) findViewById(R.id.align_bottom_rb);
centerVertRb = (RadioButton) findViewById(R.id.center_vert_rb);
toBottomRb = (RadioButton) findViewById(R.id.to_bottom_rb);
toAboveRb = (RadioButton) findViewById(R.id.to_above_rb);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RadioButton radio = (RadioButton)findViewById(R.id.personalRadio);
radio.setText("My Avatars");
}
public void browse(View view) {
BrowseConfig config = new BrowseConfig();
config.typeFilter = "Public";
RadioButton radio = (RadioButton)findViewById(R.id.personalRadio);
if (radio.isChecked()) {
config.typeFilter = "Personal";
}
Spinner sortSpin = (Spinner)findViewById(R.id.sortSpin);
config.sort = (String)sortSpin.getSelectedItem();
AutoCompleteTextView tagText = (AutoCompleteTextView)findViewById(R.id.tagsText);
config.tag = (String)tagText.getText().toString();
AutoCompleteTextView categoryText = (AutoCompleteTextView)findViewById(R.id.categoriesText);
config.category = (String)categoryText.getText().toString();
EditText filterEdit = (EditText)findViewById(R.id.filterText);
config.filter = filterEdit.getText().toString();
CheckBox checkbox = (CheckBox)findViewById(R.id.imagesCheckBox);
MainActivity.showImages = checkbox.isChecked();
config.type = "Post";
if (MainActivity.instance != null) {
config.instance = MainActivity.instance.id;
}
HttpAction action = new HttpGetPostsAction(this, config);
action.execute();
}
public int findSelectedDuration() {
RadioGroup optionsGroup = (RadioGroup) this.getView().findViewById(R.id.tremolo_picking_dlg_duration_group);
int radioButtonId = optionsGroup.getCheckedRadioButtonId();
if( radioButtonId != -1 ) {
RadioButton radioButton = (RadioButton) optionsGroup.findViewById(radioButtonId);
if( radioButton != null ) {
return ((Integer)radioButton.getTag()).intValue();
}
}
return TGDuration.EIGHTH;
}
private void initView() {
gravityBt=(Button) findViewById(R.id.gravity_bt);
alignLeftRb=(RadioButton) findViewById(R.id.align_left_rb);
alignRightRb=(RadioButton) findViewById(R.id.align_right_rb);
centerHoriRb=(RadioButton) findViewById(R.id.center_hori_rb);
toRightRb=(RadioButton) findViewById(R.id.to_right_rb);
toLeftRb=(RadioButton) findViewById(R.id.to_left_rb);
alignAboveRb=(RadioButton) findViewById(R.id.align_above_rb);
alignBottomRb=(RadioButton) findViewById(R.id.align_bottom_rb);
centerVertRb=(RadioButton) findViewById(R.id.center_vert_rb);
toBottomRb=(RadioButton) findViewById(R.id.to_bottom_rb);
toAboveRb=(RadioButton) findViewById(R.id.to_above_rb);
}
/**
* 初始化组件
*/
private void initView() {
mHomePage = (RadioButton) findViewById(R.id.rb_homePage);
mOrder = (RadioButton)findViewById(R.id.rb_order);
mFound = (RadioButton) findViewById(R.id.rb_found);
mMy = (RadioButton) findViewById(R.id.rb_my);
}
/**
* Creates a DistilledPagePrefsView.
*
* @param context Context for acquiring resources.
* @param attrs Attributes from the XML layout inflation.
*/
public DistilledPagePrefsView(Context context, AttributeSet attrs) {
super(context, attrs);
mDistilledPagePrefs = DomDistillerServiceFactory.getForProfile(
Profile.getLastUsedProfile()).getDistilledPagePrefs();
mColorModeButtons = new EnumMap<Theme, RadioButton>(Theme.class);
mPercentageFormatter = NumberFormat.getPercentInstance(Locale.getDefault());
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId > 0) {
RadioButton button = group.findViewById(checkedId);
buttonText.add(button.getText().toString());
buttonState.add(button.isChecked());
} else {
buttonText.add(null);
}
++count;
}
@Override
public void onClick(View v)
{
boolean isChanged = false;
if (v instanceof RadioButton)
{
for (int i = 0; i < rButtons.length; i++)
{
rButtons[i].setChecked(rButtons[i] == v);
}
return;
}
else if (v.getId() == R.id.dialog_button_ok && changeIf != null)
{
TextStyle textStyle = TextStyle.TEXT_BODY;
for (int i = 0; i < rButtons.length; i++)
{
if (rButtons[i].isChecked())
{
textStyle = TextStyle.values()[i];
break;
}
}
if (parameters.textStyle != textStyle)
{
isChanged = true;
parameters.textStyle = textStyle;
}
if (parameters.numbering != cbNumbering.isChecked())
{
isChanged = true;
parameters.numbering = cbNumbering.isChecked();
}
}
changeIf.onTextPropertiesChange(isChanged);
closeDialog();
}
/**
* Initiatializes a Button and selects it if it corresponds to the current
* theme.
*/
private RadioButton initializeAndGetButton(int id, final Theme theme) {
final RadioButton button = (RadioButton) findViewById(id);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDistilledPagePrefs.setTheme(theme);
}
});
return button;
}
/**
* Constructor for inflating via XML.
*/
public RadioButtonWithDescription(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.radio_button_with_description, this, true);
mRadioButton = (RadioButton) findViewById(R.id.radio_button);
mTitle = (TextView) findViewById(R.id.title);
mDescription = (TextView) findViewById(R.id.description);
if (attrs != null) applyAttributes(attrs);
// We want RadioButtonWithDescription to handle the clicks itself.
mRadioButton.setClickable(false);
setOnClickListener(this);
}
@Override
protected void onBindView(final View view) {
super.onBindView(view);
mRadioButton = (RadioButton)view.findViewById(R.id.radio_button);
mRadioButton.setChecked(mIsSelected);
mRadioButton.setOnClickListener(mClickListener);
view.setOnClickListener(mClickListener);
}
private void isSelected(RadioButton btn) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i) != btn) {
list.get(i).setChecked(false);
}
}
}
@NonNull
@Override
public View getView(int position, View view, @NonNull ViewGroup parent) {
Context context = parent.getContext();
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resId, parent, false);
}
CheckBox box = view.findViewById(android.R.id.checkbox);
RadioButton button = view.findViewById(android.R.id.button1);
TextView text1 = view.findViewById(android.R.id.text1);
TextView text2 = view.findViewById(android.R.id.text2);
CharSequence filename = objects.get(position);
text1.setText(filename);
if (multiMode) {
box.setChecked(checkedItems[position]);
} else {
button.setChecked(checked == position);
}
Date lastModified = SaveLogHelper.getLastModifiedDate(filename.toString());
DateFormat dateFormat = DateFormat.getDateTimeInstance();
text2.setText(dateFormat.format(lastModified));
return view;
}
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Toolbar toolbar = view.findViewById(R.id.toolbar);
((AppCompatActivity) requireActivity()).setSupportActionBar(toolbar);
List<String> androidVersions = new ArrayList<>(Arrays.asList(getResources().getStringArray(
R.array.android_versions)));
final int correctAnswerIndex = androidVersions.indexOf(getString(R.string.lollipop));
final RadioGroup radioGroup = view.findViewById(R.id.radio_group);
mFeedbackTextView = view.findViewById(R.id.feedback_text_view);
if (radioGroup != null && correctAnswerIndex != -1) {
for (int i = 0; i < androidVersions.size(); i++) {
RadioButton radioButton = new RadioButton(getContext());
radioButton.setText(androidVersions.get(i));
radioButton.setId(i);
radioButton.setPadding(36, 36, 36, 36 );
radioButton.setTextSize(18f);
radioGroup.addView(radioButton);
}
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int indexOfCheckedChild = radioGroup.indexOfChild(view.findViewById(checkedId));
if (indexOfCheckedChild == correctAnswerIndex) {
mFeedbackTextView.setText(R.string.correct);
mFeedbackTextView.setBackgroundColor(ContextCompat.getColor(requireContext(),
R.color.green));
} else {
mFeedbackTextView.setText(R.string.incorrect);
mFeedbackTextView.setBackgroundColor(ContextCompat.getColor(requireContext(),
R.color.red));
}
}
});
}
}