废话少说,直接上代码:

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单选使用(实现简单温度转换)的更多相关文章

  1. 【笔记】嵩天.Python语言程序设计.完成两个简单实例(温度转换和绘图)

    [博客导航] [Python相关] 目标 使用PyCharm,完成两个小实例的编写和运行.一个是温度转换,一个是蟒蛇图形绘制. 过程 1.先设置project目录,虽然命名不是很正式,主要不太习惯软件 ...

  2. Android SQLite与ListView的简单使用

    2017-04-25 初写博客有很多地方都有不足,希望各位大神给点建议. 回归主题,这次简单的给大家介绍一下Android SQLite与ListView的简单使用sqlite在上节中有介绍,所以在这 ...

  3. 计算机二级Python学习笔记(一):温度转换

    今天通过一个温度转换的十行代码,理解了一些Python的基本元素. 所谓温度转换,就是摄氏度和华氏度的转换,要求输入摄氏度,可以输出华氏度,反之一样能实现.代码如下: #TempConvert.py ...

  4. Xamarin.Android之引导页的简单制作

    0x01 前言 对于现在大部分的APP,第一次打开刚安装或更新安装的APP都会有几个引导界面,通常这几个引导页是告诉用户 APP有些什么功能或者修改了什么bug.新增了什么功能等等等. 下面就用Xam ...

  5. Android Activity的生命周期简单总结

    Android Activity的生命周期简单总结 这里的内容参考官方的文档,这篇文章的目的不是去总结Activity是如何启动,如何创造,以及暂停和销毁的,而是从实际开发中分析在Activity各个 ...

  6. ytu 2029: C语言实验——温度转换(水题)

    2029: C语言实验——温度转换 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 12  Solved: 10[Submit][Status][Web B ...

  7. Android 设计随便说说之简单实践(合理组合)

    上一篇(Android 设计随便说说之简单实践(模块划分))例举了应用商店设计来说明怎么做模块划分.模块划分主要依赖于第一是业务需求,具体是怎么样的业务.应用商店则包括两个业务,就是向用户展示appl ...

  8. 怎样在Android实现桌面清理内存简单Widget小控件

    怎样在Android实现桌面清理内存简单Widget小控件 我们常常会看到类似于360.金山手机卫士一类的软件会带一个widget小控件,显示在桌面上,上面会显示现有内存大小,然后会带一个按键功能来一 ...

  9. Android BLE与终端通信(一)——Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址

    Android BLE与终端通信(一)--Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址 Hello,工作需要,也必须开始向BLE方向学习了,公司的核心技术就是BLE终端 ...

随机推荐

  1. 前端 / JavaScript 导出PDF的实践

    1.库:jspdf,自己定义一个高宽,如A4的210mm×297mm 2.让设计给背景图(包括:页眉页脚),水印图(背景透明,高宽和你的PDF单页一致)以及很多,能设计给的设计要给,因为在pdf上,排 ...

  2. redis之expire命令详解

      expire是设置redis过期时间的命令,需要注意的点有以下几点 expire设置过期时间的单位是秒,如设置name的过期时间为1000秒 expire name 1000 超过时间后会自动删除 ...

  3. 某邀请赛misc key家用完整版

    目录 题目下载 提示 解题过程 1.提取RGB值 2.找到key 3.循环异或,得到flag 反思 题目下载 题目名:key 提示 提取钥匙中特殊颜色的RGB循环异或KEY值 解题过程 1.提取RGB ...

  4. es 内存占用优化

    对6.3: 修改Elasticsearch中JVM配置文件jvm.options: Dlog4j2.enable.threadlocals=false 注: 本文主要针对ES 2.x. “该给ES分配 ...

  5. 关于Class: ES6 JavaScript的class的静态方法、属性和实例属性。

    1.什么叫做静态方法? 1.1.类相当于实例的原型, 所有在类中定义的方法, 都会被实例继承.如果在一个方法前,加上Static关键字,就表示该方法不会被继承,而是直接通过类来调用,这被称为 “静态方 ...

  6. rpc通讯

    dotnet core各rpc组件的性能测试 一般rpc通讯组件都具有高性特性,因为大部分rpc都是基于二进制和连接复用的特点,相对于HTTP(2.0以下的版本)来说有着很大的性能优势,非常适合服务间 ...

  7. C运算符优先级和结合性

    C中运算符优先级和结合性一览表: 在上表中能总结出一下规律: (1)结合方向只有三个是从右往左,其余都是从左往右: (2)逗号运算符的优先级最低: (3)对于优先级,有一个普遍规律:算术运算符 > ...

  8. [转帖]从 SOA 到微服务,企业分布式应用架构在云原生时代如何重塑?

    从 SOA 到微服务,企业分布式应用架构在云原生时代如何重塑? 2019-10-08 10:26:28 阿里云云栖社区 阅读数 54   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权 ...

  9. DDR3(2):初始化

    调取 DDR3 IP核后,是不能直接进行读写测试的,必须先进行初始化操作,对 IP 核进行校验.本篇采用 Modelsim 软件配合 DDR3 IP核生成的仿真模型,搭建出 IP核的初始化过程. 一. ...

  10. netcore3.0 webapi集成Swagger 5.0,Swagger使用

    Swagger使用 1.描述 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务. 作用: 1.接口的文档在线自动生成. 2.功能测试 本文转自 ...