diff --git a/app/src/main/java/me/jj97181818/ch05_foodmenueventhw/MainActivity.java b/app/src/main/java/me/jj97181818/ch05_foodmenueventhw/MainActivity.java index 85bef1e..700717c 100644 --- a/app/src/main/java/me/jj97181818/ch05_foodmenueventhw/MainActivity.java +++ b/app/src/main/java/me/jj97181818/ch05_foodmenueventhw/MainActivity.java @@ -1,21 +1,28 @@ package me.jj97181818.ch05_foodmenueventhw; +import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.CheckBox; import android.widget.CompoundButton; +import android.widget.RadioGroup; import android.widget.TextView; import java.util.ArrayList; -public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { +public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener { + + RadioGroup unit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + unit = (RadioGroup) findViewById(R.id.unit); + unit.setOnCheckedChangeListener(this); + int [] chk_id = {R.id.chk1, R.id.chk2, R.id.chk3, R.id.chk4, R.id.chk5, R.id.chk6}; //註冊每個方塊的監聽事件 @@ -33,15 +40,6 @@ public class MainActivity extends AppCompatActivity implements CompoundButton.On selected.add(buttonView); else selected.remove(buttonView); - -// if (buttonView.getId() == R.id.bgGreen) { -// View bg = findViewById(R.id.bg); -// if (isChecked) -// bg.setBackgroundColor(Color.rgb(255,0, 0)); -// else -// bg.setBackgroundColor(Color.rgb(255, 255, 255)); -// return; -// } } public void takeOrder(View v) { @@ -49,15 +47,15 @@ public class MainActivity extends AppCompatActivity implements CompoundButton.On if (selected.size() == 3) { for (CompoundButton chk:selected) - msg += "\n" + chk.getText(); + msg += " " + chk.getText(); - msg = "你點購的餐點是:" + msg + "\n總共是 50 元。"; + msg = "你點購的餐點是:" + msg + ",總共是 50 元。"; } else if (selected.size() == 4) { for (CompoundButton chk:selected) - msg += "\n" + chk.getText(); + msg += " " + chk.getText(); - msg = "你點購的餐點是:" + msg + "\n總共是 60 元。"; + msg = "你點購的餐點是:" + msg + ",總共是 60 元。"; } else if (selected.size() == 0){ msg = "請點餐!"; @@ -70,12 +68,31 @@ public class MainActivity extends AppCompatActivity implements CompoundButton.On } - -// if(msg.length() > 0) -// -// else -// msg = "請點餐!"; - ((TextView) findViewById(R.id.showOrder)).setText(msg); } + + @Override + public void onCheckedChanged(RadioGroup group, int checkedId) { + change(); + return; + } + + public void change () { + View bg = findViewById(R.id.bg); +// RadioGroup unit = (RadioGroup) findViewById(R.id.unit); + switch (unit.getCheckedRadioButtonId()) { + case R.id.bgRed: + bg.setBackgroundColor(Color.rgb(211, 127, 129)); + break; + case R.id.bgGreen: + bg.setBackgroundColor(Color.rgb(90, 176, 120)); + break; + case R.id.bgBlue: + bg.setBackgroundColor(Color.rgb(143, 188, 237)); + break; + case R.id.bgWhite: + bg.setBackgroundColor(Color.rgb(255,255, 255)); + break; + } + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index adb7da6..d0a8e75 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -56,6 +56,30 @@ app:layout_constraintStart_toEndOf="@+id/chk3" app:layout_constraintTop_toBottomOf="@+id/chk2" /> + + + +