انجمن سایت کلیدستان


رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
ردیو باتن (RadioButton) (برنامه نویسی اندروید)

ردیو باتن (RadioButton) (برنامه نویسی اندروید)

#4
این هم یک مثال در رابطه از استفاده از 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);
پاسخ
 سپاس شده توسط ahmad97 ، شماره مجازی امارات ، تلگرام ضد فیلتر 2023


پیام‌های این موضوع
RE: ردیو باتن - توسط mahdi10539 - ۱۳۹۴/۱۱/۱۷, ۰۵:۴۴ ب.ظ
RE: ردیو باتن (RadioButton) (برنامه نویسی اندروید) - توسط mahdi10539 - ۱۳۹۴/۱۱/۱۸, ۱۱:۱۹ ب.ظ

پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان