라디오 그룹에서 라디오 버튼을 기본값으로 설정하는 방법은 무엇입니까?
내가 만든 RadioGroup
및 RadioButton
동적으로 다음과 같은 :
RadioGroup radioGroup = new RadioGroup(context);
RadioButton radioBtn1 = new RadioButton(context);
RadioButton radioBtn2 = new RadioButton(context);
RadioButton radioBtn3 = new RadioButton(context);
radioBtn1.setText("Less");
radioBtn2.setText("Normal");
radioBtn3.setText("More");
radioBtn2.setChecked(true);
radioGroup.addView(radioBtn1);
radioGroup.addView(radioBtn2);
radioGroup.addView(radioBtn3);
여기서 단계 radioBtn2.setChecked(true);
는 radioBtn2
항상 확인됩니다. 즉 , radioBtn2
다른 두 개의 라디오 버튼 ( radioBtn1
, radioBtn3
) 을 확인하여 선택을 취소 할 수 없습니다 . RadioGroup
한 번에 하나의 라디오 버튼 만 확인할 수 있도록하고 싶습니다 (이제 한 번에 두 개의 라디오 버튼을 확인할 수 있음).
이 문제를 어떻게 해결할 수 있습니까?
다음과 같이 라디오 그룹의 라디오 버튼을 확인해야합니다.
radiogroup.check(IdOfYourButton)
물론 먼저 라디오 버튼에 ID를 설정해야합니다
편집 : 내가 잊고, radioButton.getId()
작동뿐만 아니라, 들으 라 메쉬
EDIT2 :
android:checkedButton="@+id/my_radiobtn"
radiogroup xml에서 작동
XML 속성 경우 그 android:checkedButton
소요 id
의이 RadioButton
체크된다.
<RadioGroup
...
...
android:checkedButton="@+id/IdOfTheRadioButtonInsideThatTobeChecked"
... >....</RadioGroup>
XML 파일 android:checkedButton
에서 RadioGroup
의 ID를 기본값으로 사용하여 필드를 설정하십시오 RadioButton
.
<RadioGroup
....
android:checkedButton="@+id/button_1">
<RadioButton
android:id="@+id/button_1"
...../>
<RadioButton
android:id="@+id/button_2"
...../>
<RadioButton
android:id="@+id/button_3"
...../>
</RadioGroup>
RadioGroup radioGroup = new RadioGroup(WvActivity.this);
RadioButton radioBtn1 = new RadioButton(this);
RadioButton radioBtn2 = new RadioButton(this);
RadioButton radioBtn3 = new RadioButton(this);
radioBtn1.setText("Less");
radioBtn2.setText("Normal");
radioBtn3.setText("More");
radioGroup.addView(radioBtn1);
radioGroup.addView(radioBtn2);
radioGroup.addView(radioBtn3);
radioGroup.check(radioBtn2.getId());
RadioGroup radioGroup = new RadioGroup(context);
RadioButton radioBtn1 = new RadioButton(context);
RadioButton radioBtn2 = new RadioButton(context);
RadioButton radioBtn3 = new RadioButton(context);
radioBtn1.setText("Less");
radioBtn2.setText("Normal");
radioBtn3.setText("More");
radioGroup.addView(radioBtn1);
radioGroup.addView(radioBtn2);
radioGroup.addView(radioBtn3);
radioBtn2.setChecked(true);
동료의 코드에도 같은 문제가있었습니다. 라디오 그룹이 라디오 버튼으로 올바르게 설정되지 않았을 때 들립니다. 이것이 라디오 버튼을 여러 개 선택할 수있는 이유입니다. 나는 많은 것을 시도했고, 마침내 나는 실제로 잘못된 트릭을했지만 잘 작동합니다.
for ( int i = 0 ; i < myCount ; i++ )
{
if ( i != k )
{
System.out.println ( "i = " + i );
radio1[i].setChecked(false);
}
}
여기서는 사용 가능한 라디오 버튼을 확인하고 새로 클릭 한 버튼을 제외한 모든 버튼을 선택 취소하는 for 루프를 설정했습니다. 시도 해봐.
android:checked = "true"
activity.xml에 추가 하십시오
RadioGroup의 버그입니다
RadioButton radioBtn2 = new RadioButton(context);
viewId가없는 radioBtn2이며 generateViewId는 onChildViewAdded ()에 있습니다.
public void onChildViewAdded(View parent, View child) {
if (parent == RadioGroup.this && child instanceof RadioButton) {
int id = child.getId();
// generates an id if it's missing
if (id == View.NO_ID) {
id = View.generateViewId();
child.setId(id);
}
((RadioButton) child).setOnCheckedChangeWidgetListener(
mChildOnCheckedChangeListener);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewAdded(parent, child);
}
}
따라서 먼저 radioGroup.addView (radioBtn2), 다음 radioBtn2.setChecked (true);
이처럼 :
RadioGroup radioGroup = new RadioGroup(context);
RadioButton radioBtn1 = new RadioButton(context);
RadioButton radioBtn2 = new RadioButton(context);
RadioButton radioBtn3 = new RadioButton(context);
radioBtn1.setText("Less");
radioBtn2.setText("Normal");
radioBtn3.setText("More");
radioGroup.addView(radioBtn1);
radioGroup.addView(radioBtn2);
radioGroup.addView(radioBtn3);
radioBtn2.setChecked(true);
참고URL : https://stackoverflow.com/questions/9175635/how-to-set-radio-button-checked-as-default-in-radiogroup
'Programing' 카테고리의 다른 글
앱을 게시 할 때 zip-align을 찾을 수 없습니다 (0) | 2020.07.09 |
---|---|
줄 바꿈없이 인쇄하면 ( 'a'인쇄) 공백을 인쇄합니다. 제거하는 방법은 무엇입니까? (0) | 2020.07.09 |
테이블에서 마지막 행을 선택하십시오. (0) | 2020.07.09 |
HttpURLConnection이 프록시를 사용하도록하려면 어떻게합니까? (0) | 2020.07.09 |
Android Studio Gradle : 오류 : ': app : processDebugGoogleServices'태스크에 대한 실행에 실패했습니다. (0) | 2020.07.09 |