Android收音机按钮

时间:2014-01-10 14:53:27

标签: java android eclipse

如果我的组中具有相同文本和相同属性的单选按钮,如何找到选择了哪个单选按钮?到处都是单选按钮中不同文本的例子

3 个答案:

答案 0 :(得分:3)

使用确定单击哪个单选按钮的RadioGroup,如下所示:

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

答案 1 :(得分:0)

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;

public class RadioButtonExample extends Activity {

            private RadioGroup radioSexGroup;
            private RadioButton radioSexButton;  
            private Button btnDisplay;

            @Override
            public void onCreate(Bundle savedInstanceState) {


                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_radio_button_example);

                addListenerOnButton();

            }

            public void addListenerOnButton() {

                radioSexGroup = (RadioGroup) findViewById(R.id.radioGender);
                btnDisplay = (Button) findViewById(R.id.btnDisplay);

                btnDisplay.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        // get selected radio button from radioGroup
                        int selectedId = radioSexGroup.getCheckedRadioButtonId();

                        // find the radiobutton by returned id
                        radioSexButton = (RadioButton) findViewById(selectedId);

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

                    }

                });

            }
}

答案 2 :(得分:0)

您需要将单选按钮组分组以确定按钮选择。这可以帮助你一个想法: http://developer.android.com/guide/topics/ui/controls/radiobutton.html