close
製作一個簡易的點餐系統
將上面的選擇顯示在下面的textview裡面
view的部分如上圖所示
開啟source裡的string
<resources> <string name="app_name">order</string> <string name="desknum">桌號</string> <string name="custom">一般顧客</string> <string name="VIP">VIP</string> <string name="main">主餐</string> <string name="A">A餐100元</string> <string name="B">B餐150元</string> <string name="C">C餐200元</string> <string name="D">D餐250元</string> <string name="snack">附餐</string> <string name="coffee">咖啡</string> <string name="blacktea">紅茶</string> <string name="finish">結帳</string> </resources>
程式碼的部分
1.定義
EditText etTabNum; RadioButton rbCustom,rbVIP,rbcoffee,rbblacktea; CheckBox cbA,cbB,cbC,cbD; Button btn; TextView tvShow;
2.連接
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findviews(); } void findviews(){ etTabNum=(EditText)findViewById(R.id.editText); rbCustom=(RadioButton)findViewById(R.id.radioButton); rbVIP=(RadioButton)findViewById(R.id.radioButton2); rbcoffee=(RadioButton)findViewById(R.id.radioButton4); rbblacktea=(RadioButton)findViewById(R.id.radioButton3); cbA=(CheckBox)findViewById(R.id.checkBox); cbB=(CheckBox)findViewById(R.id.checkBox3); cbC=(CheckBox)findViewById(R.id.checkBox2); cbD=(CheckBox)findViewById(R.id.checkBox4); btn=(Button)findViewById(R.id.button); btn.setOnClickListener(btnOnlick); tvShow=(TextView)findViewById(R.id.textView5); }
3.按鈕
==>這部分比較複雜 分成三個步驟(觀念為:匿名內部類別實作介面)
a.建立按鈕元件
btn=(Button)findViewById(R.id.button);
b.建立監聽器
View.OnClickListener btnOnlick = new View.OnClickListener() { @Override public void onClick(View v) { } };
c.註冊監聽器
btn.setOnClickListener(btnOnlick);
4.按鈕內的程式碼 ==>使用StringBuilder這個物件
StringBuilder sb = new StringBuilder(); sb.append(getResources().getString(R.string.desknum)).append(etTabNum.getText().toString()).append("\n"); if (rbCustom.isChecked()) { sb.append(rbCustom.getText()).append("\n"); } else { sb.append(rbVIP.getText()).append("\n"); } sb.append("主餐").append("\n").append(" "); if (cbA.isChecked()) { sb.append(cbA.getText()).append("\n").append((" ")); } if (cbB.isChecked()) { sb.append(cbB.getText()).append("\n").append((" ")); } if (cbC.isChecked()) { sb.append(cbC.getText()).append("\n").append((" ")); } if (cbD.isChecked()) { sb.append(cbD.getText()).append(("\n")); } sb.append("附餐").append("\n"); if (rbcoffee.isChecked()) { sb.append(" ").append(rbcoffee.getText()).append("\n"); } if (rbblacktea.isChecked()) { sb.append(" ").append(rbblacktea.getText()).append("\n"); } tvShow.setText(sb);
全站熱搜