radioButton

布局:

<LinearLayout 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:orientation="vertical"
    tools:context="com.example.day03_radiobutton.MainActivity" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="性别" />

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

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />
    </RadioGroup>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="爱好" />

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

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="网球" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="篮球"/>

        <RadioButton
            android:id="@+id/button_fight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="足球" />
    </RadioGroup>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确认" />

</LinearLayout>

代码:

public class MainActivity extends Activity {
    private RadioGroup group_sex, group_favor;
    private RadioButton button_fight;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        group_sex = (RadioGroup) findViewById(R.id.group_sex);
        group_favor = (RadioGroup) findViewById(R.id.group_favor);
        button_fight = (RadioButton) findViewById(R.id.button_fight);
        button_fight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    Toast.makeText(MainActivity.this, 
                            "你……确定?", 
                            Toast.LENGTH_LONG).show();
                }
            }
        });
        
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                String str = "";
                // 读取radioGroup的状态
                // 提取radioGroup的子控件
                for(int i = 0; i < group_sex.getChildCount(); i++) {
                    RadioButton button = (RadioButton) group_sex.getChildAt(i);
                    if(button.isChecked()) {
                        str += "性别:" + button.getText() + "
";
                        break;
                    }
                }
                for(int i = 0; i < group_favor.getChildCount(); i++) {
                    RadioButton button = (RadioButton) group_favor.getChildAt(i);
                    if(button.isChecked()) {
                        str += "爱好:" + button.getText();
                        break;
                    }
                }
                Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

 

原文地址:https://www.cnblogs.com/anni-qianqian/p/5430523.html