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终端 ...
随机推荐
- Hyperparameters
参数是机器学习算法的关键.它们通常由过去的训练数据中总结得出.在经典的机器学习文献中,我们可以将模型看作假设,将参数视为对特定数据集的量身打造的假设. 模型是否具有固定或可变数量的参数决定了它是否可以 ...
- sublime3插件BracketHighlighter的配置
BracketHighlighter插件能为Sublime Text提供括号,引号这类高亮功能,但安装此插件后,默认没有高亮,只有下划线表示,不是很醒目,需要配置:1.在Sublime Text中用p ...
- 洛谷 p1047 校门外的树 线段树做法
非常easy, 注意一下它是两端开始,也就是说0的位置也有一棵树就好了 我由于太弱了,一道红题交了4,5遍 由于树的砍了就没了,lazy标记最大就是1; 直接贴代码吧 #include<bits ...
- GCD(洛谷 2568)
题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入格式 一个整数N 输出格式 答案 输入输出样例 输入 #1 4 输出 #1 4 说明/提示 ...
- 基于AOP的插件化(扩展)方案
在项目迭代开发中经常会遇到对已有功能的改造需求,尽管我们可能已经预留了扩展点,并且尝试通过接口或扩展类完成此类任务.可是,仍然有很多难以预料的场景无法通过上述方式解决.修改原有代码当然能够做到,但是这 ...
- shell脚本监控httpd服务80端口状态
监控httpd服务端口状态,根据端口判断服务器是否启动,如果没有启动则脚本自动拉起服务,如果服务正在运行则退出脚本程序:如果换成别的服务端口也可以,但是脚本程序需要做调整. #!/bin/bash # ...
- ReentrantReadWriteLock源码分析
代码在后面 读锁 = 共享锁 读锁写锁,公用一个Sync AQS state. 写锁是排他的,看到有人获取锁,他不会去获取,他获取了锁,别人也不会进来获取锁. 写锁的获取跟ReentarntLock一 ...
- [工具]法国神器mimikatz 2.1.1 一键版 & PowerShell版
无需任何参数,运行EXE即可自动读取Windows系统密码 EXE版需要其它功能请使用原版 (参数已写死仅读密码) 结果保存于当前目录mz.log EXE https://github.com/k8g ...
- excel绘制多列 其中一列作为横坐标 ; 数值拟合
excel绘制多列,其中最左列作为横纵坐标: 选中很多列,然后,,点击菜单栏的“插入”->“图标” -->在弹出的“插入图表”对话框中选择“X Y(散点图)”,默认是条形图. 左边的列会 ...
- Redis GEO地理位置信息,查看附近的人
在之前的一篇文章<SpringBoot入门教程(五)Java基于MySQL实现附近的人>,我们介绍了Java基于MySQL实现查找附近的人的功能.今天就来研究研究"查找附近的人& ...