کلیدستان

نسخه‌ی کامل: ردیو باتن (RadioButton) (برنامه نویسی اندروید)
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
سلام دوستان چرا بیرون از ردیو باتن مقدار رو نمیشه گرفت؟
مقدار های گزینه ها رو؟؟؟
فقط داخل خودش میشناسه نه خارجش
چیکار میشه کرد؟
سلام

Radiobutton ها درون container مخصوص خودشون که RadioGroup هستش قرار دارن
برای دسترسی مستقیم به RadioButton باید ابتدا به RadioGroup دسترسی پیدا کنید
سپس میتونید به Child های درون اون که همون Radiobutton ها هستن دسترسی داشته باشید
مرسی دوست عزیز مشکل منم در دسترسی هست دیگه چطوری به خود ردیو باتن و بعدش به child اون دسترسی پیدا کنیم؟
مشکل منم همینه
میشه به یه کد ساده نشون بدید؟
ممنون
این هم یک مثال در رابطه از استفاده از RadioButton و RadioGroup

اگه باز هم نیاز به دسترسی و کنترل بیشتری بر روی RadioGroup یا Radiobutton داشتید اعلام کنید چه نوع دسترسی نیاز دارید یا دقبقا میخواید چیکار کنید.

کد:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
               xmlns:tools="http://schemas.android.com/tools"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:paddingBottom="@dimen/activity_vertical_margin"
               android:paddingLeft="@dimen/activity_horizontal_margin"
               android:paddingRight="@dimen/activity_horizontal_margin"
               android:paddingTop="@dimen/activity_vertical_margin"
               tools:context="com.example.mahdi.myapplication.MainActivityFragment"
               tools:showIn="@layout/activity_main">


   <RadioGroup
       android:layout_marginTop="25dp"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentStart="true"
       android:background="#CCBBEE"
       android:id="@+id/rg">

       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="RadioButton 1"
           android:id="@+id/rb1"
           android:checked="false"/>

       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="RadioButton 2"
           android:id="@+id/rb2"
           android:checked="false"/>

       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="RadioButton 3"
           android:checked="false"
           android:id="@+id/rb3"/>
   </RadioGroup>
</RelativeLayout>


کد:
RadioGroup rg = (RadioGroup) findViewById(R.id.rg);

       rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(RadioGroup group, int checkedId) {
               switch (checkedId) {
                   case R.id.rb1 :
                       Toast.makeText(MainActivity.this,"RadioButton 1 is Checked", Toast.LENGTH_SHORT).show();
                       break;
                   case R.id.rb2 :
                       Toast.makeText(MainActivity.this,"RadioButton 2 is Checked", Toast.LENGTH_SHORT).show();
                       break;
                   case R.id.rb3 :
                       Toast.makeText(MainActivity.this,"RadioButton 3 is Checked", Toast.LENGTH_SHORT).show();
                       break;
               }

           }
       });
       RadioButton rb1 = (RadioButton) findViewById(R.id.rb2);

       rb1.setChecked(true);