<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="고기"
android:onClick="onClick_1"
android:id="@+id/box1"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="피자"
android:onClick="onClick_2"
android:id="@+id/box2" />
</LinearLayout>
package kr.hs.dsm.appb.ch_6_4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
CheckBox check;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick_1(View v) {
boolean checked = ((CheckBox) v).isChecked();
switch (v.getId()) {
case R.id.box1:
if(checked)
Toast.makeText(getApplicationContext(),"고기 선택", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(),"고기 선택 해제", Toast.LENGTH_SHORT).show();
break;
}
}
public void onClick_2(View v){
boolean checked = ((CheckBox) v).isChecked();
switch (v.getId()) {
case R.id.box2 :
if(checked)
Toast.makeText(getApplicationContext(),"피자 선택",Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(),"피자 선택 해제",Toast.LENGTH_SHORT).show();
break;
}
}
}