Android攻略--单位转化器UC--Units Converter(学习笔记)
1创建工程
注意这个名称的命名:
3. UC结构及相关代码
UC.java 用于执行单位换算的Activity
// UC.javapackage com.apress.uc;import android.app.Activity;import android.os.Bundle;import android.text.Editable; import android.text.TextWatcher;import android.view.View;import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner;public class UC extends Activity {private int position = 0;private double[] multipliers ={0.0015625, // Acres to square miles101325.0, // Atmospheres to Pascals100000.0, // Bars to Pascals0, // Degrees Celsius to Degrees Fahrenheit (placeholder)0, // Degrees Fahrenheit to Degrees Celsius (placeholder)0.00001, // Dynes to Newtons0.3048, // Feet/Second to Metres/Second0.0284130625, // Fluid Ounces (UK) to Litres0.0295735295625, // Fluid Ounces (US) to Litres746.0, // Horsepower (electric) to Watts735.499, // Horsepower (metric) to Watts1/1016.0469088, // Kilograms to Tons (UK or long)1/907.18474, // Kilograms to Tons (US or short)1/0.0284130625, // Litres to Fluid Ounces (UK)1/0.0295735295625, // Litres to Fluid Ounces (US)331.5, // Mach Number to Metres/Second1/0.3048, // Metres/Second to Feet/Second1/331.5, // Metres/Second to Mach Number0.833, // Miles/Gallon (UK) to Miles/Gallon (US)1/0.833, // Miles/Gallon (US) to Miles/Gallon (UK)100000.0, // Newtons to Dynes1/101325.0, // Pascals to Atmospheres0.00001, // Pascals to Bars640.0, // Square Miles to Acres1016.0469088, // Tons (UK or long) to Kilograms907.18474, // Tons (US or short) to Kilograms1/746.0, // Watts to Horsepower (electic)1/735.499 // Watts to Horsepower (metric) };@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);final EditText etUnits = (EditText) findViewById(R.id.units);final Spinner spnConversions = (Spinner) findViewById(R.id.conversions);ArrayAdapter<CharSequence> aa;aa = ArrayAdapter.createFromResource(this, R.array.conversions,android.R.layout.simple_spinner_item);aa.setDropDownViewResource(android.R.layout.simple_spinner_item);spnConversions.setAdapter(aa);AdapterView.OnItemSelectedListener oisl;oisl = new AdapterView.OnItemSelectedListener(){@Overridepublic void onItemSelected(AdapterView<?> parent, View view,int position, long id){UC.this.position = position;}@Overridepublic void onNothingSelected(AdapterView<?> parent){System.out.println("nothing");}};spnConversions.setOnItemSelectedListener(oisl);final Button btnClear = (Button) findViewById(R.id.clear);AdapterView.OnClickListener ocl;ocl = new AdapterView.OnClickListener(){@Overridepublic void onClick(View v){etUnits.setText("");}};btnClear.setOnClickListener(ocl);btnClear.setEnabled(false);final Button btnConvert = (Button) findViewById(R.id.convert);ocl = new AdapterView.OnClickListener(){@Overridepublic void onClick(View v){String text = etUnits.getText().toString();double input = Double.parseDouble(text);double result = 0;if (position == 3)result = input*9.0/5.0+32; // Celsius to Fahrenheitelseif (position == 4)result = (input-32)*5.0/9.0; // Fahrenheit to Celsiuselseresult = input*multipliers[position];etUnits.setText(""+result);}};btnConvert.setOnClickListener(ocl);btnConvert.setEnabled(false);Button btnClose = (Button) findViewById(R.id.close);ocl = new AdapterView.OnClickListener(){@Overridepublic void onClick(View v){finish();}};btnClose.setOnClickListener(ocl);TextWatcher tw;tw = new TextWatcher(){@Overridepublic void afterTextChanged(Editable s){}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after){}@Overridepublic void onTextChanged(CharSequence s, int start, int before,int count){if (etUnits.getText().length() == 0){btnClear.setEnabled(false);btnConvert.setEnabled(false);}else{btnClear.setEnabled(true);btnConvert.setEnabled(true);}}};etUnits.addTextChangedListener(tw);} }
main.xml 用于保存小部件和布局信息的main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_vertical"android:background="@drawable/gradientbg"android:padding="5dip"><LinearLayout android:layout_width="fill_parent"android:layout_height="wrap_content"><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="10dip"android:text="@string/units"android:textColor="#000000"android:textSize="15sp"android:textStyle="bold"/><EditText android:id="@+id/units"android:layout_width="fill_parent"android:layout_height="wrap_content"android:hint="type a number"android:inputType="numberDecimal|numberSigned"android:maxLines="1"/></LinearLayout><Spinner android:id="@+id/conversions"android:layout_width="fill_parent"android:layout_height="wrap_content"android:prompt="@string/prompt"/><LinearLayout android:layout_width="fill_parent"android:layout_height="wrap_content"><Button android:id="@+id/clear"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="@string/clear"/><Button android:id="@+id/convert"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="@string/convert"/><Button android:id="@+id/close"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="@string/close"/></LinearLayout> </LinearLayout>
strings.xml 保存了应用中的字符串
<?xml version="1.0" encoding="utf-8"?> <resources><string name="app_name">Units Converter</string><string name="clear">Clear</string><string name="close">Close</string><string name="convert">Convert</string><string name="prompt">Select a conversion</string><string name="units">Units</string> </resources>
arrays.xml 文件中保存了换算类型的字符串数组
<?xml version="1.0" encoding="utf-8"?> <resources><string-array name="conversions"><item>Acres to Square Miles</item><item>Atmospheres to Pascals</item><item>Bars to Pascals</item><item>Degrees Celsius to Degrees Fahrenheit</item><item>Degrees Fahrenheit to Degrees Celsius</item><item>Dynes to Newtons</item><item>Feet/Second to Metres/Second</item><item>Fluid Ounces (UK) to Litres</item><item>Fluid Ounces (US) to Litres</item><item>Horsepower (electric) to Watts</item><item>Horsepower (metric) to Watts</item><item>Kilograms to Tons (UK or long)</item><item>Kilograms to Tons (US or short)</item><item>Litres to Fluid ounces (UK)</item><item>Litres to Fluid ounces (US)</item><item>Mach Number to Metres/Second</item><item>Metres/Second to Feet/Second</item><item>Metres/Second to Mach Number</item><item>Miles/Gallon (UK) to Miles/Gallon (US)</item><item>Miles/Gallon (US) to Miles/Gallon (UK)</item><item>Newtons to Dynes</item><item>Pascals to Atmospheres</item><item>Pascals to Bars</item><item>Square Miles to Acres</item><item>Tons (UK or long) to Kilograms</item><item>Tons (US or short) to Kilograms</item><item>Watts to Horsepower (electric)</item><item>Watts to Horsepower (metric)</item></string-array> </resources>
gradientbg.xml 文件保存了用于Activity背景色的渐变形状
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><gradient android:startColor="#fccb06" android:endColor="#fd6006"android:angle="270"/><corners android:radius="10dp"/> </shape>
4 虚拟机运行结果
注意:
其中的uc.xml 换成以下代码可以运行
<menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"tools:context="com.apress.uc.UC" > </menu>
转载于:https://www.cnblogs.com/yzmb/p/4002760.html
Android攻略--单位转化器UC--Units Converter(学习笔记)相关推荐
- Android攻略之入门
1.Android简介 Android Developer's Guide 中将Android定义为针对移动设备的软件栈---用于实现完整的功能解决方案的软件子系统集合.这个栈中包括操作系统(由Lin ...
- 【嵌入式Android开发学习攻略】手把手教你循序渐进的学习
[嵌入式Android开发学习攻略]手把手教你循序渐进的学习 学习嵌入式主要有以下几个方面: C语言:C是必须学的,它是最基础的 操作系统:Linux.Android 目前Linux是主流,这个一 ...
- Android学习攻略:手把手教你循序渐进地学习Android知识
前言 如果你也学习Android,那么你大概率会看过我的文章.经常有读者给我留言:"该怎么学习Android?"."日常学习Android的方法是什么". 所以 ...
- uC/OS-II系统学习笔记(4)—— uC/OS-II概述上篇
1 临界段 uC/OS-II在处理临界段代码时,是需要关闭中断的,处理完毕之后再开中断,这使得uC/OS-II能够避免同时有其他任务或中断服务进入临界代码段. uC/OS-II定义了两个宏(macro ...
- Android深度探索(卷1)HAL与驱动开发学习笔记(8)
Android深度探索(卷1)HAL与驱动开发学习笔记(8) 第八章 蜂鸣器驱动 L i n u x驱动的代码重用有很多种方法.可以采用标准C程序的方式.将要重用的代码放在其他的文件(在头文件中声 ...
- Android入门到精通|安卓/Android开发零基础系列Ⅱ【职坐标】-学习笔记(1)-- 常用控件及资源介绍
前言 为了巩固Android基础知识,回顾一下学习内容,才有此学习笔记. IDE Androdi Studio 4 + Genymotion 创建项目 修改项目的 build.gradle,添加国内镜 ...
- android中资源文件的两种访问方式,Android_Android学习笔记-保存文件(Saving Files),Android设备有两种文件存储区域 - phpStudy...
Android学习笔记-保存文件(Saving Files) Android设备有两种文件存储区域: 内部存储和外部存储 ("internal" and "externa ...
- 31道Android面试题,字节跳动移动架构师学习笔记,大厂直通车!
关于Android的近况 大家都知道,今年移动开发不那么火热了,完全没有了前两年Android开发那种火热的势头,如此同时,AI热火朝天,很多言论都说Android不行了.其实不光是Android,i ...
- Unity3d用户手册用户攻略缓存效劳器常见疑问
缓存效劳器常见疑问 我的缓存效劳器 (Cache Server) 数据库巨细是不是会跟着导入和存储越来越多的资源而无限添加? 缓存效劳器 (Cache Server) 将主动移除长时刻未用的资源(当然 ...
- eflv2 android攻略,USM 1.5.2_ISO全能维护版(03pe+win8pe_x64全能,u+v2高端隐藏,EFI)
文件格式:ISO(量产专用),下载后为EXE自解压包,运行即可解压.量产成功后,U盘就为2个盘符. 如果需要下载UD版的,请移步: 软件来自系统总裁,作者:红毛樱木 此版依旧是03pe+win8pex ...
最新文章
- DataSet 动态添加列
- Xilinx IP核专题之PLL IP核介绍(Spartan-6)
- 2.MOC文件解读(上)——MOC文件中的数据
- 武汉linux驱动培训转让,linux驱动培训靠谱吗
- NewBeeNLP 年中 | From NewBee To NB
- mybatis plus 插入生成id_Mybatis-Plus 3.3.0 发布,让你专注业务快速敏捷开发
- VIP客户服务系统设计(外传)——面对变化的“世界”
- ENVI用ROI进行裁剪
- ArcGIS for Android 100.3的学习与应用(三) 实现地图添加自定义指北针
- 数量乘单价秋金额的计算机公式,在电子表格中数量乘以单价等于金额用什么样的公式啊?怎么算呢?...
- php木马在线查杀_网站木马监控查杀扫描 ASP木马 PHP木马上传拦截
- 手机显示DNS服务器异常,DNS服务器异常?
- RxSwift 介绍与简单使用
- 机器学习——决策树+随机森林
- “贵州与您相约”英文网站8月18日正式上线;大华股份助力长兴打造景区数字化管理样板 | 全球旅报...
- 十大炒股杠杆平台到底有哪些交易模式?
- python简易BMI测体重
- 【叔小生】JavaScript进阶篇
- MMORPG游戏的人工智能(AI)和行为树设计
- 区块链培训中的Golang为什么这么重要?怎么学?