单选框

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="一天,张三的店里来了一个顾客,挑了25元钱的货,顾客拿出100元,张三没有零钱找不开,
            就到隔壁李四的店里100元换成零钱,回来给顾客找了75元。过一会,李四来找张三,说刚才的100是假钱,张
            三马上给李四换了一张真钱,问张三赔了多少钱?"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/rg">
                <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content"
                             android:text="A:125"
                             />

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

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

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

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:id="@+id/bt"
            android:text="提交"
            />
</LinearLayout>
package com.example.radiogroup;

import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    RadioGroup rg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rg=findViewById(R.id.rg);
        Button button=findViewById(R.id.bt);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i=0;i<rg.getChildCount();i++)
                {
                    RadioButton radioButton= (RadioButton) rg.getChildAt(i);
                    if(radioButton.isChecked())
                    {
                        if(radioButton.getText().equals("B:100"))
                        {
                            Toast.makeText(MainActivity.this,"回答正确",Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                            AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                            builder.setMessage("回答错误,sb");
                            builder.setPositiveButton("确定",null).show();
                        }

                        break;
                    }
                }
            }
        });
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值