بسم الله الرحمان الرحيم:
السلام عليكم و رجمة الله،نلتقي اليوم أحبتي مع درس جديد مع RadioButton و RadioGroup و تعريف كل واحد منهما.
المكون RadioButton يمكن المستخدم من اختيار عنصر واحد من مجموعة معينة(RadioGroup)،
المكون RadioGroup هو المكون الذي يضم أزرار الإختيار RadioButton ،ويمكننا ترصيصها فيه عموديا أو أفقيا.
نعطي هنا مثالا نطلب فيه من المستخدم أن يحدد نوع نظام تشغيل هاتفه ، هذا هو ملف ال xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is your phone OS?"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroupOS"
android:orientation="vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButtonAndroid"
android:text="Android"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButtonIOS"
android:text="iOS"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButtonBada"
android:text="Bada"/>
</RadioGroup>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
المكون RadioGroup يضم أزرار الإختيار التي أنشأناها وعرفناها،وهو كذلك معرف بمعرِّف سنستعمله برمجيا.الخاصيات ليس هناك خاصية تستدعي شرحها في هذا الملف.ننتقل إلى كود الكلاس
package com.example.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
//
RadioGroup radiogroupOS;
Button buttonOk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radiogroupOS = (RadioGroup) findViewById(R.id.radioGroupOS);
buttonOk = (Button) findViewById(R.id.button);
buttonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int idCheckedRadio = radiogroupOS.getCheckedRadioButtonId();
switch(idCheckedRadio){
case R.id.radioButtonAndroid:
Toast.makeText(getApplicationContext(), "Your phone OS is Android", Toast.LENGTH_SHORT).show();
break;
case R.id.radioButtonIOS:
Toast.makeText(getApplicationContext(), "Your phone OS is iOS", Toast.LENGTH_SHORT).show();
break;
case R.id.radioButtonBada:
Toast.makeText(getApplicationContext(), "Your phone OS is Bada", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
}
قمنا بالتصريح بالمتغير radiogroupOS و قمنا بتعريفه أيضا من خلال الid الموجود لدينا في التصميم،هذا المكون يوفر لنا دالة من خلالها يمكن معرفة id الزر الذي تم اختياره و هي getCheckedRadioButtonId و مُخرج هذه الدالة عدد من نوع int ،يكفي مقارنة هذا العدد بid الأزرار الأخرى،إذن يكفي أن نعرف متغير من نوع int و نعطيه القيمة ()radiogroupOS.getCheckedRadioButtonId ونقارنها ب id الأزرار الثلاثة
وها هي النتيجة:








دمتم في رعاية الله
0 commentaires:
Enregistrer un commentaire