شرح التنقل بين الـ Activity في أندرويد استوديوا

بسم الله والحمد لله والصلاة والسلام على رسول الله وبعد.

إليكم شرح بسيط ووافي لكيفية برمجة التنقل بين النشاطات [Activity] في أندرويد ستوديوا تابعونا .

 

هناك ثلاث طرق للحصول على نتيجة مرضية وسلسة بالتنقل إليكم الحل :

  1. الطريقة الأولى عبر استخدام سمة onClick للبوتن. (طريقة مبتدئة)
  2. الطريقة الثانية عبر تعيين OnClickListener() من خلال فصل مجهول. (طريقة متوسطة)
  3. الطريقة الثالثة هي من خلال استخدام تعليمة  switch. (طريقة متقدمة)

الطريقة الأولى عبر استخدام سمة onClick للبوتن. (طريقة مبتدئة)

 

اضافة علامة onClick  ضمن خيارات الزر الموجودة في ملف .xml:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="goToAnActivity"
    android:text="to an activity" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="goToAnotherActivity"
    android:text="to another activity" />

واضف هذا الكود ضمن ملف الجافا java :

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

public void goToAnActivity(View view) {
    Intent Intent = new Intent(this, AnActivity.class);
    startActivity(Intent);
}

public void goToAnotherActivity(View view) {
    Intent Intent = new Intent(this, AnotherActivity.class);
    startActivity(Intent);
}

 

[tie_tooltip text=”Advantage: Easy to make on the fly, modular, and can easily set multiple onClicks to the same intent easily. Disadvantage: Difficult readability when reviewing.” gravity=”s”]ملاحظات بالنسبة لهذه الطريقة :[/tie_tooltip]

 

الطريقة الثانية عبر تعيين OnClickListener() من خلال فصل مجهول. (طريقة متوسطة)

 

هذه الطريقة عندما تقوم بتعيين setOnClickListener() منفصلة لكل button وتجاوز كل onClick() بصفة خاصة.

قم فقط بإدراج الكود التالي في ملف الجافا java :

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

        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent Intent = new Intent(view.getContext(), AnActivity.class);
                view.getContext().startActivity(Intent);}
            });

        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent Intent = new Intent(view.getContext(), AnotherActivity.class);
                view.getContext().startActivity(Intent);}
            });

 

[tie_tooltip text=”Advantage: Easy to make on the fly. Disadvantage: there will be a lot of anonymous classes which will make readability difficult when reviewing.” gravity=”s”]ملاحظات بالنسبة لهذه الطريقة :[/tie_tooltip]

الطريقة الثالثة هي من خلال استخدام تعليمة  switch. (طريقة متقدمة):

 

هذه عندما تستخدم عبارة switch للأزرار الخاصة بك ضمن طريقة onClick() لإدارة جميع أزرار النشاط [Activity] .

 

قم فقط بإدراج الكود التالي في ملف الجافا java :

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

    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.button1:
            Intent intent1 = new Intent(this, AnActivity.class);
            startActivity(intent1);
            break;
        case R.id.button2:
            Intent intent2 = new Intent(this, AnotherActivity.class);
            startActivity(intent2);
            break;
        default:
            break;
    }

[tie_tooltip text=”Advantage: Easy button management because all button intents are registered in a single onClick() method” gravity=”n”]ملاحظات بالنسبة لهذه الطريقة :[/tie_tooltip]

ميزة : إدارة سهلة للأزرار لأن كل نوايا الأزرار مسجلة بطريقة onClick() واحدة

startActivity(new Intent(MainActivity.this, SecondActivity.class)) طريقة مبسطة

 

 

 

 

 

enter image description here

أيضا إليكم الكود الخاص بلغة كوتلين :

testActivityBtn1.setOnClickListener{
      val intent = Intent(applicationContext,MainActivity::class.Java)
      startActivity(intent)
}

أرجوا أن نكون قد وفقنا في الشرح وقدمنا لكم أمثلة مبسطة ومفيدة :

لمزيد من الشرح باللغة الإنجليزية :

ToNewActivityButtons

شكرا لكم على المتابعة .

مدونة فور يو .

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button