Android Studio获取单选框和复选框的值

1.LinearLayout视图页面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/layout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1、请选择你的职业:"
        android:textColor="@color/black" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="教师" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="学生" />
    </RadioGroup>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2、请选择你的业余爱好:"
        android:textColor="@color/black"/>
    <CheckBox
        android:id="@+id/checkbox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="弹钢琴" />
    <CheckBox
        android:id="@+id/checkbox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打网球" />
    <CheckBox
        android:id="@+id/checkbox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="摄影" />

    <Space
        android:layout_width="match_parent"
        android:layout_height="20dp"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="3"
        android:text="显示选择结果:"
        android:textColor="@android:color/black"/>
    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="3"
        android:text=""
        android:textColor="@android:color/black"/>

    <Space
        android:layout_width="match_parent"
        android:layout_height="20dp"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="确定" />
</LinearLayout>

2.入口代码 MainActivity.java

package com.ning.group;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    //声明文本框
    TextView textView2;
    TextView textView4;

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

        //实例化布局管理器
        final LinearLayout mLyaout = (LinearLayout) findViewById(R.id.layout1);
        //获取文本框
        textView2 = (TextView) findViewById(R.id.textView2);
        textView4 = (TextView) findViewById(R.id.textView4);

        //获取复选框按钮
        final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkbox1);
        final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkbox2);
        final CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkbox3);
        //获取单选框按钮
        final RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
        final RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
        //设置状态改变监听器
        checkBox1.setOnCheckedChangeListener(checkBox_listener);
        checkBox2.setOnCheckedChangeListener(checkBox_listener);
        checkBox3.setOnCheckedChangeListener(checkBox_listener);
        radioButton1.setOnCheckedChangeListener(radioButton_listener);
        radioButton2.setOnCheckedChangeListener(radioButton_listener);

        //显示选择结果
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               
                StringBuilder myHobby = new StringBuilder();    //选择结果
                StringBuilder career = new StringBuilder();
                myHobby.append("你的业余爱好是:\n");

                if(checkBox1.isChecked())   //当第1个复选框被选中
                    myHobby.append(checkBox1.getText().toString()+" ");
                if(checkBox2.isChecked())   //当第2个复选框被选中
                    myHobby.append(checkBox2.getText().toString()+" ");
                if(checkBox3.isChecked())   //当第3个复选框被选中
                    myHobby.append(checkBox3.getText().toString()+" ");
                //显示被选中的复选框的值
                if(myHobby.length() == 1){
                    textView2.setText("未选择任何爱好。");
                }else{
                    textView2.setText(myHobby);
                }

                career.append("你的职业是:\n");
                if(radioButton1.isChecked())
                    career.append(radioButton1.getText().toString()+" ");
                if(radioButton2.isChecked())
                    career.append(radioButton2.getText().toString()+" ");
                //显示被选中的单选框的值
                textView4.setText(career);
            }
        });
    }

    //创建一个状态改变监听对象
    private CompoundButton.OnCheckedChangeListener checkBox_listener = new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
            //判断复选框是否被选中
            if(isChecked){
                textView2.setText("你选中的业余爱好是:\n"+buttonView.getText().toString());
            }else{
                textView2.setText("复选框的选择被取消。");
            }
        }
    };
    private CompoundButton.OnCheckedChangeListener radioButton_listener = new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
            if(isChecked){
                textView4.setText("你选中的职业是:\n"+buttonView.getText().toString());
            }
        }
    };
}

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将单选框复选框的数据添加到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、付费专栏及课程。

余额充值