Choose background color
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -56,6 +56,30 @@
|
||||
app:layout_constraintStart_toEndOf="@+id/chk3"
|
||||
app:layout_constraintTop_toBottomOf="@+id/chk2" />
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/chk5"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="7dp"
|
||||
android:layout_marginTop="17dp"
|
||||
android:text="豬血糕"
|
||||
android:textSize="30sp"
|
||||
app:layout_constraintEnd_toStartOf="@+id/chk6"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/chk3" />
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/chk6"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="17dp"
|
||||
android:layout_marginEnd="25dp"
|
||||
android:text="牛排"
|
||||
android:textSize="30sp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/chk5"
|
||||
app:layout_constraintTop_toBottomOf="@+id/chk4" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button"
|
||||
android:layout_width="0dp"
|
||||
@ -83,39 +107,54 @@
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/button" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/bgGreen"
|
||||
android:layout_width="wrap_content"
|
||||
<RadioGroup
|
||||
android:id="@+id/unit"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="70dp"
|
||||
android:layout_marginBottom="70dp"
|
||||
android:text="背景顏色"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.0"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/showOrder" />
|
||||
app:layout_constraintTop_toBottomOf="@+id/showOrder"
|
||||
app:layout_constraintVertical_bias="0.36"
|
||||
tools:orientation="horizontal">
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/chk5"
|
||||
<RadioButton
|
||||
android:id="@+id/bgRed"
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="紅"
|
||||
tools:layout_editor_absoluteX="44dp"
|
||||
tools:layout_editor_absoluteY="415dp"
|
||||
tools:layout_marginRight="8dp" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/bgGreen"
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="綠"
|
||||
tools:layout_editor_absoluteX="125dp"
|
||||
tools:layout_editor_absoluteY="415dp"
|
||||
tools:layout_marginRight="8dp" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/bgBlue"
|
||||
android:layout_width="50dp"
|
||||
android:text="藍"
|
||||
tools:layout_editor_absoluteX="208dp"
|
||||
tools:layout_editor_absoluteY="415dp" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/bgWhite"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="7dp"
|
||||
android:layout_marginTop="17dp"
|
||||
android:text="豬血糕"
|
||||
android:textSize="30sp"
|
||||
app:layout_constraintEnd_toStartOf="@+id/chk6"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/chk3" />
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/chk6"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="17dp"
|
||||
android:layout_marginEnd="25dp"
|
||||
android:text="牛排"
|
||||
android:textSize="30sp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/chk5"
|
||||
app:layout_constraintTop_toBottomOf="@+id/chk4" />
|
||||
android:layout_weight="1"
|
||||
android:text="白"
|
||||
tools:layout_editor_absoluteX="292dp"
|
||||
tools:layout_editor_absoluteY="415dp" />
|
||||
</RadioGroup>
|
||||
</android.support.constraint.ConstraintLayout>
|
Reference in New Issue
Block a user