下面列出了怎么用android.support.v7.widget.AppCompatRadioButton的API类实例代码及写法,或者点击链接到github查看源代码。
public void setRadioButtons(boolean expose)
{
boolean requiresFullRedraw = false;
//uncheck all ranges, note that the selected range will be checked after the refresh
for (int i = 0; i < items.size(); i++)
{
SortedItem si = items.get(i);
if (si.isRadioExposed() != expose) requiresFullRedraw = true;
if (si.view != null)
{
AppCompatRadioButton button = si.view.itemView.findViewById(R.id.radioBox);
if (button != null && (button.isChecked() || si.isItemChecked())) button.setChecked(false);
}
si.setIsChecked(false);
si.setExposeRadio(expose);
}
if (requiresFullRedraw)
{
notifyDataSetChanged();
}
}
private void init() {
mRgChoice.setVisibility(View.VISIBLE);
mChoiceBox.setVisibility(View.GONE);
mScdTitle.setText(title);
for (int i = 0; i < datas.length; i++) {
AppCompatRadioButton arb = new AppCompatRadioButton(mContext);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(-1, ScreenUtil.getInstance().dip2px(48));
arb.setLayoutParams(layoutParams);
arb.setGravity(Gravity.CENTER_VERTICAL);
arb.setId(i);
arb.setText(datas[i]);
arb.setTextSize(15);
arb.setTextColor(mContext.getResources().getColor(R.color.new_text_color_first));
arb.setPadding(ScreenUtil.getInstance().dip2px(16), 0, 0, 0);
if (i == 0)
arb.setChecked(true);
mRgChoice.addView(arb);
}
}
private void setQuestionsGroupView(){
LinearLayout.LayoutParams rglp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams rblp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mQuestionsGroup.setLayoutParams(rglp);
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{mRadioBtnColor},
new int[]{mRadioBtnColor}
},
new int[]{mRadioBtnColor, mRadioBtnColor}
);
for (int i = 0; i < mQuestionsArray.length; i++) {
AppCompatRadioButton rb = new AppCompatRadioButton(mContext);
setQuestionItem(i, rb, rblp, colorStateList);
mQuestionsGroup.addView(rb);
}
}
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
// Allow super to try and create a view first
final View result = super.onCreateView(name, context, attrs);
if (result != null) {
return result;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
switch (name) {
case "EditText":
return new AppCompatEditText(this,attrs);
case "Spinner":
return new AppCompatSpinner(this,attrs);
case "CheckBox":
return new AppCompatCheckBox(this,attrs);
case "RadioButton":
return new AppCompatRadioButton(this,attrs);
case "CheckedTextView":
return new AppCompatCheckedTextView(this,attrs);
}
}
return null;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_led);
mContentLed = (TextInputEditText) findViewById(R.id.content_led);
Button mFontcolorBtnLed = (Button) findViewById(R.id.fontcolor_btn_led);
Button mBgcolorBtnLed = (Button) findViewById(R.id.bgcolor_btn_led);
mPreviewLed = (TextView) findViewById(R.id.preview_led);
ImageView mReverseColorLed = (ImageView) findViewById(R.id.reverseColor_led);
mShowstyleRadiogroupLed = (RadioGroup) findViewById(R.id.showstyle_radiogroup_led);
AppCompatRadioButton mSingleRadiobtnLed = (AppCompatRadioButton) findViewById(R.id.single_radiobtn_led);
AppCompatRadioButton mSingleTossBtnLed = (AppCompatRadioButton) findViewById(R.id.single_toss_radiobtn_led);
mRollspeedSeekbarLed = (AppCompatSeekBar) findViewById(R.id.rollspeed_seekbar_led);
mAdaptiveRadiobtnLed = (AppCompatRadioButton) findViewById(R.id.adaptive_radiobtn_led);
mLinesTextView = (TextView) findViewById(R.id.tv_lines_led);
mlinesSeekbar = (AppCompatSeekBar) findViewById(R.id.lines_seekbar_led);
AppCompatRadioButton mMagicRadiobtnLed = (AppCompatRadioButton) findViewById(R.id.magic_radiobtn_led);
mCompatSpinner = (AppCompatSpinner) findViewById(R.id.spinner_magicstyle_led);
Button mStartBtnLed = (AppCompatButton) findViewById(R.id.start_btn_led);
if (mFontcolorBtnLed != null) {
mFontcolorBtnLed.setOnClickListener(this);
}
if (mStartBtnLed != null) {
mStartBtnLed.setOnClickListener(this);
}
if (mBgcolorBtnLed != null) {
mBgcolorBtnLed.setOnClickListener(this);
}
initViewEvent();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.account_sub_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.account_sub_item_text);
tv.setText(showSubItems.get(position).getName());
AppCompatRadioButton acrb = (AppCompatRadioButton) convertView.findViewById(R.id.aitt_button);
convertView.findViewById(R.id.account_sub_item).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedSubItemId = showSubItems.get(position).getId();
Log.i(TAG, "selectedSubItemId:>>>>>>" + selectedSubItemId);
Log.i(TAG, "seletectedItemPosition:" + seletectedItemPosition);
subAdapter.notifyDataSetChanged();
String item = items.get(seletectedItemPosition).getItem() + (getSubItemById(selectedSubItemId) != null ? "," + getSubItemById(selectedSubItemId).getName() : "");
if (mItemListener != null) {
mItemListener.onItemSelected(item);
}
cancel();
}
});
// ((TextView) ((LinearLayout) convertView).getChildAt(0)).setText(showSubItems.get(position).getName());
Log.i(TAG, "selectedSubItemId:" + selectedSubItemId);
Log.i(TAG, "showSubItems.get(position).getId():" + showSubItems.get(position).getId());
if (showSubItems.get(position).getId() == selectedSubItemId) {
acrb.setVisibility(View.VISIBLE);
} else {
acrb.setVisibility(View.GONE);
}
return convertView;
}
public void onResume() {
// reset to default
AppCompatRadioButton activeRadioButton = this.activeRadioButton;
clearCheckedChildren();
this.activeRadioButton = activeRadioButton;
this.activeRadioButton.setChecked(true);
}
private void setChildrenOnClickListener(AppCompatRadioButton child) {
GridLayout parent = (GridLayout) child.getParent();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View v = parent.getChildAt(i);
if (v instanceof AppCompatRadioButton) {
if (((RadioButton) v).isChecked()) {
activeRadioButton = (AppCompatRadioButton) v;
}
v.setOnClickListener(this);
}
}
}
private void clearCheckedChildren() {
for (int i = 0; i < getChildCount(); i++) {
if (getChildAt(i) instanceof AppCompatRadioButton) {
((AppCompatRadioButton) getChildAt(i)).setChecked(false);
}
}
}
public View getView(final int position, View convertView, ViewGroup parent) {
Answer item = (Answer) getItem(position);
if (convertView == null) {
radioListItemView = (RadioListItemView)View.inflate(context, R.layout.radio_list_item, null);
} else{
radioListItemView = (RadioListItemView)convertView;
}
final AppCompatRadioButton radio = (AppCompatRadioButton)radioListItemView.findViewById(R.id.radio);
radio.setChecked(item.isChecked());
final TextView textView = (TextView)radioListItemView.findViewById(R.id.tv1);
final RadioListItemView finalRadioListItemView = radioListItemView;
textView.setText(item.getAnswer());
radioListItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setAnswer(position, finalRadioListItemView);
}
});
radio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setAnswer(position, finalRadioListItemView);
}
});
return radioListItemView;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
radioButton = (AppCompatRadioButton) findViewById(R.id.radio);
textView = (AppCompatTextView)findViewById(R.id.tv1);
}
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
// Allow super to try and create a view first
final View result = super.onCreateView(name, context, attrs);
if (result != null) {
return result;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// If we're running pre-L, we need to 'inject' our tint aware Views in place of the
// standard framework versions
switch (name) {
case "EditText":
return new AppCompatEditText(this, attrs);
case "Spinner":
return new AppCompatSpinner(this, attrs);
case "CheckBox":
return new AppCompatCheckBox(this, attrs);
case "RadioButton":
return new AppCompatRadioButton(this, attrs);
case "CheckedTextView":
return new AppCompatCheckedTextView(this, attrs);
}
}
return null;
}
private void setQuestionItem(int pos, AppCompatRadioButton rb, ViewGroup.LayoutParams lp,
ColorStateList csl){
rb.setLayoutParams(lp);
rb.setTag(pos);
rb.setTextColor(mTextColor);
rb.setTextSize(mTextSize);
rb.setText(mQuestionsArray[pos]);
rb.setOnClickListener(onQuestionItemListener);
rb.setSupportButtonTintList(csl);
mQuestionRBtnsList.add(rb);
}
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
// Allow super to try and create a view first
final View result = super.onCreateView(name, context, attrs);
if (result != null) {
return result;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// If we're running pre-L, we need to 'inject' our tint aware Views in place of the
// standard framework versions
switch (name) {
case "EditText":
return new AppCompatEditText(this, attrs);
case "Spinner":
return new AppCompatSpinner(this, attrs);
case "CheckBox":
return new AppCompatCheckBox(this, attrs);
case "RadioButton":
return new AppCompatRadioButton(this, attrs);
case "CheckedTextView":
return new AppCompatCheckedTextView(this, attrs);
}
}
return null;
}
private void findView() {
this.switchDonuts = (SwitchCompat) findViewById(R.id.switch_donuts);
this.switchText = (SwitchCompat) findViewById(R.id.switch_text);
this.switchTouchAnimation = (SwitchCompat) findViewById(R.id.switch_touch_animation);
this.switchCanTouch = (SwitchCompat) findViewById(R.id.switch_can_touch);
this.switchWithLegends = (SwitchCompat) findViewById(R.id.switch_with_legends);
this.inputDuration = (TextInputLayout) findViewById(R.id.input_duration);
this.inputStartAngle = (TextInputLayout) findViewById(R.id.input_start_angle);
this.inputTouchScaleSize = (TextInputLayout) findViewById(R.id.input_touch_scale_size);
this.inputTouchScaleUpDuration = (TextInputLayout) findViewById(R.id.input_touch_scale_up_duration);
this.inputTouchScaleDownDuration = (TextInputLayout) findViewById(R.id.input_touch_scale_down_duration);
this.inputTouchShadowRadius = (TextInputLayout) findViewById(R.id.input_touch_shadow_radius);
this.inputTouchExpandAngle = (TextInputLayout) findViewById(R.id.input_touch_expand_angle);
this.inputPieRadiusScale = (TextInputLayout) findViewById(R.id.input_pie_radius_scale);
this.inputTextMarginLine = (TextInputLayout) findViewById(R.id.input_text_margin_line);
this.inputTextSize = (TextInputLayout) findViewById(R.id.input_text_size);
this.inputTextPointRadius = (TextInputLayout) findViewById(R.id.input_text_point_radius);
this.inputTextLineStrokeWidth = (TextInputLayout) findViewById(R.id.input_text_line_stroke_width);
this.inputTextLineStartMargin = (TextInputLayout) findViewById(R.id.input_text_line_start_margin);
this.inputSplitAngle = (TextInputLayout) findViewById(R.id.input_split_angle);
this.inputFocusAlphaType = (TextInputLayout) findViewById(R.id.input_focus_alpha_type);
this.radioFocusWithAlpha = (AppCompatRadioButton) findViewById(R.id.radio_focus_with_alpha);
this.radioFocusWithAlphaRev = (AppCompatRadioButton) findViewById(R.id.radio_focus_with_alpha_rev);
this.radioFocusWithoutAlpha = (AppCompatRadioButton) findViewById(R.id.radio_focus_without_alpha);
this.textGravityAbove = (AppCompatRadioButton) findViewById(R.id.text_gravity_above);
this.textGravityBelow = (AppCompatRadioButton) findViewById(R.id.text_gravity_below);
this.textGravityAlign = (AppCompatRadioButton) findViewById(R.id.text_gravity_align);
this.textGravityDystopy = (AppCompatRadioButton) findViewById(R.id.text_gravity_dystopy);
this.popupAnima = (RelativeLayout) findViewById(R.id.popup_anima);
this.btnOk = (Button) findViewById(R.id.btn_ok);
switchDonuts.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switchDonuts.setText(isChecked ? "饼图(pie-chat)" : "甜甜圈(ring-chat)");
}
});
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewConfig != null) {
dismiss(viewConfig);
if (mOnClickListener != null) {
mOnClickListener.onClick(v);
}
}
}
});
}
public RingHolder(View itemView) {
super(itemView);
tvRingName = (TextView) itemView.findViewById(R.id.list_speaker_text);
btnFavor = (AppCompatRadioButton) itemView.findViewById(R.id.list_favorite_bt);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.account_sub_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.account_sub_item_text);
tv.setTextColor(mContext.getResources().getColor(R.color.new_text_color_first));
tv.setText(items.get(position).getItem());
AppCompatRadioButton acrb = (AppCompatRadioButton) convertView.findViewById(R.id.aitt_button);
convertView.findViewById(R.id.account_sub_item).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
seletectedItemPosition = position;
itemAdapter.notifyDataSetChanged();
if (seletectedItemPosition > -1) {
if (mItemListener != null) {
mItemListener.onIncomeSelected(items.get(seletectedItemPosition).getItem());
}
}
cancel();
}
});
// ((TextView) ((LinearLayout) convertView).getChildAt(0)).setText(showSubItems.get(position).getName());
if (position == seletectedItemPosition) {
acrb.setVisibility(View.VISIBLE);
} else {
acrb.setVisibility(View.GONE);
}
return convertView;
// if (convertView == null) {
// convertView = new LinearLayout(mContext);
//
// AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, dp2px(64));
// convertView.setLayoutParams(lp);
// ((LinearLayout) convertView).setOrientation(LinearLayout.HORIZONTAL);
// TextView tv = new TextView(mContext);
// tv.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
// tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
// tv.setTextColor(mContext.getResources().getColorStateList(R.color.new_text_color_first));
// LinearLayout.LayoutParams llpt = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
// llpt.weight = 1.0f;
//
// LinearLayout.LayoutParams llpi = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// ImageView iv = new ImageView(mContext);
// iv.setBackgroundResource(R.drawable.ic_green_checkbox);
// iv.setVisibility(View.GONE);
// llpi.gravity = Gravity.CENTER_VERTICAL;
//
// ((LinearLayout) convertView).addView(tv, 0, llpt);
// ((LinearLayout) convertView).addView(iv, 1, llpi);
// }
// ((TextView) ((LinearLayout) convertView).getChildAt(0)).setText(items.get(position).getItem());
// if (position == seletectedItemPosition) {
// ((LinearLayout) convertView).getChildAt(1).setVisibility(View.VISIBLE);
// } else {
// ((LinearLayout) convertView).getChildAt(1).setVisibility(View.GONE);
// }
// return convertView;
}
@Override
public void onClick(View view) {
final AppCompatRadioButton rb = (AppCompatRadioButton) view;
if ( activeRadioButton != null ) {
activeRadioButton.setChecked(false);
}
rb.setChecked(true);
activeRadioButton = rb;
switch (getCheckedRadioButtonId())
{
case R.id.top_left:
position = RadialSort.Position.TOP_LEFT;
break;
case R.id.top_middle:
position = RadialSort.Position.TOP_MIDDLE;
break;
case R.id.top_right:
position = RadialSort.Position.TOP_RIGHT;
break;
case R.id.right:
position = RadialSort.Position.RIGHT;
break;
case R.id.middle:
position = RadialSort.Position.MIDDLE;
break;
case R.id.left:
position = RadialSort.Position.LEFT;
break;
case R.id.bottom_left:
position = RadialSort.Position.BOTTOM_LEFT;
break;
case R.id.bottom_middle:
position = RadialSort.Position.BOTTOM_MIDDLE;
break;
case R.id.bottom_right:
position = RadialSort.Position.BOTTOM_RIGHT;
break;
}
listener.onRadioGroupChildChanged();
}
@Override
public void addView(View child, ViewGroup.LayoutParams params) {
super.addView(child, params);
setChildrenOnClickListener((AppCompatRadioButton) child);
}
private void showThemeDialog() {
ScrollView scrollView = new ScrollView(getActivity());
RadioGroup radioGroup = new RadioGroup(getActivity());
scrollView.addView(radioGroup);
int margin = ScreenUtils.dp2px(16);
radioGroup.setPadding(margin / 2, margin, margin, margin);
for (int i = 0; i < themeColorArray.length; i++) {
AppCompatRadioButton arb = new AppCompatRadioButton(getActivity());
RadioGroup.LayoutParams params =
new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
arb.setLayoutParams(params);
arb.setId(i);
arb.setTextColor(getResources().getColor(themeColorArray[i]));
arb.setText(themeNameArray[i]);
arb.setTextSize(16);
arb.setPadding(0, margin / 2, 0, margin / 2);
radioGroup.addView(arb);
}
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
theme = checkedId;
}
});
DialogHelper dialogHelper = new DialogHelper();
dialogHelper.showCustomDialog(getActivity(), scrollView,
getString(R.string.theme_preference), new DialogListener() {
@Override
public void onPositive(DialogInterface dialog, int which) {
super.onPositive(dialog, which);
dialog.dismiss();
String key = getString(R.string.app_preference_theme);
int oldTheme = Preferences.getInt(key, 0);
if (theme != oldTheme) {
Preferences.putInt(key, theme);
ActivityUtil.finishAll();
startActivity(new Intent(app.mContext, CourseActivity.class));
}
}
});
}
public void initializeStep() {
setOrientation(VERTICAL);
ConsentQuizModel.QuizQuestion question = step.getQuestion();
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.rss_layout_quiz_question, this, true);
((TextView) findViewById(R.id.title)).setText(step.getTitle());
radioGroup = (RadioGroup) findViewById(R.id.radio_group);
submitBar = (SubmitBar) findViewById(R.id.submit_bar);
submitBar.getNegativeActionView().setVisibility(GONE);
resultTitle = (TextView) findViewById(R.id.quiz_result_title);
resultSummary = (TextView) findViewById(R.id.quiz_result_summary);
radioItemBackground = findViewById(R.id.quiz_result_item_background);
if (question.getType().equals("instruction")) {
TextView instructionText = (TextView) findViewById(R.id.instruction_text);
instructionText.setText(question.getText());
instructionText.setVisibility(VISIBLE);
// instruction steps don't need submit, also always count as correct answer
submitBar.setPositiveTitle(R.string.rsb_next);
submitBar.setPositiveAction(v -> onNext(true));
} else {
submitBar.setPositiveTitle(R.string.rsb_submit);
submitBar.setPositiveAction(v -> onSubmit());
for (Choice<String> choice : getChoices(question)) {
AppCompatRadioButton button = (AppCompatRadioButton) inflater.inflate(R.layout.rss_item_radio_quiz,
radioGroup,
false);
button.setText(choice.getText());
button.setTag(choice);
radioGroup.addView(button);
if (question.getExpectedAnswer().equals(choice.getValue())) {
expectedChoice = choice;
}
}
}
}
public static BaseDSL.ViewClassResult appCompatRadioButton() {
return BaseDSL.v(AppCompatRadioButton.class);
}
public static Void appCompatRadioButton(Anvil.Renderable r) {
return BaseDSL.v(AppCompatRadioButton.class, r);
}
@Override
public void init(JSONObject element,
List<Field> fields,
Bundle savedState,
Cursor featureCursor,
SharedPreferences preferences,
Map<String, Map<String, String>> translations) throws JSONException{
JSONObject attributes = element.getJSONObject(JSON_ATTRIBUTES_KEY);
mFieldName = attributes.getString(JSON_FIELD_NAME_KEY);
mIsShowLast = ControlHelper.isSaveLastValue(attributes);
boolean isEnabled = ControlHelper.isEnabled(fields, mFieldName);
setEnabled(isEnabled);
String lastValue = null;
if (ControlHelper.hasKey(savedState, mFieldName))
lastValue = savedState.getString(ControlHelper.getSavedStateKey(mFieldName));
else if (null != featureCursor) { // feature exists
int column = featureCursor.getColumnIndex(mFieldName);
if (column >= 0)
lastValue = featureCursor.getString(column);
} else if (mIsShowLast)
lastValue = preferences.getString(mFieldName, null);
JSONArray values = attributes.getJSONArray(JSON_VALUES_KEY);
int position = Constants.NOT_FOUND;
mAliasValueMap = new HashMap<>();
for (int j = 0; j < values.length(); j++) {
JSONObject keyValue = values.getJSONObject(j);
String value = keyValue.getString(JSON_VALUE_NAME_KEY);
String value_alias = keyValue.getString(JSON_VALUE_ALIAS_KEY);
if (lastValue == null && keyValue.has(JSON_DEFAULT_KEY) && keyValue.getBoolean(JSON_DEFAULT_KEY)) {
position = j;
}
if (lastValue != null && lastValue.equals(value)) { // if modify data
position = j;
}
mAliasValueMap.put(value_alias, value);
AppCompatRadioButton radioButton = new AppCompatRadioButton(getContext());
radioButton.setText(value_alias);
radioButton.setEnabled(isEnabled);
addView(radioButton);
}
if (getChildAt(position) != null)
check(getChildAt(position).getId());
setOrientation(RadioGroup.VERTICAL);
}