© Khmer Angkor Academy - sophearithput168

Views & Widgets

Views និង Widgets

Views គឺជា UI elements ដែលអ្នកមើលឃើញ និង interact នៅលើ screen។ គ្រប់យ៉ាងដែលអ្នកឃើញនៅលើ Android screen គឺជា View!

🏗️ View Class Hierarchy

គ្រប់ UI components ទាំងអស់ inherit ពី base View class:

                    Object
                      ↓
                    View  ──► Base class សម្រាប់ UI components ទាំងអស់
                      ↓
        ┌─────────────┴──────────────┐
        ↓                            ↓
    TextView                    ViewGroup
        ↓                            ↓
    ┌───┴───┐              ┌─────────┴─────────┐
    ↓       ↓              ↓                   ↓
 Button  EditText    LinearLayout      RelativeLayout
                           ↓                   ↓
                    ConstraintLayout    FrameLayout

🎯 View vs ViewGroup

Class ពិពណ៌នា ឧទាហរណ៍
View Individual UI component (មិនមាន children) TextView, Button, ImageView, EditText
ViewGroup Container ដែលមាន child views (layout) LinearLayout, RelativeLayout, ConstraintLayout

📱 Common Views

View ពិពណ៌នា ប្រើសម្រាប់
📝 TextView បង្ហាញអត្ថបទ (read-only) Labels, titles, descriptions
✏️ EditText Input field សម្រាប់បញ្ចូលអត្ថបទ Forms, search boxes, text input
🔘 Button ប៊ូតុង clickable Submit, cancel, actions
🖼️ ImageView បង្ហាញរូបភាព Photos, icons, graphics
☑️ CheckBox ជម្រើស check/uncheck (multiple) Agree terms, select options
🔘 RadioButton ជម្រើសមួយក្នុងចំណោមជាច្រើន (single) Gender, payment method
🎚️ SeekBar Slider សម្រាប់ជ្រើសរើសតម្លៃ Volume, brightness, progress
🔄 ProgressBar បង្ហាញ loading status Loading data, file upload
🔽 Spinner Dropdown list Select country, category
🔘 Switch Toggle ON/OFF Enable/disable features

🎨 View Properties (Common Attributes)

Property ពិពណ៌នា ឧទាហរណ៍
android:id ID តែមួយគត់ @+id/textView
android:layout_width ទទឹង view match_parent, wrap_content, 200dp
android:layout_height កម្ពស់ view match_parent, wrap_content, 50dp
android:background ពណ៌ផ្ទៃខាងក្រោយ #FF5733, @color/primary
android:padding Padding ខាងក្នុង 16dp
android:margin Margin ខាងក្រៅ 8dp
android:visibility ការបង្ហាញ view visible, invisible, gone

✏️ EditText Input Types

EditText supports ច្រើន input types សម្រាប់ keyboard layout ផ្សេងៗ:

Input Type ពិពណ៌នា Keyboard
text អត្ថបទធម្មតា Standard keyboard
textPassword លេខសម្ងាត់ (hidden) Standard keyboard with dots
number លេខតែប៉ុណ្ណោះ Numeric keyboard
phone លេខទូរស័ព្ទ Phone pad
textEmailAddress អ៊ីមែល Email keyboard with @
textCapWords អក្សរធំដើមពាក្យ Auto-capitalize words
textMultiLine ច្រើនបន្ទាត់ Multi-line input
date កាលបរិច្ឆេទ Date picker

👆 Event Handling

Views អាច respond ទៅ user interactions តាមរយៈ event listeners:

Listener Event ពេលណាកើតឡើង
OnClickListener Click User ចុចលើ view
OnLongClickListener Long Click User ចុចរយៈពេលយូរ
OnTouchListener Touch User touch screen (down, move, up)
OnFocusChangeListener Focus Changed View ទទួល/បាត់ focus
TextWatcher Text Changed អត្ថបទផ្លាស់ប្តូរ (EditText)

🎨 View States

Views អាចមាន states ផ្សេងៗ:

┌──────────────────┐
│  View States     │
├──────────────────┤
│ ✅ enabled       │  ──► View អាចប្រើបាន
│ ❌ disabled      │  ──► View មិនអាចប្រើបាន
│ 🎯 focused       │  ──► View មាន focus (ready for input)
│ 👆 pressed       │  ──► User កំពុងចុច
│ ☑️ selected      │  ──► View ត្រូវបានជ្រើសរើស
│ ☑️ checked       │  ──► CheckBox/RadioButton checked
└──────────────────┘

💡 StateListDrawable: អ្នកអាចបង្កើត drawable ផ្សេងៗសម្រាប់ states ផ្សេងៗ (pressed, focused, enabled)

Java Code
Click "Run" to execute the Java code