close

res/layou/activity_main.xml

<?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:orientation="vertical"
    android:background="@drawable/pic"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.lai.class3_order.MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tableNumber"
            android:textSize="@dimen/BigText"
            android:textColor="@color/colorAccent"
            android:id="@+id/textView" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText" />
    </LinearLayout>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_margin="20dp"
        android:orientation="horizontal">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/BigText"
            android:text="@string/normal"
            android:textColor="@color/colorAccent"
            android:id="@+id/radioButton" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/BigText"
            android:textColor="@color/colorAccent"
            android:text="@string/vip"
            android:id="@+id/radioButton2" />
    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/meal"

        android:textSize="@dimen/BigText"
        android:layout_gravity="center_horizontal"
        android:id="@+id/textView2" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:gravity="center_horizontal"
        android:layout_margin="20dp"
        android:layout_height="wrap_content">

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/BigText"
            android:text="@string/ameal"
            android:id="@+id/checkBox" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:text="@string/bmeal"
            android:textSize="@dimen/BigText"
            android:id="@+id/checkBox2"
            android:checked="false" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:gravity="center_horizontal"
        android:layout_margin="20dp"
        android:layout_height="wrap_content">

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/BigText"
            android:text="@string/cmeal"
            android:id="@+id/checkBox3" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/BigText"
            android:text="@string/dmeal"
            android:id="@+id/checkBox4" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textColor="@color/colorAccent"
        android:textSize="@dimen/BigText"
        android:text="@string/beverage"
        android:id="@+id/textView3" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/BigText"
            android:text="@string/tea"
            android:id="@+id/radioButton3" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/BigText"
            android:text="@string/coffee"
            android:id="@+id/radioButton4" />
    </RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="20dp"
        android:textColor="@color/colorAccent"
        android:textSize="@dimen/BigText"
        android:text="@string/submit"
        android:id="@+id/button" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textColor="@color/colorAccent"
        android:textSize="@dimen/BigText"
        android:text="@string/totalMoney"
        android:padding="20dp"
        android:id="@+id/textView4" />
</LinearLayout>

 

java/MainActivity

package com.lai.class3_order;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText etTableNumber;
    RadioButton rbNormal,rbVip,rbTea,rbCoffee;
    CheckBox cbA,cbB,cbC,cbD;
    Button btn;
    TextView tvShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findviews();
    }

    void findviews(){
        etTableNumber = (EditText)findViewById(R.id.editText);
        rbNormal = (RadioButton)findViewById(R.id.radioButton);
        rbVip = (RadioButton)findViewById(R.id.radioButton2);
        rbTea = (RadioButton)findViewById(R.id.radioButton3);
        rbCoffee = (RadioButton)findViewById(R.id.radioButton4);
        cbA = (CheckBox)findViewById(R.id.checkBox);
        cbA.setTag(100.0); //新增價錢的標籤
        cbB = (CheckBox)findViewById(R.id.checkBox2);
        cbB.setTag(150.0);
        cbC = (CheckBox)findViewById(R.id.checkBox3);
        cbC.setTag(200.0);
        cbD = (CheckBox)findViewById(R.id.checkBox4);
        cbD.setTag(250.0);
        btn = (Button)findViewById(R.id.button);
        tvShow = (TextView)findViewById(R.id.textView4);
        //設定監聽器
        btn.setOnClickListener(btnListener);

    }

    //建立監聽器
    View.OnClickListener btnListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double total=0.0;
            StringBuilder sb = new StringBuilder();
            sb.append("桌號").append(etTableNumber.getText().toString()).append("\n");
            if(rbNormal.isChecked()){
                sb.append(rbNormal.getText()).append("\n");
            }else{
                sb.append(rbVip.getText()).append("\n");
            }
            //主餐的目錄
            sb.append(getResources().getString(R.string.meal)).append("\n").append(" ");
            if(cbA.isChecked()){
                sb.append(cbA.getText()).append(" ");
                total+=(double)cbA.getTag();
            }
            if(cbB.isChecked()){
                sb.append(cbB.getText()).append(" ");
                total+=(double)cbB.getTag();
            }
            if(cbC.isChecked()){
                sb.append(cbC.getText()).append(" ");
                total+=(double)cbC.getTag();
            }
            if(cbD.isChecked()){
                sb.append(cbD.getText()).append(" ");
                total+=(double)cbD.getTag();
            }
            //附餐的目錄
            sb.append("\n").append(getResources().getString(R.string.beverage)).append("\n").append(" ");
            if(rbTea.isChecked()){
                sb.append(rbTea.getText()).append("\n");
            }else{
                sb.append(rbCoffee.getText()).append("\n");
            }

            if(rbVip.isChecked()){
                sb.append(getResources().getString(R.string.totalMoney)).append(total*0.9).append(getResources()).append(getString(R.string.dollar));
            }else{
                sb.append(getResources().getString(R.string.totalMoney)).append(total).append(getResources()).append(getString(R.string.dollar));

            }

            tvShow.setText(sb);

        }
    };
}
arrow
arrow
    文章標籤
    Android
    全站熱搜
    創作者介紹
    創作者 mingyilai 的頭像
    mingyilai

    mingyilai的部落格

    mingyilai 發表在 痞客邦 留言(0) 人氣()