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终端 ...
随机推荐
- JS中把其他类型转换成字符串的三种方法
1.toString()方法 toString()方法返回的是相应值的字符串表现 数值.布尔值.对象和字符串值都有toString()方法,但是null和undefined值没有这个方法 例子: va ...
- idea启动java项目,使用调试会占用更多内存
idea启动java项目,使用调试会占用更多内存
- django ORM创建
简短的例子 from django.db import models class Person(models.Model): first_name = models.CharField(max_len ...
- IIS 加载字体
原文:https://blog.csdn.net/prospertu/article/details/72852500 <system.webServer> <staticConte ...
- code 1716
# import_company def test_import_company(self): headers=self.headers headers["Content-Type" ...
- [Noip2018]填数游戏
传送门 Description 耳熟能详,就不多说了 Solution 对于一个不会推式子的蒟蒻,如何在考场优雅地通过此题 手玩样例,发现对于 \(n=1\) , \(ans=2^m\) .对于 \( ...
- Linux】目录文件权限的查看和修改【转】
转载自:http://zhaoyuqiang.blog.51cto.com/6328846/1214718 ============================================== ...
- 【Gamma阶段】第五次Scrum Meeting
[Gamma阶段]第五次Scrum Meeting 每日任务内容 今日工作任务 明日待完成任务 配合前端调整评论页面的样式 课程列表页针对移动端进行调整 戴荣 Gamma阶段后测试点样例编写 移除部分 ...
- mongo 操作
1.链接mongo /path_to_mongo/bin/mongo MongoDB shell version: connecting to: test > use logs switched ...
- IO流——字节流
文件输出流 FileOutputStream:文件输出流是用于将数据写入 File,每次运行,都会覆盖之前文件中的数据 FileOutputStream(File file):创建一个向指定 File ...