كل المقررات

الرئيسية / مقرر تطبيقات الموبايل / الفصل 3 الجزء 2

الفصل 3 (الجزء 2): Methods, Log and Toast

A method does one job, and the user sees a Toast

على طول: نكتب دالة بنفسنا، ونربطها بالزرار، ونطبع ونعرض رسائل.

فيديو المحاضرة

فيديو المحاضرة

بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.

رسوم متحركة

من Java للشاشة

أمثلة كود

أمثلة قصيرة وبسيطة

كل مثال وراه زرار «شغّل خطوة بخطوة»: بيعلّم على السطر اللي بيتنفّذ ويطبع الناتج زي ما بيتنفّذ بالظبط. ده شرح تنفيذ الكود سطر بسطر، مش مترجم حقيقي جوه الصفحة.

المثال ١: دالة بترجّع قيمة

int add(int a, int b) {    return a + b;}int total = add(3, 4);Log.d("MainActivity", "total = " + total);

    

المثال ٢: مستويات الـ Log

Log.d("MainActivity", "debug: onCreate started");Log.i("MainActivity", "info: layout loaded");Log.w("MainActivity", "warning: no name yet");Log.e("MainActivity", "error: file not found");

    

المثال ٣: الزرار بينادي دالة

Button top = findViewById(R.id.buttonTop);top.setOnClickListener(v -> topClick()); void topClick() {    Log.d("MainActivity", "top clicked");    Toast.makeText(this, "Top", Toast.LENGTH_SHORT).show();}

    

اختبار قصير

اختبر فهمك

1. void in a method signature means:

2. Which line shows a small pop-up to the user?

3. Logcat is used to:

4. A NullPointerException on a view usually means:

5. setOnClickListener is used to:

تمارين

جرّب بإيدك

التمرين 1

اكتب دالة multiply بترجّع حاصل ضرب رقمين، واطبع ناتج 6 في 7.

Write a multiply method returning the product of two numbers, and log 6 times 7.

Starter code:

// TODO: multiply method and one Log line

التمرين 2

اكتب دالة bottomClick بتطبع سطر في الـ Log وتظهر Toast، واربطها بالزرار buttonBottom.

Write a bottomClick method that logs a line and shows a Toast, and wire it to buttonBottom.

Starter code:

Button bottom = findViewById(R.id.buttonBottom);// TODO: set the click listener// TODO: write bottomClick

مشروع صغير في آخر المحاضرة

آلة حاسبة صغيرة في الـ Log

الداتا: calc: our own two numbers (6 and 7), no user data.

License / source: Our own code; Log reference by Google (link only). Reference page

الخطوات

  1. اكتب دالتين: add و multiply، كل واحدة بتاخد رقمين وبترجّع الناتج.
  2. في onCreate نادي الدالتين على 6 و 7.
  3. اطبع الناتجين في الـ Log بـ tag اسمه Calc.
  4. شغّل التطبيق وبص على Logcat. المتوقع تحت.

Starter code:

int add(int a, int b) {    // TODO: return the sum    return 0;}int multiply(int a, int b) {    // TODO: return the product    return 0;}// TODO: log add(6, 7) and multiply(6, 7) with tag "Calc"

مشروع نهاية الفصل

ملف المشروع العملي للفصل 3

ملف PDF فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.

افتح ملف المشروع (PDF)

Credits: the topic order follows the course notes "Mobile Application Development 1" by Dr. Muhammad Kashif Saeed (Applied College, Muhayil, King Khalid University), shared by the course owner. The explanations, diagrams, data and code in this lecture are our own; we copy nothing from the notes. Extra reference: Android developer documentation: layouts, views and the activity life cycle (developer.android.com).