1. CheckBox 위젯
1) 레이아웃 부분
디자인의 위젯 기능을 이용하여 checkbox를 추가한다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.progdv.app7_checkbox.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="체크하세요!"
android:id="@+id/textView" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox1"
android:id="@+id/checkBox"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="58dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox2"
android:id="@+id/checkBox2"
android:layout_below="@+id/checkBox"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="44dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox3"
android:id="@+id/checkBox3"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
2) 기능 구현 부분
체크박스는 OnCheckedChangeListener 를 이용하여 기능을 구현한다.
package com.progdv.app7_checkbox;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements CheckBox.OnCheckedChangeListener {
CheckBox chk1, chk2, chk3;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chk1 = (CheckBox)findViewById(R.id.checkBox);
chk2 = (CheckBox)findViewById(R.id.checkBox2);
chk3 = (CheckBox)findViewById(R.id.checkBox3);
chk1.setOnCheckedChangeListener(this);
chk2.setOnCheckedChangeListener(this);
chk3.setOnCheckedChangeListener(this);
result = (TextView)findViewById(R.id.textView);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(chk1.isChecked()) {
result.setText("첫번째 체크박스 선택");
}
if(chk2.isChecked()) {
result.setText("두번째 체크박스 선택");
}
if(chk3.isChecked()) {
result.setText("세번째 체크박스 선택");
}
}
}
2. RadioButton 위젯
1) 레이아웃 부분
디자인의 위젯 기능을 이용하여 RadioButton를 추가한다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.progdv.app8_radiobutton.MainActivity">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton2"
android:layout_below="@+id/radioButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton3"
android:layout_below="@+id/radioButton2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RadioGroup>
</RelativeLayout>
2) 기능 부분
RadioButton은 RadioGroup으로 묶어줘야 한다.
package com.progdv.app8_radiobutton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
RadioButton rBtn1, rBtn2, rBtn3;
RadioGroup rGroup;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rBtn1 = (RadioButton)findViewById(R.id.radioButton);
rBtn2 = (RadioButton)findViewById(R.id.radioButton2);
rBtn3 = (RadioButton)findViewById(R.id.radioButton3);
rGroup = (RadioGroup)findViewById(R.id.radioGroup);
result = (TextView)findViewById(R.id.textView);
rGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
case R.id.radioButton:
result.setText("첫번째 라디오버튼 선택");
break;
case R.id.radioButton2:
result.setText("두번째 라디오버튼 선택");
break;
case R.id.radioButton3:
result.setText("세번째 라디오버튼 선택");
break;
}
}
}
'안드로이드' 카테고리의 다른 글
레이아웃: LinearLayout (0) | 2016.04.21 |
---|---|
위젯: ImageView, ButtonView (0) | 2016.04.21 |
위젯: Button (0) | 2016.04.21 |
위젯: Textview, EditText (0) | 2016.04.20 |
레이아웃 만들기 (0) | 2016.04.20 |