下面列出了android.widget.RadioButton#getId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Checks the radio button with the specified id.
* If the specified id is not found does nothing.
*
* @param id the radio button's id
*/
@Override
public void check(int id) {
if (id <= 0)
return;
for (RadioButton radioButton : mRadioButtons) {
if (radioButton.getId() == id) {
if (checkButton(radioButton)) { // True if the button wasn't already checked.
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(
MultiLineRadioGroup.this, radioButton);
}
}
return;
}
}
}
@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;
}
}
}
}
@OnCheckedChanged({ R.id.source_align_start, R.id.source_align_center, R.id.source_align_end })
void onSourceAlignmentChanged(RadioButton button, boolean isChecked) {
if (isChecked) {
int alignment = Gravity.NO_GRAVITY;
switch (button.getId()) {
case R.id.source_align_start:
alignment = Gravity.START;
break;
case R.id.source_align_center:
alignment = Gravity.CENTER_HORIZONTAL;
break;
case R.id.source_align_end:
alignment = Gravity.END;
break;
}
onUpdateListener.updateSourceTextAlignment(alignment);
}
}
@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);
}
}
/**
* Checks the radio button with the specified id.
* If the specified id is not found does nothing.
*
* @param id the radio button's id
*/
@Override
public void check(int id) {
if (id <= 0)
return;
for (RadioButton radioButton : mRadioButtons) {
if (radioButton.getId() == id) {
if (checkButton(radioButton)) { // True if the button wasn't already checked.
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(
MultiLineRadioGroup.this, radioButton);
}
}
return;
}
}
}
@Override
public void onCheckedChanged(final RadioGroup group, final int checkedId) {
RadioButton radioButton = findViewById(checkedId);
// Change key mode.
int i = radioButton.getId();
if (i == R.id.radioButton1) {
mKeyMode = KeyMode.STD_KEY;
} else if (i == R.id.radioButton2) {
mKeyMode = KeyMode.MEDIA_CTRL;
} else if (i == R.id.radioButton3) {
mKeyMode = KeyMode.DPAD_BUTTON;
} else if (i == R.id.radioButton4) {
mKeyMode = KeyMode.USER;
}
}
private void checkButton(ArrayList<RadioButton> buttons, ArrayList<EditText> options, int id, Context context) {
for (RadioButton button : buttons) {
if (button.getId() == id) {
int index = buttons.indexOf(button);
if ("".equals(options.get(index).getText().toString().trim())) {
options.get(index).setError(context.getString(R.string.valid_before_setting_answer));
button.setChecked(false);
return;
} else {
button.setChecked(true);
}
} else {
button.setChecked(false);
}
}
}
private void checkButton(ArrayList<RadioButton> buttons, ArrayList<EditText> options, int id, Context context) {
for (RadioButton button : buttons) {
if (button.getId() == id) {
int index = buttons.indexOf(button);
if (options.get(index).getText().toString().equals("")) {
Toast.makeText(context, "Enter a valid option before marking it as answer", Toast.LENGTH_LONG).show();
button.setChecked(false);
return;
} else {
button.setChecked(true);
}
} else {
button.setChecked(false);
}
}
}
private void checkButton(ArrayList<RadioButton> buttons, ArrayList<EditText> options, int id, Context context) {
for (RadioButton button : buttons) {
if (button.getId() == id) {
int index = buttons.indexOf(button);
if ("".equals(options.get(index).getText().toString().trim())) {
options.get(index).setError(context.getString(R.string.valid_before_answer));
options.get(index).setText(null);
button.setChecked(false);
return;
} else {
button.setChecked(true);
}
} else {
button.setChecked(false);
}
}
}
private void checkButton(ArrayList<RadioButton> buttons, ArrayList<EditText> options, int id, Context context) {
for (RadioButton button : buttons) {
if (button.getId() == id) {
int index = buttons.indexOf(button);
if (options.get(index).getText().toString().equals("")) {
Toast.makeText(context, "Enter a valid option before marking it as answer", Toast.LENGTH_LONG).show();
button.setChecked(false);
return;
} else {
button.setChecked(true);
}
} else {
button.setChecked(false);
}
}
}
public RadioButtonGroupHelper(RadioButton... groups) {
mViewSet = new ArrayList<>(groups.length - 1);
for (RadioButton view : groups) {
// 如果这个RadioButton没有设置id的话
if (view.getId() == View.NO_ID) {
throw new IllegalArgumentException("are you ok?");
}
view.setOnCheckedChangeListener(this);
mViewSet.add(view);
}
}
/**
* onClick handler for radio buttons.
*/
public void onRadioButtonClicked(View view) {
RadioButton rb = (RadioButton) view;
if (!rb.isChecked()) {
Log.d(TAG, "Got click on non-checked radio button");
return;
}
switch (rb.getId()) {
case R.id.recDrawTwice_radio:
mSelectedRecordMethod = RECMETHOD_DRAW_TWICE;
break;
case R.id.recFbo_radio:
mSelectedRecordMethod = RECMETHOD_FBO;
break;
case R.id.recFramebuffer_radio:
mSelectedRecordMethod = RECMETHOD_BLIT_FRAMEBUFFER;
break;
default:
throw new RuntimeException("Click from unknown id " + rb.getId());
}
Log.d(TAG, "Selected rec mode " + mSelectedRecordMethod);
RenderHandler rh = mRenderThread.getHandler();
if (rh != null) {
rh.setRecordMethod(mSelectedRecordMethod);
}
}
private int getCheckedId() {
for (RadioButton button : this.buttons) {
if (button.isChecked()) {
return button.getId();
}
}
return -1;
}
private int getCheckedId() {
for (RadioButton button : this.buttons) {
if (button.isChecked()) {
return button.getId();
}
}
return -1;
}
private int getCheckedId() {
for (RadioButton button : this.buttons) {
if (button.isChecked()) {
return button.getId();
}
}
return -1;
}
@Override
public void createViews(Context context, ViewGroup layout) {
RadioGroup group = new RadioGroup(context);
TextView tv = new TextView(context);
PixateFreestyle.setStyleId(tv, "rgTitle");
tv.setText("Will you be attending?");
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
group.addView(tv);
String[] titles = new String[] { "Yes", "No", "Maybe" };
Integer id = null;
for (String title : titles) {
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
RadioButton rb = new RadioButton(context);
PixateFreestyle.setStyleClass(rb, "myRadioButton");
PixateFreestyle.setStyleId(rb, "myRadioButton" + title);
rb.setText(title);
group.addView(rb, params);
if (id == null) {
id = rb.getId();
}
}
layout.addView(group,
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
group.check(id);
addView(group);
}
/**
* onClick handler for radio buttons.
*/
public void onRadioButtonClicked(View view) {
int newSize;
RadioButton rb = (RadioButton) view;
if (!rb.isChecked()) {
Log.d(TAG, "Got click on non-checked radio button");
return;
}
switch (rb.getId()) {
case R.id.surfaceSizeTiny_radio:
newSize = SURFACE_SIZE_TINY;
break;
case R.id.surfaceSizeSmall_radio:
newSize = SURFACE_SIZE_SMALL;
break;
case R.id.surfaceSizeMedium_radio:
newSize = SURFACE_SIZE_MEDIUM;
break;
case R.id.surfaceSizeFull_radio:
newSize = SURFACE_SIZE_FULL;
break;
default:
throw new RuntimeException("Click from unknown id " + rb.getId());
}
mSelectedSize = newSize;
int[] wh = mWindowWidthHeight[newSize];
// Update the Surface size. This causes a "surface changed" event, but does not
// destroy and re-create the Surface.
SurfaceView sv = (SurfaceView) findViewById(R.id.hardwareScaler_surfaceView);
SurfaceHolder sh = sv.getHolder();
Log.d(TAG, "setting size to " + wh[0] + "x" + wh[1]);
sh.setFixedSize(wh[0], wh[1]);
}
/**
* onClick handler for radio buttons.
*/
public void onRadioButtonClicked(View view) {
RadioButton rb = (RadioButton) view;
if (!rb.isChecked()) {
Log.d(TAG, "Got click on non-checked radio button");
return;
}
switch (rb.getId()) {
case R.id.recDrawTwice_radio:
mSelectedRecordMethod = RECMETHOD_DRAW_TWICE;
break;
case R.id.recFbo_radio:
mSelectedRecordMethod = RECMETHOD_FBO;
break;
case R.id.recFramebuffer_radio:
mSelectedRecordMethod = RECMETHOD_BLIT_FRAMEBUFFER;
break;
default:
throw new RuntimeException("Click from unknown id " + rb.getId());
}
Log.d(TAG, "Selected rec mode " + mSelectedRecordMethod);
RenderHandler rh = mRenderThread.getHandler();
if (rh != null) {
rh.setRecordMethod(mSelectedRecordMethod);
}
}