android设计一个页面,选择"是"与"否",并得到他,请给出代码

时间: 2023-07-19 08:04:14 浏览: 43
以下是一个简单的Android页面,包含一个“是”和一个“否”的按钮,当用户点击按钮时,会弹出一个Toast消息显示用户选择的选项。 布局文件代码(activity_main.xml): ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请选择:" /> <RadioGroup android:id="@+id/radio_group" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/yes_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="是" /> <RadioButton android:id="@+id/no_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="否" /> </RadioGroup> <Button android:id="@+id/submit_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交" /> </LinearLayout> ``` Java代码(MainActivity.java): ``` import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private RadioGroup radioGroup; private Button submitButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radioGroup = findViewById(R.id.radio_group); submitButton = findViewById(R.id.submit_button); submitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int selectedId = radioGroup.getCheckedRadioButtonId(); RadioButton radioButton = findViewById(selectedId); String selectedOption = radioButton.getText().toString(); Toast.makeText(MainActivity.this, "您选择了:" + selectedOption, Toast.LENGTH_SHORT).show(); } }); } } ``` 这个页面包含一个TextView来显示“请选择”,一个RadioGroup包含两个RadioButton(“是”和“否”),以及一个提交按钮。在Java代码中,我们使用setOnClickListener()方法为提交按钮添加一个单击监听器。当用户单击按钮时,我们使用getCheckedRadioButtonId()方法找到选中的RadioButton,然后使用getText()方法获取选中的选项,并使用Toast消息将其显示给用户。

相关推荐

最新推荐

recommend-type

解决Android软键盘弹出覆盖h5页面输入框问题

之前我们在使用vue进行 h5 表单录入的过程中,遇到了Android软键盘弹出,覆盖 h5页面 输入框 问题,在此进行回顾并分享给大家,感兴趣的朋友跟随脚本之家小编一起学习吧
recommend-type

Android 中Notification弹出通知实现代码

NotificationManager 是状态栏通知的管理类,负责发通知、清除通知等操作。接下来通过本文给大家介绍Android 中Notification弹出通知实现代码,需要的的朋友参考下吧
recommend-type

Android 通过代码安装 APK的方法详解

主要介绍了Android 通过代码安装 APK的方法详解,本文通过实例代码给大家介绍的非常详细,对大家的学习火锅工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Android开心消消乐代码实例详解

主要介绍了Android开心消消乐代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

Android实现拍照、选择相册图片并裁剪功能

主要为大家详细介绍了Android实现拍照、选择相册图片并裁剪功能的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】增量式PID的simulink仿真实现

# 2.1 Simulink仿真环境简介 Simulink是MATLAB中用于建模、仿真和分析动态系统的图形化环境。它提供了一个直观的用户界面,允许用户使用块和连接线来创建系统模型。Simulink模型由以下元素组成: - **子系统:**将复杂系统分解成更小的、可管理的模块。 - **块:**代表系统中的组件,如传感器、执行器和控制器。 - **连接线:**表示信号在块之间的流动。 Simulink仿真环境提供了广泛的块库,涵盖了各种工程学科,包括控制系统、电子和机械工程。它还支持用户自定义块的创建,以满足特定仿真需求。 # 2. Simulink仿真环境的搭建和建模 ### 2.
recommend-type

训练集和测试集的准确率都99%,但是预测效果不好

即使训练集和测试集的准确率都很高,但是在实际的预测中表现不佳,可能会有以下几个原因: 1. 数据质量问题:模型训练和测试的数据可能存在问题,比如数据标签错误、数据集采样不均衡、数据集中存在异常值等问题,这些问题可能会导致模型在实际预测中表现不佳。 2. 特征工程问题:模型的特征工程可能存在问题,导致模型无法很好地捕捉数据的特征,从而影响预测效果。 3. 模型选择问题:模型的选择可能不合适,比如选择的模型太简单,无法很好地拟合数据,或者选择的模型太复杂,导致过拟合等问题。 4. 超参数调整问题:模型的超参数可能没有调整到最佳状态,需要进行调整。 针对以上可能的原因,可以采取相应的措施进
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。