废话少说,直接上代码:

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. hadoop KerberosUtil 做Kerberos认证

    网上找了一下,自己写了个KerberosUtil工具类,测试过可以用. 注意这个不是 org.apache.hadoop.security.authentication.util.KerberosUt ...

  2. c# 关于反射

    反射的用途大体总结:1.使用Assembly定义和加载程序集,加载在程序集清单中列出模块,以及从程序集中查找类型并创建该类型的实例.CreateInstance2.使用Module了解包含模块的程序集 ...

  3. 如何使用离线存储(localStorage)?

    1.存储数据:localStorage.setItem("属性","属性值") 2.获取本地存储的值:localStorage.getItem("属性 ...

  4. 两次bfs求树的直径的正确性

    结论:离树上任意点\(u\)最远的点一定是这颗树直径的一个端点. 证明: 若点\(u\)在树的直径上,设它与直径两个端点\(x,y\)的距离分别为\(S1\).\(S2\),若距离其最远的点\(v\) ...

  5. 深入js系列-环境

    javascript运行环境 js如果只在引擎中运行,它会严格遵循并且可以预测的,但是js几乎都在宿主环境中运行,浏览器或者Node环境 ECMAScript中的Annex B 介绍了浏览器兼容性问题 ...

  6. tensorflow数据加载、模型训练及预测

    数据集 DNN 依赖于大量的数据.可以收集或生成数据,也可以使用可用的标准数据集.TensorFlow 支持三种主要的读取数据的方法,可以在不同的数据集中使用:本教程中用来训练建立模型的一些数据集介绍 ...

  7. CF852A Digits

    CF852A Digits 隔壁yijian大佬写出了正解.那我就写一个随机化大法吧? 我们先考虑一种错误的贪心,每个数字分成一位,使其分割后数字和最小.虽然这样是错的,但是我们发现错误的概率很小,所 ...

  8. c++隐式转换(implicit conversion)

    1.缘由 最近在使用nlohmann的json,发现有些地方不是特别好用,所以就想自己修改一下(目的是为了增加类似jsoncpp中可以//增加注释的功能),在看源码的时候看到了一个迷惑的地方,就是解析 ...

  9. 使用PhantomJS后台web界面截图

    自动化截web页面的图 一.工具介绍: PhantomJS是一个基于webkit的JavaScript API.它使用QtWebKit作为它核心浏览器的功能,使用webkit来编译解释执行JavaSc ...

  10. 树莓派4B基本配置

    一.系统安装 官网下载好系统解压,使用SD Card Formatter格式化内存卡 # 查看内存卡状态,通过内存卡大小判断是哪个 df -lh # 卸载内存卡 diskutil unmount /d ...