尚未完成Main Activity.java:
package com.CodersCafe.www;
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
;
public class MainActivity extends Activity {
private RadioGroup sizerg;
private RadioGroup icerg;
private RadioGroup sugarrg;
private RadioButton[] sizerb = new RadioButton[3];
private RadioButton[] icerb = new RadioButton[5];
private RadioButton[] sugarrb = new RadioButton[5];
private Button[] cafebtns = new Button[8];
// 咖啡名稱
private String[] cafeName = { "美式咖啡", "招牌咖啡", "海鹽咖啡", "拿鐵咖啡", "義式咖啡",
"卡布奇諾", "焦糖瑪奇朵", "北海道醇濃奶茶", };
// 咖啡價格
private int[][] cafePrice = { { 35, 50, 60 }, { 45, 60, 70 },
{ 45, 60, 70 }, { 55, 70, 80 }, { 55, 70, 80 }, { 55, 70, 80 },
{ 65, 80, 90 }, { 65, 80, 90 }, };
private int radioButtonCheckedColor = Color.RED;
private int radioButtonUnCheckedColor = Color.BLACK;
// 目前選擇大小杯
private int sizeIndex = 2;
// 目前選擇的冰度
private int iceIndex = 4;
// 目前選擇甜度
private int sugarIndex = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
processView();
processControl();
}
private void processView() {
sizerg = (RadioGroup) findViewById(R.id.sizerg);
icerg = (RadioGroup) findViewById(R.id.icerg);
sugarrg = (RadioGroup) findViewById(R.id.sugarrg);
sizerb[0] = (RadioButton) findViewById(R.id.sizerb1);
sizerb[1] = (RadioButton) findViewById(R.id.sizerb2);
sizerb[2] = (RadioButton) findViewById(R.id.sizerb3);
icerb[0] = (RadioButton) findViewById(R.id.icerb1);
icerb[1] = (RadioButton) findViewById(R.id.icerb2);
icerb[2] = (RadioButton) findViewById(R.id.icerb3);
icerb[3] = (RadioButton) findViewById(R.id.icerb4);
icerb[4] = (RadioButton) findViewById(R.id.icerb5);
sugarrb[0] = (RadioButton) findViewById(R.id.sugarrb1);
sugarrb[1] = (RadioButton) findViewById(R.id.sugarrb2);
sugarrb[2] = (RadioButton) findViewById(R.id.sugarrb3);
sugarrb[3] = (RadioButton) findViewById(R.id.sugarrb4);
sugarrb[4] = (RadioButton) findViewById(R.id.sugarrb5);
cafebtns[0] = (Button) findViewById(R.id.cafebtn1);
cafebtns[1] = (Button) findViewById(R.id.cafebtn2);
cafebtns[2] = (Button) findViewById(R.id.cafebtn3);
cafebtns[3] = (Button) findViewById(R.id.cafebtn4);
cafebtns[4] = (Button) findViewById(R.id.cafebtn5);
cafebtns[5] = (Button) findViewById(R.id.cafebtn6);
cafebtns[6] = (Button) findViewById(R.id.cafebtn7);
cafebtns[7] = (Button) findViewById(R.id.cafebtn8);
}
private void processControl() {
// 設定RadioGroup改選的監聽
MyOnCheckedChangeListener myOnCheckedChangeListener = new MyOnCheckedChangeListener();
sizerg.setOnCheckedChangeListener(myOnCheckedChangeListener);
sugarrg.setOnCheckedChangeListener(myOnCheckedChangeListener);
icerg.setOnCheckedChangeListener(myOnCheckedChangeListener);
// 刷新RadioButton文字
updateRadioButtons(sizerg);
updateRadioButtons(sugarrg);
updateRadioButtons(icerg);
// 刷新CafeButtons文字內容
refreshCafeButton();
}
// RadioGroup 改選的受委任者
class MyOnCheckedChangeListener implements OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup parent, int id) {
// TODO Auto-generated method stub
updateRadioButtons(parent);
}
}
private void updateRadioButtons(RadioGroup parent){
//取得RadioGroup內RadioButton數量
int RadioButtonNumber=parent.getChildCount();
for(int i=0; i<RadioButtonNumber;i++){
//輪詢RadioGroup內RadioButton
RadioButton radioButton=(RadioButton)parent.getChildAt(i);
if(radioButton.isChecked()){
//若RadioButton被選取時文字顏色變更
radioButton.setTextColor(radioButtonCheckedColor);
//是否為尺寸的RadioGroup
if(sizerg.equals(parent)){
for(int j=0; j<sizerb.length;j++){
if(sizerb[j].equals(radioButton)){
sizeIndex=j;
}
}
//是否為糖的RadioGroup
if(sugarrg.equals(parent)){
for(int j=0; j<sugarrb.length;j++){
if(sugarrb[j].equals(radioButton)){
sugarIndex=j;
}
}
//是否為冰的RadioGroup
if(icerg.equals(parent)){
for(int j=0; j<icerb.length;j++){
if(icerb[j].equals(radioButton)){
iceIndex=j;
}
}
refreshCafeButton();
}else{
//若RadioButton沒被選取時文字顏色變更
radioButton.setTextColor(radioButtonUnCheckedColor);
}
}
}
// 刷新CafeButton文字內容
private void refreshCafeButton() {
for (int i = 0; i < cafebtns.length; i++) {
cafebtns[i].setText(cafeName[i] + "(" + cafePrice[i][sizeIndex]+")");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
老師Main_Activity:
尚未完成activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/llv0_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="1920"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/llh0_1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="150"
android:weightSum="1080" >
<RadioGroup
android:id="@+id/sizerg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/sizerb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="小杯"
android:textSize="12sp" />
<RadioButton
android:id="@+id/sizerb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中杯"
android:textSize="12sp" />
<RadioButton
android:id="@+id/sizerb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大杯"
android:textSize="12sp" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="@+id/llh0_2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="150"
android:weightSum="1080" >
<RadioGroup
android:id="@+id/sugarrg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/sugarrb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="無糖"
android:textSize="12sp" />
<RadioButton
android:id="@+id/sugarrb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="三分糖"
android:textSize="12sp" />
<RadioButton
android:id="@+id/sugarrb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="半糖"
android:textSize="12sp" />
<RadioButton
android:id="@+id/sugarrb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="七分糖"
android:textSize="12sp" />
<RadioButton
android:id="@+id/sugarrb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正常"
android:textSize="12sp" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="@+id/llh0_3"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="150"
android:weightSum="1080" >
<RadioGroup
android:id="@+id/icerg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/icerb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="熱飲"
android:textSize="12sp" />
<RadioButton
android:id="@+id/icerb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="溫飲"
android:textSize="12sp" />
<RadioButton
android:id="@+id/icerb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="去冰"
android:textSize="12sp" />
<RadioButton
android:id="@+id/icerb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="半冰"
android:textSize="12sp" />
<RadioButton
android:id="@+id/icerb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正常"
android:textSize="12sp" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="@+id/llh0_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="800"
android:weightSum="1080" >
<LinearLayout
android:id="@+id/llv0_4_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="540"
android:orientation="vertical"
android:weightSum="800" >
<Button
android:id="@+id/cafebtn1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="美式咖啡" />
<Button
android:id="@+id/cafebtn1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="招牌咖啡" />
<Button
android:id="@+id/cafebtn2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="海鹽咖啡" />
<Button
android:id="@+id/cafebtn3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="拿鐵咖啡" />
</LinearLayout>
<LinearLayout
android:id="@+id/llv0_4_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="540"
android:orientation="vertical"
android:weightSum="800" >
<Button
android:id="@+id/cafebtn1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="義式咖啡" />
<Button
android:id="@+id/cafebtn1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="卡布奇諾" />
<Button
android:id="@+id/cafebtn2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="焦糖瑪奇朵" />
<Button
android:id="@+id/cafebtn3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="北海道醇濃奶茶" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/llh0_5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="520"
android:weightSum="1080" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFF" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_order"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="已訂購餐點"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="@+id/llh0_6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="150"
android:weightSum="1080" >
<TextView
android:id="@+id/tv_money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="付款金額"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
老師activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/llv0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="1920"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/llh0_1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="150"
android:weightSum="1080" >
<RadioGroup
android:id="@+id/sizerg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/sizerb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="小杯"
android:textSize="10sp" />
<RadioButton
android:id="@+id/sizerb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中杯"
android:textSize="10sp" />
<RadioButton
android:id="@+id/sizerb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大杯"
android:textSize="10sp" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="@+id/llh0_2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="150"
android:weightSum="1080" >
<RadioGroup
android:id="@+id/sugarrg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/sugarrb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="無糖"
android:textSize="10sp" />
<RadioButton
android:id="@+id/sugarrb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="三分糖"
android:textSize="10sp" />
<RadioButton
android:id="@+id/sugarrb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="半糖"
android:textSize="10sp" />
<RadioButton
android:id="@+id/sugarrb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="七分糖"
android:textSize="10sp" />
<RadioButton
android:id="@+id/sugarrb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正常"
android:textSize="10sp" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="@+id/llh0_3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="150"
android:weightSum="1080" >
<RadioGroup
android:id="@+id/icerg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/icerb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="熱飲"
android:textSize="10sp" />
<RadioButton
android:id="@+id/icerb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="溫飲"
android:textSize="10sp" />
<RadioButton
android:id="@+id/icerb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="去冰"
android:textSize="10sp" />
<RadioButton
android:id="@+id/icerb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="半冰"
android:textSize="10sp" />
<RadioButton
android:id="@+id/icerb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正常"
android:textSize="10sp" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="@+id/llh0_4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="800"
android:weightSum="1080" >
<LinearLayout
android:id="@+id/llv0_4_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="540"
android:orientation="vertical"
android:weightSum="800" >
<Button
android:id="@+id/cafebtn1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="美式咖啡" />
<Button
android:id="@+id/cafebtn2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="招牌咖啡" />
<Button
android:id="@+id/cafebtn3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="海岩咖啡" />
<Button
android:id="@+id/cafebtn4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="拿鐵咖啡" />
</LinearLayout>
<LinearLayout
android:id="@+id/llv0_4_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="540"
android:orientation="vertical"
android:weightSum="800" >
<Button
android:id="@+id/cafebtn5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="義式咖啡" />
<Button
android:id="@+id/cafebtn6"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="卡布奇諾" />
<Button
android:id="@+id/cafebtn7"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="焦糖瑪奇朵" />
<Button
android:id="@+id/cafebtn8"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="200"
android:text="北海道醇濃奶茶" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/llh0_5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="520"
android:background="#00FFFF"
android:weightSum="1080" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_order"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="@+id/llh0_6"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="150"
android:weightSum="1080" >
<TextView
android:id="@+id/tv_money"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="0 / 0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="#FFFF00"
android:textColor="#FF0000" />
</LinearLayout>
</LinearLayout>