Project: Calculator App
🧮 Project: Android Calculator App
នៅក្នុងគម្រោងនេះ យើងនឹងបង្កើត Calculator App មួយដែលមានមុខងារពេញលេញ។ នេះគឺជាគម្រោងដ៏ល្អមួយសម្រាប់អនុវត្តន៍ចំណេះដឹងដែលបានរៀន!
📋 Features នៃ Calculator App
Feature | ពិពណ៌នា |
---|---|
➕ Basic Operations | បូក ដក គុណ ចែក |
🔢 Display Screen | បង្ហាញលេខ និងលទ្ធផល |
🔄 Clear Function | សម្អាត display |
⌫ Delete Function | លុបលេខម្តងមួយ |
📍 Decimal Point | គណនាលេខទសភាគ |
📁 Project Structure
មុនពេលចាប់ផ្តើម សូមយល់ដឹងអំពីរចនាសម្ព័ន្ធ files នៃគម្រោង:
📦 CalculatorApp/ ├── 📂 app/ │ ├── 📂 src/ │ │ ├── 📂 main/ │ │ │ ├── 📂 java/ │ │ │ │ └── 📂 com/example/calculator/ │ │ │ │ └── MainActivity.java │ │ │ ├── 📂 res/ │ │ │ │ ├── 📂 layout/ │ │ │ │ │ └── activity_calculator.xml │ │ │ │ ├── 📂 values/ │ │ │ │ │ ├── colors.xml │ │ │ │ │ └── strings.xml │ │ │ │ └── 📂 drawable/ │ │ │ └── AndroidManifest.xml │ └── build.gradle └── build.gradle
📝 Note: បង្កើត New Project នៅក្នុង Android Studio ជាមួយ Empty Activity template ហើយ folder structure នេះនឹងត្រូវបានបង្កើតដោយស្វ័យប្រវត្តិ។
🎨 Step 1: XML Layout (activity_calculator.xml)
បង្កើត layout សម្រាប់ Calculator ដែលមាន display screen និង buttons ទាំងអស់។
Java Code
Click "Run" to execute the Java code
💻 Step 2: MainActivity.java
បង្កើត logic សម្រាប់ calculator ដោយប្រើ Java។
Java Code
Click "Run" to execute the Java code
🎯 ការពន្យល់ Code
1. Variables
tvDisplay
- TextView សម្រាប់បង្ហាញលេខ និងលទ្ធផលfirstValue
- លេខដំបូងsecondValue
- លេខទីពីរoperator
- operator (+, -, *, /)isNewOperation
- flag ដើម្បីដឹងថា operation ថ្មីឬអត់
2. Methods
Method | មុខងារ |
---|---|
setupNumberButtons() |
Setup listeners សម្រាប់ number buttons (0-9) |
setupOperatorButtons() |
Setup listeners សម្រាប់ operator buttons (+, -, *, /) |
onNumberClick() |
Handle number button click |
onOperatorClick() |
រក្សាទុកលេខ និង operator |
calculate() |
គណនាលទ្ធផល |
performOperation() |
ធ្វើការគណនាតាម operator |
✨ Features បន្ថែម (Enhancements)
💡 អ្នកអាចបន្ថែមបាន:
- 🔢 Percentage calculation (%)
- 📐 Square root (√)
- 💪 Power (x²)
- 📊 History of calculations
- 🎨 Dark/Light theme
- 🔊 Sound effects
- 📱 Vibration on button click
🎨 Styling Tips
ដើម្បីធ្វើឱ្យ Calculator ស្អាតជាង:
- Colors: ប្រើពណ៌ផ្សេងៗសម្រាប់ number buttons និង operator buttons
- Typography: ប្រើ font size ធំសម្រាប់ display
- Spacing: ដាក់ margin និង padding សមស្រប
- Ripple Effect: ប្រើ
?android:attr/selectableItemBackground
សម្រាប់ buttons - Elevation: ដាក់ CardView ឬ elevation សម្រាប់ display
🐛 Common Issues & Solutions
Issue | Solution |
---|---|
❌ App crashes on equals | Check if display text is empty before parsing |
❌ Division by zero | Add check: if (secondValue != 0) |
❌ Multiple decimal points | Check if display already contains "." |
❌ Long numbers overflow | Use scrollable TextView or limit digits |
🎓 Learning Outcomes
បន្ទាប់ពីបញ្ចប់គម្រោងនេះ អ្នកនឹងយល់ដឹងពី:
- ✅ GridLayout usage
- ✅ Button click listeners
- ✅ String manipulation
- ✅ Mathematical operations
- ✅ Error handling
- ✅ UI/UX design
✅ Congratulations! អ្នកបានបង្កើត Calculator App មួយដែលមានមុខងារពេញលេញ! សាកល្បងបន្ថែម features ផ្សេងទៀតដើម្បីលើកកម្ពស់ skills របស់អ្នក!