Android studio APP开发 单选框和复选框

单选框和复选框

单选按钮和复选按钮都是普通按钮Button的子类,所以可以使用所有Button的方法和属性。也有自己特有的属性方法

单选框

单选框就是在多个选项中只选择一个。 在Android中,单选按钮用RadioButton表示,而RadioButton类又是Button子类。
通常情况下,RadioButton组件需要与RadioGroup组件一起使用。

设置单选框

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout"
    android:padding="10dp"
    android:gravity="center_horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="性别:"
        android:height="45dp"
        android:textSize="30sp"/>

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:textSize="30sp"
            android:checked="true"/>
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textSize="30sp"/>
    </RadioGroup>

    <TextView
        android:id="@+id/button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        android:textSize="30sp"/>
</LinearLayout>

效果
android:checked 为指定选中状态,即设定一个默认选择的按钮。

获取单选框组中选中项的值

通常在以下两种情况下获取单选框组中选中项的值。

  1. 在改变单选框组的值时获取
  2. 在单击其他按钮时获取

获取单选框组选值的基本步骤如下:

  1. 找到这个单选框组。通过RadioGroup的id
  2. 调用setOnCheckedChangeListener方法,根据checkedId来获取被选中的单选按钮。
  3. 通过getText来获取单选按钮的值
  4. 进行其他操作
在改变单选框组的值时获取

为了能够清晰展示单选框选择的效果,添加了一个TextView来实时显示单选框获取的值。
修改后的布局管理器如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout"
    android:padding="10dp"
    android:gravity="center_horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性别:"
            android:height="45dp"
            android:textSize="30sp"/>

        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男"
                android:textSize="30sp"
                android:checked="true"/>
            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女"
                android:textSize="30sp"/>
        </RadioGroup>

        <TextView
            android:id="@+id/button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提交"
            android:textSize="30sp"/>
    </LinearLayout>

    <TextView
        android:id="@+id/textshow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单选框选中的值"
        android:textSize="24sp"/>
        
    <TextView
        android:id="@+id/textshow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单击提交按钮获得的值"
        android:textSize="24sp"/>
</LinearLayout>

预览
在单选框改变时获取选值需要用到setOnCheckedChangeListener方法。在onCreate中的方法如下:

public class SecondActivity extends AppCompatActivity  {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        RadioGroup sex = (RadioGroup) findViewById(R.id.radioGroup1);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton r = (RadioButton) findViewById(checkedId);
                String str = r.getText().toString();
                TextView textView = (TextView) findViewById(R.id.textshow);
                textView.setText(str);
            }
        });
    }
}

xiaoguo1
xiaoguo2
xiaoguo3

单击其他按钮时获取

要获取单选按钮组中按钮的值,首先要做的就是在被点击的其他按钮的监听事件onClick中获取单选按钮组中选中按钮的id,再获得其值。获得id的过程可以通过for循环遍历所有单选框,根据isChecked()的方法判断按钮是否被选中,当被选中时,通过getText()来获取值。
代码实现如下:

public class SecondActivity extends AppCompatActivity  {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        //在改变单选框组的值时获取
        final RadioGroup sex = (RadioGroup) findViewById(R.id.radioGroup1);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton r = (RadioButton) findViewById(checkedId);
                String str = r.getText().toString();
                TextView textView = (TextView) findViewById(R.id.textshow);
                textView.setText(str);
            }
        });
        
        //单击其他按钮时获取
        Button button = (Button) findViewById(R.id.button01);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1;
                for(int i=0;i<sex.getChildCount();i++){
                    RadioButton r = (RadioButton)sex.getChildAt(i);
                    if(r.isChecked()){
                        str1 = r.getText().toString();
                        TextView textView1 = (TextView) findViewById(R.id.textshow1);
                        textView1.setText(str1);
                        break;
                    }
                }
            }
        });
    }
}


小锅
效果

复选框

复选框可以进行多项选择,每一个复选框都提供了选中和补选中两种状态。在Android 中,复选框用CheckBox()方法来表示,CheckBox()的子类是,所以可以直接使用Button来实现。

设置复选框

在之前的布局管理器中添加如下代码:

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/backLinear">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:text="爱好"
            android:width="100dp"
            android:height="50dp"
            android:gravity="center"/>

        <CheckBox
            android:id="@+id/hobby1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="音乐"
            android:textSize="24sp"/>

        <CheckBox
            android:id="@+id/hobby2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跑步"
            android:textSize="24sp"/>

        <CheckBox
            android:id="@+id/hobby3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="排球"
            android:textSize="24sp"/>

        <CheckBox
            android:id="@+id/hobby4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="看书"
            android:textSize="24sp"/>
        <Button
            android:id="@+id/button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提交"
            android:textSize="24sp"/>
    </LinearLayout>

