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() + "\n";
                        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);
    }
}
复制代码

 

posted @   嘉禾世兴  阅读(274)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示