베어_
TechBear
베어_
전체 방문자
오늘
어제
  • 분류 전체보기 (336)
    • Spring (33)
      • 개념 (13)
      • Security (5)
      • 실습 (1)
      • 토비 스프링 (11)
    • JPA (6)
    • 프로젝트 기록 (24)
    • DB (13)
    • JAVA (18)
    • 알고리즘 (50)
      • 유형정리 (8)
      • Baekjoon (21)
      • LeetCode (18)
    • 디자인패턴 (0)
    • 개발서적 (79)
      • Effective Java (78)
      • 객체지향의 사실과 오해 (1)
    • 독후감 (4)
    • 보안 (2)
    • 운영체제(OS) (53)
      • 공룡책 (53)
    • 컴퓨터 네트워크 (28)
      • 컴퓨터 네트워크 하향식 접근 (23)
    • 자료구조 (1)
    • DevOps (2)
    • 앱 개발 (20)
      • 안드로이드 스튜디오 (20)

블로그 메뉴

    공지사항

    인기 글

    태그

    • 자바8
    • 데이터베이스
    • BFS
    • 스프링
    • leetcode
    • 스프링시큐리티
    • 함수형인터페이스
    • 자바
    • 이펙티브자바
    • dfs
    • C++
    • 알고리즘
    • java
    • 토비스프링
    • 스레드
    • 백준
    • 운영체제
    • jpa
    • 코드업
    • Spring

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    베어_

    TechBear

    앱 개발/안드로이드 스튜디오

    [안드로이드 스튜디오] 라디오 버튼, RadioButton

    2020. 8. 14. 02:58

    [xml 파일]

    radioGroup을 만든 후 안에 radiobutton을 2개 넣어줍니다. (저는 버튼도 만들었습니다)

    radiogroup을 만드는 이유는 radiobutton을 한 개만 선택하게 만들기 위함입니다.

    cf ) radiogroup없이 버튼을 나열할 경우 중복 선택이 됩니다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
     <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="224dp"
            android:orientation="horizontal">
     
            <RadioButton
                android:id="@+id/radioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Man" />
     
            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Girl" />
        </RadioGroup>
     
    <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/radioGroup"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="25dp"
            android:text="ENGLISH NAME" />
    cs

     

    [.java파일]

    radioGroup = (RadioGroup) findViewById(R.id.radioGroup); 을 참조해줍니다.

    radioGroup.setOnCheckedChangeListener(radioGroupClickListener); // 클릭 되었을 때를 정의해줍니다

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    public class MainActivity extends AppCompatActivity {
        private RadioGroup radioGroup;
        int state;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            radioGroup = (RadioGroup) findViewById(R.id.radioGroup);          //라디오 그룹 참조하기
            radioGroup.setOnCheckedChangeListener(radioGroupClickListener);  // 클릭 되었을 때~~ onChcekedChanged함수를 따라라
     
     
            Button button = findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(state == 1) {
                        Intent intent = new Intent(MainActivity.this, EnglishName_man.class);
                        startActivity(intent);
                    } else if(state == 2) {
                        Intent intent = new Intent(MainActivity.this, EnglishName_girl.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(getApplicationContext(), "Choose the gender", Toast.LENGTH_LONG).show();
                    }
     
                }
            });
     
    RadioGroup.OnCheckedChangeListener radioGroupClickListener = new RadioGroup.OnCheckedChangeListener() {  // 중요
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.radioButton) {                // 첫 번째 버튼이 선택 되었을 때 
                    state = 1;
                } else if (i == R.id.radioButton2) {      // 두 번째 버튼이 선택 되었을 때
                    state = 2;
                } else {
                    state = 3;
                }
            }
        };
    }
    Colored by Color Scripter
    cs

     

     

     

     

     

     

      '앱 개발/안드로이드 스튜디오' 카테고리의 다른 글
      • [안드로이드 스튜디오] Fragment에서 findviewbyid 오류
      • [안드로이드 스튜디오] EditText 밑줄 없애기
      • [안드로이드 스튜디오] 프래그먼트에서 클릭 이벤트 오류
      • [안드로이드 스튜디오] WebView를 사용해 네이버 연결하기
      베어_
      베어_
      Today I learned | 문제를 해결하는 개발자

      티스토리툴바