在Activity中添加如下两种方法。并且在onCreate中调用。
为了不让Activity中的代码看起来很乱,所以写在一个方法里。

 public void checkBox_button(){

        final CheckBox hobby01 = (CheckBox) findViewById(R.id.hobby1);
        final CheckBox hobby02 = (CheckBox) findViewById(R.id.hobby2);
        final CheckBox hobby03 = (CheckBox) findViewById(R.id.hobby3);
        final CheckBox hobby04 = (CheckBox) findViewById(R.id.hobby4);
        Button button = (Button) findViewById(R.id.button02);
        hobby01.setOnCheckedChangeListener(checkBox_listener);
        hobby02.setOnCheckedChangeListener(checkBox_listener);
        hobby03.setOnCheckedChangeListener(checkBox_listener);
        hobby04.setOnCheckedChangeListener(checkBox_listener);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String hob = "";
                if(hobby01.isChecked()){
                    hob += hobby01.getText().toString() + " ";
                }
                if(hobby02.isChecked()){
                    hob += hobby02.getText().toString() + " ";
                }
                if(hobby03.isChecked()){
                    hob += hobby03.getText().toString() + " ";
                }
                if(hobby04.isChecked()){
                    hob += hobby04.getText().toString() + " ";
                }
                Toast.makeText(SecondActivity.this,hob,Toast.LENGTH_SHORT).show();
            }
        });
    }
    private CompoundButton.OnCheckedChangeListener checkBox_listener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                Toast.makeText(SecondActivity.this,buttonView.getText().toString(),Toast.LENGTH_SHORT).show();
            }
        }
    };

在这里插入图片描述
运行效果如图:
效果

要将单选框复选框的数据添加到SQLite数据库中,您需要执行以下步骤: 1. 创建一个SQLite数据库并创建一个表来存储数据。 2. 在您的Android应用程序中,使用单选框复选框来收集用户数据。 3. 将单选框复选框的值转换为字符串,并将其插入到SQLite数据库表中。 以下是一个简单的示例: 创建一个SQLite数据库并创建一个表来存储数据: ```java public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "mydatabase.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_NAME = "mytable"; private static final String COLUMN_ID = "id"; private static final String COLUMN_CHECKBOX = "checkbox"; private static final String COLUMN_RADIOBUTTON = "radiobutton"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String createTable = "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_CHECKBOX + " TEXT, " + COLUMN_RADIOBUTTON + " TEXT)"; db.execSQL(createTable); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } } ``` 在您的Android应用程序中,使用单选框复选框来收集用户数据: ```java public class MainActivity extends AppCompatActivity { private CheckBox checkBox; private RadioButton radioButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkBox = findViewById(R.id.checkbox); radioButton = findViewById(R.id.radiobutton); Button saveButton = findViewById(R.id.save_button); saveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { saveDataToDatabase(); } }); } private void saveDataToDatabase() { String checkboxValue = checkBox.isChecked() ? "true" : "false"; String radioButtonValue = radioButton.isChecked() ? "selected" : "not selected"; DatabaseHelper dbHelper = new DatabaseHelper(this); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(DatabaseHelper.COLUMN_CHECKBOX, checkboxValue); values.put(DatabaseHelper.COLUMN_RADIOBUTTON, radioButtonValue); long newRowId = db.insert(DatabaseHelper.TABLE_NAME, null, values); if (newRowId == -1) { Toast.makeText(this, "Error saving data to database!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Data saved to database!", Toast.LENGTH_SHORT).show(); } } } ``` 将单选框复选框的值转换为字符串,并将其插入到SQLite数据库表中: 在 `saveDataToDatabase()` 方法中,我们将单选框复选框的值转换为字符串,并将这些值插入到SQLite数据库表中。我们使用 `ContentValues` 对象来存储列名和值的映射,然后调用 `insert()` 方法将该行插入到数据库表中。 请注意,我们使用 `isChecked()` 方法来检查复选框是否选中,以及使用 `isChecked()` 方法来检查单选框是否被选中。如果复选框单选框被选中,我们将字符串值设为 "true" 或 "selected",否则设为 "false" 或 "not selected"。 希望这可以帮助您将单选框复选框的数据添加到SQLite数据库中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值