User Interface. RadioButton

În cadrul acestui tutorial vei învăța cum se utilizează RadioButton pentru a prelua input de la utilizator. De asemenea vei afla cum poate fi customizat acest control.

Ngv2E

1. Adăugare control în xml-ul asignat layout-ului

 <RadioGroup
        android:id="@+id/radioGender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_male"
            android:checked="true" />

        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_female" />

    </RadioGroup>

2. Preluare valoare în mod programatic

RadioGroup radioGender = (RadioGroup) findViewById(R.id.radioGender);

//get selected radio button from radioGroup

int selectedId = radioGender.getCheckedRadioButtonId();

//find the radiobutton by returned id

radioGender = (RadioButton) findViewById(selectedId);

Toast.makeText(RadioButtonExample.this, radioSexButton.getText(), Toast.LENGTH_SHORT).show();

3. Evenimente asociate


radioGender.setOnCheckedChangeListener(new OnCheckedChangeListener()

{

public void onCheckedChanged(RadioGroup group, int checkedId)

{

// TODO Auto-generated method stub

if(radiobutton1.isChecked())

{

//your code

}

else

if(radiobutton2.isChecked())

{

//your code

}

}

});

Advertisements
User Interface. RadioButton

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s