RadioButton

이 섹션에서는 를 사용하여 두 개의 상호 배타적 라디오 단추를 만듭니다(하나를 사용하도록 설정하면 다른 단추는 사용하지 않도록 설정). RadioGroupRadioButton 위젯. 라디오 단추를 누르면 알림 메시지가 표시됩니다.

Resources/layout/Main.axml 파일을 열고 (내부LinearLayout) 중첩된 두 개의 RadioButtons를 RadioGroup 추가합니다.

<RadioGroup
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical">
  <RadioButton android:id="@+id/radio_red"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Red" />
  <RadioButton android:id="@+id/radio_blue"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Blue" />
</RadioGroup>

RadioButton한 번에 하나 이상 선택할 수 없도록 요소별로 RadioGroup s를 그룹화해야 합니다. 이 논리는 Android 시스템에서 자동으로 처리됩니다. 1일 때 RadioButton 그룹 내에서 다른 모든 항목이 자동으로 선택 취소됩니다.

RadioButton 항목을 선택할 때 작업을 수행하려면 이벤트 처리기를 작성해야 합니다.

private void RadioButtonClick (object sender, EventArgs e)
{
    RadioButton rb = (RadioButton)sender;
    Toast.MakeText (this, rb.Text, ToastLength.Short).Show ();
}

먼저 전달되는 발신자가 RadioButton으로 캐스팅됩니다. 그런 다음 Toast 메시지는 선택한 라디오 단추의 텍스트를 표시합니다.

지금, 의 하단에 OnCreate() 메서드를 사용하여 다음을 추가합니다.

RadioButton radio_red = FindViewById<RadioButton>(Resource.Id.radio_red);
RadioButton radio_blue = FindViewById<RadioButton>(Resource.Id.radio_blue);

radio_red.Click += RadioButtonClick;
radio_blue.Click += RadioButtonClick;

이렇게 하면 레이아웃에서 각 s를 RadioButton캡처하고 새로 만든 이벤트 처리기를 각각에 추가합니다.

애플리케이션을 실행합니다.

상태를 직접 변경해야 하는 경우(예: 저장된 상태를 CheckBoxPreference로드할 때) 다음을 사용합니다. Checked 속성 setter 또는 Toggle() 메서드를 호출하여 생성됩니다.

이 페이지의 일부는 Android 오픈 소스 프로젝트에서 만들고 공유하고 Creative Commons 2.5 특성 라이선스에 설명된 용어에따라 사용되는 작업을 기반으로 하는 수정 사항입니다.