일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 쿠버네티스
- CUK
- 코테
- BFS
- AWS
- DFS
- homelab
- k8s
- AwsCloudClubs
- 프로그래머스
- 커뮤니티
- GDGCampusKorea
- 파이썬
- eks
- Python
- kubecostinstall
- GDG
- GDSC
- 나동빈
- ci/cd
- 비용최적화
- kubecost
- 설치
- CI
- ekskubecost
- kubecost설치
- eks비용
- karpenter
- GithubActions
- Whatever
Archives
- Today
- Total
촬리의늘솔길
앱만들기.35(Radio Button) 본문
RadioButton은 주로 여러종류의 선택항목에서
한가지만 선택하는 형태의 버튼입니다.
안드로이드 디바이스에서 RadioButton.isChecked() 메소드를 통해 체크되었는지
확인하여 앱을 개발하면 됩니다.
Difference between RadioGroup and RadioButton?
RadioButton은 주로 RadioGroup 내에 존재합니다.
출처: [유혁의 개발 스토리]
rg_gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { //라디오 버튼들의 상태값 변경감지
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
});
main.java
package com.example.radiobuttonexample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private RadioGroup rg_gender;
private RadioButton rb_man, rb_woman;
private Button btn_result;
private String str_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg_gender = findViewById(R.id.rg_gender); //라디오 버튼들을 담고있는 그룹
rb_man = findViewById(R.id.rb_man); // 라디오 버튼들
rb_woman = findViewById(R.id.rb_woman); // 라디오 버튼
btn_result = findViewById(R.id.btn_result); //결과값을 출력하라는 신호를 보낼 버튼
rg_gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { //라디오 버튼들의 상태값 변경감지
@Override
public void onCheckedChanged(RadioGroup group, int i) {
if(i == R.id.rb_man){//남자가 선택이 된다면
Toast.makeText(MainActivity.this,"남자 라디오 버튼",Toast.LENGTH_SHORT).show();
str_result = rb_man.getText().toString(); //남자 라디오 버튼의 텍스트값을 String에 담아줌
}else if(i== R.id.rb_woman){
Toast.makeText(MainActivity.this,"여자 라디오 버튼",Toast.LENGTH_SHORT).show();
str_result = rb_woman.getText().toString(); //여자 라디오 버튼의 텍스트값을 String에 담아줌
}
}
});
//버튼을 누르지 않으면 onchecked가 실행안되어서 str가 null값이 되므로 else에 값 넣어줌
btn_result.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(str_result !=null){ //str_result 가 빈 값이 아니라면
Toast.makeText(MainActivity.this,str_result,Toast.LENGTH_SHORT).show();
}else{//str_result 가 빈 값일경우
Toast.makeText(MainActivity.this,"라디오 버튼 선택해주세요",Toast.LENGTH_SHORT).show();
}
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/rg_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/rb_man"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="남자" />
<RadioButton
android:id="@+id/rb_woman"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="여자" />
</RadioGroup>
<Button
android:id="@+id/btn_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="결과버튼"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rg_gender" />
</androidx.constraintlayout.widget.ConstraintLayout>
728x90
'✍~2022 > app(android studio)' 카테고리의 다른 글
앱만들기.37(Google Login) (0) | 2021.08.18 |
---|---|
앱만들기.36(Check Box) (0) | 2021.08.16 |
앱만들기. 40 (android studio Github연동으로 협업하는 방법) (0) | 2021.08.12 |
H &Y Calendar (0) | 2021.08.11 |
앱만들기.34 (ViewPager androidx ver) (0) | 2021.08.09 |