반응형
RadioButton을 사용하여 성별을 체크하는 앱 만들기
먼저 라디오 그룹 밑에 라디오 버튼 3개를 넣어준다. (레이아웃은 LinearLayout으로 한다. 사실 이 레이아웃으로 왠만한 레이아웃 만들기 다 가능)
1. activity_main.xml
이제 버튼이나 텍스트 그리고 배경에 색을 입혀주었다.
색 참고 주소 https://color.adobe.com/ko/create
https://color.adobe.com/ko/create
color.adobe.com
여기에 들어가서 색을 조합한 다음 복사해서 붙여준다.
android:textColor="#000000"
android:background="#E6D7BE"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:background="#FDFBDE"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="당신은 ?"
android:textColor="#000000"
android:textSize="20dp"
android:background="#E6D7BE"
android:padding="16dp"
android:gravity="center"
android:singleLine="true"
android:textStyle="bold" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio_men"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="남자" />
<RadioButton
android:id="@+id/radio_women"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="여자" />
<RadioButton
android:id="@+id/radio_robot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="로봇" />
</RadioGroup>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E8CA90"
android:textSize="17dp"
android:textStyle="bold"
android:text="확인" />
</LinearLayout>
이 상태에서 저 View Options(눈모양 저거 말하는 거임) 누르고 show Layout Decorations를 누르면
이렇게 된다.
2. MainActivity.java
package com.example.myapplicationtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView textView;
RadioButton radio_men, radio_women, radio_robot;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
radio_men = findViewById(R.id.radio_men);
radio_men.setChecked(true);
radio_men.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(radio_women.isChecked())
textView.setText("여자가 체크되었습니다.");
else
textView.setText("로봇이 체크되었습니다.");
}
});
radio_women = findViewById(R.id.radio_women);
radio_women.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(radio_men.isChecked())
textView.setText("남자가 체크되었습니다.");
else
textView.setText("로봇이 체크되었습니다.");
}
});
radio_robot = findViewById(R.id.radio_robot);
radio_robot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(radio_robot.isChecked())
textView.setText("로봇이 체크되었습니다.");
else
textView.setText("여자가 체크되었습니다.");
}
});
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str;
if(radio_men.isChecked()) {
str = "남자";
}
else if(radio_women.isChecked()){
str = "여자";
}
else{
str = "로봇";
}
Toast.makeText(getApplicationContext(),"당신은" + str,Toast.LENGTH_SHORT).show();
}
});
}
}
3. 실행화면
반응형
'2019~2020 > 안드로이드 스튜디오 기초' 카테고리의 다른 글
[안드로이드 스튜디오] firebase와 연결하고 사용하기 (0) | 2020.05.23 |
---|---|
[안드로이드 스튜디오] paint 사용 (0) | 2020.04.19 |
[안드로이드 스튜디오] canvas (그림판) (0) | 2020.04.19 |
[안드로이드 스튜디오] 별점 앱 (0) | 2020.04.18 |
[안드로이드 스튜디오]intent 사용하기 (0) | 2020.04.05 |