博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android攻略--单位转化器UC--Units Converter(学习笔记)
阅读量:6955 次
发布时间:2019-06-27

本文共 7468 字,大约阅读时间需要 24 分钟。

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 miles      101325.0,          // Atmospheres to Pascals      100000.0,          // Bars to Pascals      0,                 // Degrees Celsius to Degrees Fahrenheit (placeholder)      0,                 // Degrees Fahrenheit to Degrees Celsius (placeholder)      0.00001,           // Dynes to Newtons      0.3048,            // Feet/Second to Metres/Second      0.0284130625,      // Fluid Ounces (UK) to Litres      0.0295735295625,   // Fluid Ounces (US) to Litres      746.0,             // Horsepower (electric) to Watts      735.499,           // Horsepower (metric) to Watts      1/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/Second      1/0.3048,          // Metres/Second to Feet/Second      1/331.5,           // Metres/Second to Mach Number      0.833,             // Miles/Gallon (UK) to Miles/Gallon (US)      1/0.833,           // Miles/Gallon (US) to Miles/Gallon (UK)      100000.0,          // Newtons to Dynes      1/101325.0,        // Pascals to Atmospheres      0.00001,           // Pascals to Bars      640.0,             // Square Miles to Acres      1016.0469088,      // Tons (UK or long) to Kilograms      907.18474,         // Tons (US or short) to Kilograms      1/746.0,           // Watts to Horsepower (electic)      1/735.499          // Watts to Horsepower (metric)   };   @Override   public 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
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() { @Override public void onItemSelected(AdapterView
parent, View view, int position, long id) { UC.this.position = position; } @Override public 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() { @Override public void onClick(View v) { etUnits.setText(""); } }; btnClear.setOnClickListener(ocl); btnClear.setEnabled(false); final Button btnConvert = (Button) findViewById(R.id.convert); ocl = new AdapterView.OnClickListener() { @Override public 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 Fahrenheit else if (position == 4) result = (input-32)*5.0/9.0; // Fahrenheit to Celsius else result = input*multipliers[position]; etUnits.setText(""+result); } }; btnConvert.setOnClickListener(ocl); btnConvert.setEnabled(false); Button btnClose = (Button) findViewById(R.id.close); ocl = new AdapterView.OnClickListener() { @Override public void onClick(View v) { finish(); } }; btnClose.setOnClickListener(ocl); TextWatcher tw; tw = new TextWatcher() { @Override public void afterTextChanged(Editable s) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public 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文件

strings.xml 保存了应用中的字符串

Units Converter
Clear
Close
Convert
Select a conversion
Units

arrays.xml 文件中保存了换算类型的字符串数组

Acres to Square Miles
Atmospheres to Pascals
Bars to Pascals
Degrees Celsius to Degrees Fahrenheit
Degrees Fahrenheit to Degrees Celsius
Dynes to Newtons
Feet/Second to Metres/Second
Fluid Ounces (UK) to Litres
Fluid Ounces (US) to Litres
Horsepower (electric) to Watts
Horsepower (metric) to Watts
Kilograms to Tons (UK or long)
Kilograms to Tons (US or short)
Litres to Fluid ounces (UK)
Litres to Fluid ounces (US)
Mach Number to Metres/Second
Metres/Second to Feet/Second
Metres/Second to Mach Number
Miles/Gallon (UK) to Miles/Gallon (US)
Miles/Gallon (US) to Miles/Gallon (UK)
Newtons to Dynes
Pascals to Atmospheres
Pascals to Bars
Square Miles to Acres
Tons (UK or long) to Kilograms
Tons (US or short) to Kilograms
Watts to Horsepower (electric)
Watts to Horsepower (metric)

gradientbg.xml 文件保存了用于Activity背景色的渐变形状

4 虚拟机运行结果

 

注意:

其中的uc.xml 换成以下代码可以运行

 

转载于:https://www.cnblogs.com/yzmb/p/4002760.html

你可能感兴趣的文章
技术解析系列 | PouchContainer 支持 LXCFS 实现高可靠容器隔离
查看>>
linux中web服务器的基本配置
查看>>
linux服务器之间设置ssh免密登录
查看>>
如何将M4A格式的音频转换为MP3格式?只需一步搞定
查看>>
APP项目资源对接平台有那几家
查看>>
微信自定义网页分享链接(可自定义链接 图片 内容介绍)
查看>>
Oracle管理表空间(三)--Oracle UNDO表空间
查看>>
Oracle使用rman进行表空间基于时间点的恢复
查看>>
DNS 多网段的反向记录
查看>>
mac效率工具
查看>>
Oracle imp和exp的使用
查看>>
软件工程---典型用户
查看>>
Acunetix Web Vulnerability Scanner 8.x.x 逆向
查看>>
判断是否是IP地址格式
查看>>
我心目中的牛程序员、我们可以对比看看(人家还是看多年朋友面子上才肯帮忙1周,至少需支付1万元辛苦费)...
查看>>
实际工作中遇到的技术难题与大家交流(工作流条件表达式计算部分),希望技术高手能给于指点...
查看>>
Yii 2 —— 记住密码
查看>>
javascript面向对象技术基础(六)
查看>>
HBase-1.0.1学习笔记(八)启动脚本解析
查看>>
Spring学习笔记AOP(四)
查看>>