Android实战教程第一篇之最简单的计算器

所属分类: 软件编程 / Android 阅读数: 25
收藏 0 赞 0 分享

从今天开始,本专栏持续更新Android简易实战类博客文章。和以往专栏不同,此专栏只有实例。每个实例尽量按照知识点对应相应一章节的内容去写,循序渐进。有些实例可能会与另一个专栏有重复的文章。

开始本专栏的第一个简易案例:

首先设置两个布局文件,一个布局文件进行输入数据,获取加法运算;另一个布局文件进行显示最终结果。Activity1启动Activity2,并传递计算结果值给Activity2.

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" 
 > 
<EditText 
 android:id="@+id/factorOne" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 /> 
<TextView 
 android:id="@+id/symbol" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 /> 
<EditText 
 android:id="@+id/factorTwo" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 /> 
<Button 
 android:id="@+id/calculate" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 /> 
</LinearLayout> 

页面展示:

result.xml

activity03活动:

package mars.activity03; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
//1.在Activity03当中,要声明四个控件 
//2.要为其中的两个控件设置显示的值 
//3.创建一个监听器类,监听按钮按下的动作 
//4.将监听器类的对象,绑定在按钮对象上 
public class Activity03 extends Activity { 
 /** Called when the activity is first created. */ 
 private EditText factorOne ; 
 private EditText factorTwo; 
 private TextView symbol; 
 private Button calculate; 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.main); 
 //根据控件的ID来取得代表控件的对象 
 factorOne = (EditText)findViewById(R.id.factorOne); 
 factorTwo = (EditText)findViewById(R.id.factorTwo); 
 symbol = (TextView)findViewById(R.id.symbol); 
 calculate = (Button)findViewById(R.id.calculate); 
 //为symbol和calculate设置显示的值 
// symbol.setText("乘以"); 
// calculate.setText("计算"); 
 symbol.setText(R.string.symbol);//这里通过引用的方式,去String文件中引用。保证了业务逻辑、视图、引用资源分开 
 calculate.setText(R.string.calculate); 
 //将监听器的对象绑定到按钮对象上面 
 calculate.setOnClickListener(new CalculateListener()); 
 } 
 //当客户点击MENU按钮的时候,调用该方法 
 @Override 
 public boolean onCreateOptionsMenu(Menu menu) { 
 menu.add(0, 1, 1, R.string.exit); 
 menu.add(0,2,2,R.string.about); 
 return super.onCreateOptionsMenu(menu); 
 } 
 //当客户点击菜单当中的某一个选项时,会调用该方法 
 @Override 
 public boolean onOptionsItemSelected(MenuItem item) { 
 if(item.getItemId() == 1){ 
  finish(); 
 } 
 return super.onOptionsItemSelected(item); 
 } 
 class CalculateListener implements OnClickListener{ 
 
 @Override 
 public void onClick(View v) { 
  //取得两个EditText控件的值 
  String factorOneStr = factorOne.getText().toString(); 
  String factorTwoStr = factorTwo.getText().toString(); 
  //将这两个值存放到Intent对象当中 
  Intent intent = new Intent(); 
  intent.putExtra("one",factorOneStr); 
  intent.putExtra("two",factorTwoStr); 
  intent.setClass(Activity03.this, ResultActivity.class); 
  //使用这个Intent对象来启动ResultActivity 
  Activity03.this.startActivity(intent); 
 } 
 } 
} 

resultActivity:

package mars.activity03; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 
//1.接受从Activity03当中所传递的值 
//2.计算两个值的积 
//3.将计算的结果显示在Activity上 
public class ResultActivity extends Activity{ 
 private TextView resultView; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 // TODO Auto-generated method stub 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.result); 
 resultView = (TextView)findViewById(R.id.result); 
 //得到Intent对象当中的值 
 Intent intent = getIntent(); 
 String factorOneStr = intent.getStringExtra("one"); 
 String factorTwoStr = intent.getStringExtra("two"); 
 int factorOneInt = Integer.parseInt(factorOneStr); 
 int factorTwoInt = Integer.parseInt(factorTwoStr); 
 //计算两个值的积 
 int result = factorOneInt * factorTwoInt; 
 resultView.setText(result + ""); 
 } 
 
} 

String.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 <string name="hello">Hello World, Activity03!</string> 
 <string name="app_name">activity03</string> 
 <string name="resultLabel">result</string> 
 <string name="symbol">乘以</string> 
 <string name="calculate">计算</string> 
 <string name="exit">退出</string> 
 <string name="about">关于</string> 
</resources> 

最后再看一下配置文件:活动都要进行注册,并且设置Activity03为主活动

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 package="mars.activity03" 
 android:versionCode="1" 
 android:versionName="1.0"> 
 <application android:icon="@drawable/icon" android:label="@string/app_name"> 
 <activity android:name=".Activity03" 
   android:label="@string/app_name"> 
  <intent-filter> 
  <action android:name="android.intent.action.MAIN" /> 
  <category android:name="android.intent.category.LAUNCHER" /> 
  </intent-filter> 
 </activity> 
 <activity android:name=".ResultActivity" android:label="@string/resultLabel"/><!--这里使ResultActivity标题栏显示result--> 
 </application> 
 <uses-sdk android:minSdkVersion="4" /> 
 
</manifest> 

结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多