Android-----RadioButton单选使用(实现简单温度转换)
废话少说,直接上代码:
xml布局文件代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.hs.example.exampleapplication.RadioButtonMain"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"> <TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="输入温度:"/> <RadioGroup
android:id="@+id/unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> <RadioButton
android:id="@+id/unitF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="华氏"/> <RadioButton
android:id="@+id/unitC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="摄氏"/> <RadioButton
android:id="@+id/unitK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="绝对"/> </RadioGroup> </LinearLayout> <EditText
android:id="@+id/value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength=""
android:singleLine="true"
android:inputType="numberDecimal"/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:id="@+id/degF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=""
android:gravity="center"
android:textSize="45sp"
android:maxLines=""
android:text="@string/charF"/> <TextView
android:id="@+id/degC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=""
android:gravity="center"
android:textSize="45sp"
android:maxLines=""
android:text="@string/charC"/> <TextView
android:id="@+id/degK"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=""
android:gravity="center"
android:textSize="45sp"
android:maxLines=""
android:text="K"/> </LinearLayout> </LinearLayout>
Java代码:
public class RadioButtonMain extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,TextWatcher{
RadioGroup unit;
EditText value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radiobutton_main);
unit = this.findViewById(R.id.unit);
unit.setOnCheckedChangeListener(this);
value = this.findViewById(R.id.value);
value.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
calc();
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
calc();
}
protected void calc(){
TextView degF = this.findViewById(R.id.degF);
TextView degC = this.findViewById(R.id.degC);
TextView degK = this.findViewById(R.id.degK);
double f,c,k;
if(unit.getCheckedRadioButtonId() == R.id.unitF){
f = Double.parseDouble(value.getText().toString());
c = (f-)*/; //华氏度转摄氏度
k = c + 273.15; //绝对 = 摄氏度 + 273.15
}else if(unit.getCheckedRadioButtonId() == R.id.unitC){
c = Double.parseDouble(value.getText().toString());
f = c*/+; //摄氏度转华氏度
k = c + 273.15;
}else{
k = Double.parseDouble(value.getText().toString());
c = k - 273.15;
f = c*/+;
}
degC.setText(String.format("%.1f",c)+getResources().getString(R.string.charC));
degF.setText(String.format("%.1f",f)+getResources().getString(R.string.charF));
degK.setText(String.format("%.2f",k)+"K");
}
}
运行效果:



该例子还存在一些问题,这里就不指出了,运行一遍自会发现问题所在,最好能够自行解决。
Android-----RadioButton单选使用(实现简单温度转换)的更多相关文章
- 【笔记】嵩天.Python语言程序设计.完成两个简单实例(温度转换和绘图)
[博客导航] [Python相关] 目标 使用PyCharm,完成两个小实例的编写和运行.一个是温度转换,一个是蟒蛇图形绘制. 过程 1.先设置project目录,虽然命名不是很正式,主要不太习惯软件 ...
- Android SQLite与ListView的简单使用
2017-04-25 初写博客有很多地方都有不足,希望各位大神给点建议. 回归主题,这次简单的给大家介绍一下Android SQLite与ListView的简单使用sqlite在上节中有介绍,所以在这 ...
- 计算机二级Python学习笔记(一):温度转换
今天通过一个温度转换的十行代码,理解了一些Python的基本元素. 所谓温度转换,就是摄氏度和华氏度的转换,要求输入摄氏度,可以输出华氏度,反之一样能实现.代码如下: #TempConvert.py ...
- Xamarin.Android之引导页的简单制作
0x01 前言 对于现在大部分的APP,第一次打开刚安装或更新安装的APP都会有几个引导界面,通常这几个引导页是告诉用户 APP有些什么功能或者修改了什么bug.新增了什么功能等等等. 下面就用Xam ...
- Android Activity的生命周期简单总结
Android Activity的生命周期简单总结 这里的内容参考官方的文档,这篇文章的目的不是去总结Activity是如何启动,如何创造,以及暂停和销毁的,而是从实际开发中分析在Activity各个 ...
- ytu 2029: C语言实验——温度转换(水题)
2029: C语言实验——温度转换 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 12 Solved: 10[Submit][Status][Web B ...
- Android 设计随便说说之简单实践(合理组合)
上一篇(Android 设计随便说说之简单实践(模块划分))例举了应用商店设计来说明怎么做模块划分.模块划分主要依赖于第一是业务需求,具体是怎么样的业务.应用商店则包括两个业务,就是向用户展示appl ...
- 怎样在Android实现桌面清理内存简单Widget小控件
怎样在Android实现桌面清理内存简单Widget小控件 我们常常会看到类似于360.金山手机卫士一类的软件会带一个widget小控件,显示在桌面上,上面会显示现有内存大小,然后会带一个按键功能来一 ...
- Android BLE与终端通信(一)——Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址
Android BLE与终端通信(一)--Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址 Hello,工作需要,也必须开始向BLE方向学习了,公司的核心技术就是BLE终端 ...
随机推荐
- springmvc controller层接收List类型的参数
Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是applica ...
- Pandas | 13 索引和选择数据
Pandas现在支持三种类型的多轴索引; 编号 索引 描述 1 .loc() 基于标签 2 .iloc() 基于整数 3 .ix() 基于标签和整数 .loc() Pandas提供了各种方法来完成基于 ...
- pcm音频的格式类型
[文章内容属于多方转载内容] PCM Parameters PCM audio is coded using a combination of various parameters. Resoluti ...
- 三天精通Vue教程
在这里更新作为后端工程师想要快速掌握Vue需要看的重点内容,三天精通教程,加油! 学前摘要 ES6的常用语法 Vue的常用语法
- sklearn保存模型的两种方式
sklearn 中模型保存的两种方法 一. sklearn中提供了高效的模型持久化模块joblib,将模型保存至硬盘. from sklearn.externals import joblib # ...
- 【视频开发】【计算机视觉】相机标定(Camera calibration)《二》
简介 摄像机标定(Camera calibration)简单来说是从世界坐标系换到图像坐标系的过程,也就是求最终的投影矩阵 P 的过程,下面相关的部分主要参考UIUC的计算机视觉的课件(网址Sprin ...
- 【原创】在windows下使用xampp搭建phpcms v9
我的操作环境: 操作系统:windows 7 64 位操作系统(有点古老,哈哈) 1.下载php环境和phpcmsv9源代码:phpcms v9 的源码:phpcms_v9.5.10_UT ...
- React的状态管理工具
Mobx-React : 当前最适合React的状态管理工具 MobX 简单.可扩展的状态管理 MobX 是由 Mendix.Coinbase.Facebook 开源和众多个人赞助商 ...
- 【java】获取客户端访问的公网ip和归属地
import com.alibaba.druid.support.json.JSONUtils; import org.thymeleaf.util.StringUtils; import javax ...
- 宏offsetof分析
1.前言 在C语言的结构体中,由于字节对齐的问题,所以成员的地址并不能直接根据数据类型的大小进行计算,使用宏offsetof可以获得结构体成员相对于结构体首地址的字节偏移量. 2.offsetof宏实 ...