Domanda

Ho due RadioButtons all'interno di un RadioGroup. Voglio impostare OnClickListener su quelle RadioButtons. A seconda di quale RadioButton viene cliccato, voglio cambiare il testo di un EditText. Come posso raggiungere questo obiettivo?

È stato utile?

Soluzione

mi piacerebbe pensare un modo migliore è quello di utilizzare RadioGroup e impostare l'ascoltatore in questo al cambiamento e aggiornare il View conseguenza (evita di dover 2 o 3 o 4, ecc ascoltatori).

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
        }
    });

Altri suggerimenti

Spero che questo vi aiuterà a ...

RadioButton rb = (RadioButton) findViewById(R.id.yourFirstRadioButton);
rb.setOnClickListener(first_radio_listener);

e

OnClickListener first_radio_listener = new OnClickListener (){
 public void onClick(View v) {
   //Your Implementaions...
 }
};
 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
            RadioButton rb=(RadioButton)findViewById(checkedId);
            textViewChoice.setText("You Selected " + rb.getText());
            //Toast.makeText(getApplicationContext(), rb.getText(), Toast.LENGTH_SHORT).show();
        }
    });

La domanda era su Rilevazione , che pulsante viene cliccato, questo è come è possibile ottenere il pulsante che si fa clic

final RadioGroup radio = (RadioGroup) dialog.findViewById(R.id.radioGroup1);
        radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                View radioButton = radio.findViewById(checkedId);
                int index = radio.indexOfChild(radioButton);

                // Add logic here

                switch (index) {
                case 0: // first button

                    Toast.makeText(getApplicationContext(), "Selected button number " + index, 500).show();
                    break;
                case 1: // secondbutton

                    Toast.makeText(getApplicationContext(), "Selected button number " + index, 500).show();
                    break;
                }
            }
        });

Si potrebbe anche aggiungere ascoltatore dal layout XML:. android:onClick="onRadioButtonClicked" nel tag <RadioButton/>

<RadioButton android:id="@+id/radio_pirates"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/pirates"
    android:onClick="onRadioButtonClicked"/>

sviluppatore Android SDK- Radio per i dettagli.

Nel caso in cui qualcun altro è stato struggeling con la risposta accettata:

Ci sono diversi OnCheckedChangeListener-Interfaces. Ho aggiunto al primo per vedere se un CheckBox è stata cambiata.

import android.widget.CompoundButton.OnCheckedChangeListener;

vs

import android.widget.RadioGroup.OnCheckedChangeListener;

Quando si aggiunge il frammento Ricky ho avuto errori:

Il metodo setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener) nel tipo RadioGroup non è applicabile per gli argomenti (nuova CompoundButton.OnCheckedChangeListener () {})

Può essere fissato con risposta da Ali:

new RadioGroup.OnCheckedChangeListener()

Dal momento che questa domanda non è specifico per Java, vorrei aggiungere come è possibile farlo in Kotlin :

radio_group_id.setOnCheckedChangeListener({ radioGroup, optionId -> {
        when (optionId) {
            R.id.radio_button_1 -> {
                // do something when radio button 1 is selected
            }
            // add more cases here to handle other buttons in the RadioGroup
        }
    }
})

Ecco radio_group_id è la android:id assegnata della questione RadioGroup. Per usarlo in questo modo si avrebbe bisogno di importare kotlinx.android.synthetic.main.your_layout_name.* nel file di Kotlin della vostra attività. Si noti inoltre che nel caso in cui il parametro radioGroup lambda è inutilizzato, esso può essere sostituito con _ (un trattino) dal Kotlin 1.1.

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);     
    radioGroup.setOnClickListener(v -> {
                        // get selected radio button from radioGroup
                        int selectedId = radioGroup.getCheckedRadioButtonId();
                        // find the radiobutton by returned id
                        radioButton =  findViewById(selectedId);
                        String slectedValue=radioButton.getText()          
        });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top