Deperecated/Android_강의

안드로이드 - 라디오 버튼

누알라리 2020. 2. 7. 23:06
1. 라디오 버튼이란?

- 하나의 그룹 안에서 하나만 선택할 수 있도록 하는 뷰

- 그룹 내에서 하나는 반드시 선택되어 있도록 제공되어있다.

 

2. 리스너

- OnCheckedChangedListener

- RadioGroup에 붙는다.

 

3. 라디오 그룹 내 어떤 라디오 버튼이 눌렸는지 버튼으로 알아보는 코드
        button.setOnClickListener { view ->

            when(RadioGroup.checkedRadioButtonId)
            {
                R.id.radioButton->
                    textView.text = "눈";
                R.id.radioButton2->
                    textView.text = "코";
                R.id.radioButton3->
                    textView.text = "입";
            }

            when(RadioGroup2.checkedRadioButtonId)
            {
                R.id.radioButton4->
                    textView2.text = "팔";
                R.id.radioButton5->
                    textView2.text = "다리";
                R.id.radioButton6->
                    textView2.text = "허리";
            }
        }

 

4. 리스너 클래스 생성하여 어떤 버튼이 눌렸는지 보기. 
    inner class RadioListener:RadioGroup.OnCheckedChangeListener
    {
        override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
            when(group?.id)
            {
                R.id.RadioGroup ->
                    when(checkedId)
                    {
                        R.id.radioButton->
                            textView.text = "체크 이벤트 : 눈";
                        R.id.radioButton2->
                            textView.text = "체크 이벤트 : 코";
                        R.id.radioButton3->
                            textView.text = "체크 이벤트 : 입";
                    }
                R.id.RadioGroup2 ->
                    when(checkedId) {
                        R.id.radioButton4 ->
                            textView2.text = "체크 이벤트 : 허리";
                        R.id.radioButton5 ->
                            textView2.text = "체크 이벤트 : 다리";
                        R.id.radioButton6 ->
                            textView2.text = "체크 이벤트 : 팔";
                    }
            }
        }
    }
    
    
        var Listener2 = RadioListener();
        RadioGroup.setOnCheckedChangeListener(Listener2);
        RadioGroup2.setOnCheckedChangeListener(Listener2);

 

5. 람다식으로 어느 라디오 버튼이 선택되었는지 확인하기
        RadioGroup.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId)
            {
                R.id.radioButton->
                    textView.text = "체크 이벤트 : 눈";
                R.id.radioButton2->
                    textView.text = "체크 이벤트 : 코";
                R.id.radioButton3->
                    textView.text = "체크 이벤트 : 입";
            }
        }

        RadioGroup2.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId)
            {
                R.id.radioButton->
                    textView2.text = "체크 이벤트 : 눈";
                R.id.radioButton2->
                    textView2.text = "체크 이벤트 : 코";
                R.id.radioButton3->
                    textView2.text = "체크 이벤트 : 입";
            }
        }

'Deperecated > Android_강의' 카테고리의 다른 글

안드로이드 - SeekBar  (0) 2020.02.08
안드로이드 - Progress Bar  (0) 2020.02.07
안드로이드 - CheckBox  (0) 2020.02.07
안드로이드 - View - Button  (0) 2020.02.07
안드로이드 - View - TextView  (0) 2020.02.07