Choose background color

This commit is contained in:
2018-11-07 16:16:28 +08:00
parent 1b2c41a7c7
commit be52e67698
2 changed files with 105 additions and 49 deletions

View File

@ -1,21 +1,28 @@
package me.jj97181818.ch05_foodmenueventhw; package me.jj97181818.ch05_foodmenueventhw;
import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.View; import android.view.View;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView; import android.widget.TextView;
import java.util.ArrayList; 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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); 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}; 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); selected.add(buttonView);
else else
selected.remove(buttonView); 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) { public void takeOrder(View v) {
@ -49,15 +47,15 @@ public class MainActivity extends AppCompatActivity implements CompoundButton.On
if (selected.size() == 3) { if (selected.size() == 3) {
for (CompoundButton chk:selected) for (CompoundButton chk:selected)
msg += "\n" + chk.getText(); msg += " " + chk.getText();
msg = "你點購的餐點是:" + msg + "\n總共是 50 元。"; msg = "你點購的餐點是:" + msg + "總共是 50 元。";
} }
else if (selected.size() == 4) { else if (selected.size() == 4) {
for (CompoundButton chk:selected) for (CompoundButton chk:selected)
msg += "\n" + chk.getText(); msg += " " + chk.getText();
msg = "你點購的餐點是:" + msg + "\n總共是 60 元。"; msg = "你點購的餐點是:" + msg + "總共是 60 元。";
} }
else if (selected.size() == 0){ else if (selected.size() == 0){
msg = "請點餐!"; 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); ((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;
}
}
} }

View File

@ -56,6 +56,30 @@
app:layout_constraintStart_toEndOf="@+id/chk3" app:layout_constraintStart_toEndOf="@+id/chk3"
app:layout_constraintTop_toBottomOf="@+id/chk2" /> 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 <Button
android:id="@+id/button" android:id="@+id/button"
android:layout_width="0dp" android:layout_width="0dp"
@ -83,39 +107,54 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" /> app:layout_constraintTop_toBottomOf="@+id/button" />
<RadioButton <RadioGroup
android:id="@+id/bgGreen" android:id="@+id/unit"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="70dp" android:layout_marginStart="8dp"
android:layout_marginBottom="70dp" android:layout_marginTop="8dp"
android:text="背景顏色" android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" 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 <RadioButton
android:id="@+id/chk5" android:id="@+id/bgRed"
android:layout_width="wrap_content" android:layout_width="50dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="7dp" android:text=""
android:layout_marginTop="17dp" tools:layout_editor_absoluteX="44dp"
android:text="豬血糕" tools:layout_editor_absoluteY="415dp"
android:textSize="30sp" tools:layout_marginRight="8dp" />
app:layout_constraintEnd_toStartOf="@+id/chk6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chk3" />
<CheckBox <RadioButton
android:id="@+id/chk6" android:id="@+id/bgGreen"
android:layout_width="wrap_content" android:layout_width="50dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="17dp" android:text="綠"
android:layout_marginEnd="25dp" tools:layout_editor_absoluteX="125dp"
android:text="牛排" tools:layout_editor_absoluteY="415dp"
android:textSize="30sp" tools:layout_marginRight="8dp" />
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/chk5" <RadioButton
app:layout_constraintTop_toBottomOf="@+id/chk4" /> 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_weight="1"
android:text="白"
tools:layout_editor_absoluteX="292dp"
tools:layout_editor_absoluteY="415dp" />
</RadioGroup>
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>