模块二Android Studio 单选框与复选框的使用 RadioButton CheckBox

运行结果

activity_main.xml

MainActivity.java


运行结果

activity_main.xml

使用单选按钮时,要把单选按钮RadioButton放入RadioGroup里,如图

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

    <EditText
        android:id="@+id/editTextText"
        android:layout_width="376dp"
        android:layout_height="47dp"
        android:ems="10"
        android:hint="输入姓名"

        android:inputType="text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.086"
        tools:layout_editor_absoluteX="1dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="73dp"
        android:layout_height="21dp"
        android:background="#3CAEEA00"
        android:text="专业技能表"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.022" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="212dp"
        android:layout_height="30dp"
        android:text="1.所在专业"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.073"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.169" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="409dp"
        android:layout_height="208dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.26999998">

        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="356dp"
            android:layout_height="wrap_content"
            android:text="计算机科学学与技术" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="软件工程" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="网络工程" />

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="人工智能" />

    </RadioGroup>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="212dp"
        android:layout_height="30dp"
        android:text="2.专业技能"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.07"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499" />

    <CheckBox
        android:id="@+id/tcp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TCP/IP协议"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.052"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.576" />

    <CheckBox
        android:id="@+id/spring"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Spring架构"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.53"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.576" />

    <CheckBox
        android:id="@+id/jdbc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="JDBC编程"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.93"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.576" />

    <CheckBox
        android:id="@+id/linux"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Linux基本使用"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.927"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.683" />

    <CheckBox
        android:id="@+id/socket"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Socket编程"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.051"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.683" />

    <CheckBox
        android:id="@+id/jvm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="JVM调优"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.509"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.683" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private CheckBox tcp_checkBox;
    private CheckBox spring_checkBox;
    private CheckBox jdbc_checkBox;
    private CheckBox linux_checkBox;
    private CheckBox socket_checkBox;
    private CheckBox jvm_checkBox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup=(RadioGroup) findViewById(R.id.radioGroup);
        tcp_checkBox=(CheckBox)findViewById(R.id.tcp);
        spring_checkBox=(CheckBox)findViewById(R.id.spring);
        jdbc_checkBox=(CheckBox)findViewById(R.id.jdbc);
        linux_checkBox=(CheckBox)findViewById(R.id.linux);
        socket_checkBox=(CheckBox)findViewById(R.id.socket);
        jvm_checkBox=(CheckBox)findViewById(R.id.jvm);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton=(RadioButton) findViewById(i);
                Toast.makeText(getApplicationContext(),"已选择"+radioButton.getText(),Toast.LENGTH_LONG).show();
            }
        });
        tcp_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked())
                    Toast.makeText(getApplicationContext(),"TCP/IP协议",Toast.LENGTH_LONG).show();
            }
        });
        spring_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked())
                    Toast.makeText(getApplicationContext(),"Spring架构",Toast.LENGTH_LONG).show();
            }
        });
        jdbc_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked())
                    Toast.makeText(getApplicationContext(),"JDBC编程",Toast.LENGTH_LONG).show();
            }
        });
        linux_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked())
                    Toast.makeText(getApplicationContext(),"Linux基本使用",Toast.LENGTH_LONG).show();
            }
        });
        socket_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked())
                    Toast.makeText(getApplicationContext(),"Socket编程",Toast.LENGTH_LONG).show();
            }
        });
        jvm_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked())
                    Toast.makeText(getApplicationContext(),"JVM调优",Toast.LENGTH_LONG).show();
            }
        });
    }
}

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个含有单选框复选框Android Studio注册样式的代码示例: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/editTextEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email" /> <EditText android:id="@+id/editTextPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Gender" /> <RadioGroup android:id="@+id/radioGroupGender" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radioButtonMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Male" /> <RadioButton android:id="@+id/radioButtonFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Female" /> </RadioGroup> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hobbies" /> <CheckBox android:id="@+id/checkBoxReading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Reading" /> <CheckBox android:id="@+id/checkBoxTravelling" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Travelling" /> <Button android:id="@+id/buttonRegister" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Register" /> </LinearLayout> ``` 在这个布局中,我们使用了一个EditText来输入邮箱和密码,一个RadioGroup来选择性别(男/女),以及两个CheckBox来选择爱好(阅读/旅行)。最后,我们使用了一个Button来注册。你可以根据需要进行修改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值