Android: RadioGroup - How to configure the event listener

前端 未结 5 1094
北恋
北恋 2020-11-27 16:30

From my understanding, to determine if a checkbox is \"clicked\" and find if it\'s checked or not, code such as the following can be used:

cb=(CheckBox)findV         


        
相关标签:
5条回答
  • 2020-11-27 16:54

    This is how you get the checked radiobutton:

    // This will get the radiogroup
    RadioGroup rGroup = (RadioGroup)findViewById(r.id.radioGroup1);
    // This will get the radiobutton in the radiogroup that is checked
    RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId());
    

    To use the listener, you do this:

    // This overrides the radiogroup onCheckListener
    rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
            // This will get the radiobutton that has changed in its check state
            RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
            // This puts the value (true/false) into the variable
            boolean isChecked = checkedRadioButton.isChecked();
            // If the radiobutton that has changed in check state is now checked...
            if (isChecked)
            {
                // Changes the textview's text to "Checked: example radiobutton text"
                tv.setText("Checked:" + checkedRadioButton.getText());
            }
        }
    });
    
    0 讨论(0)
  • 2020-11-27 16:55
    //Within the Activity that hosts this layout, the following method handles the click event for both radio buttons:
    
    public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();
    
    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_pirates:
            if (checked)
                // Pirates are the best
            break;
        case R.id.radio_ninjas:                       
            if (checked)
                // Ninjas rule
            break;
    }
    }
    
    0 讨论(0)
  • 2020-11-27 16:56

    If you want to see which radio Button is checked or selected in the radio group then use the following:

    //1. declare the radio group and the radio Button in the java file.
    RadioGroup radiobtn;
    RadioButton radio;
    Button btnClick;
     //the radio is the element of the radiogroup which will assigned when we select the radio button
     //Button to trigger the toast to show which radio button is selected of the radio group
    
    
    //2. now define them in the java file
    radiobtn = findViewById(R.id.radiobtn);
    btnClick = findViewById(R.id.btnClick);
     //we are instructing it to find the elements in the XML file using the id
    
    
    //3. Now Create an on Click listener for the button
    btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int selectedId = radiobtn.getCheckedRadioButtonId();
                 //we are defining the selectId and then we are fetching the id of the checked radio button using the function getCheckedRadioButton()
                radio = findViewById(selectedId);
                 //now the radioButton object we have defined will come into play, we will assign the radio to the radio button of the fetched id of the radio group
                Toast.makeText(MainActivity.this,radio.getText(),Toast.LENGTH_SHORT).show();
                 //we are using toast to display the selected id of the radio button
                 //radio.getText() will fetch the id of the radio Button of the radio group
            }
        });
    
    0 讨论(0)
  • 2020-11-27 17:01

    It should be something like this.

    RadioGroup rb = (RadioGroup) findViewById(R.id.radioGroup1);
    rb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
    
                }
            }
    
        });
    

    Based on the checkedId, you would know which of the radiobutton has been clicked and then use your above code to figure out if its checked or unchecked. This is homework. ;)

    0 讨论(0)
  • 2020-11-27 17:09

    Using Switch is better:

    radGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
          public void onCheckedChanged(RadioGroup arg0, int id) {
            switch (id) {
            case -1:
              Log.v(TAG, "Choices cleared!");
              break;
            case R.id.chRBtn:
              Log.v(TAG, "Chose Chicken");
              break;
            case R.id.fishRBtn:
              Log.v(TAG, "Chose Fish");
              break;
            case R.id.stkRBtn:
              Log.v(TAG, "Chose Steak");
              break;
            default:
              Log.v(TAG, "Huh?");
              break;
            }
          }
        });
    
    0 讨论(0)
提交回复
热议问题