Android初级教程Activity小案例(计算器乘法运算)
首先设置两个布局文件,一个布局文件进行输入数据,获取加法运算;另一个布局文件进行显示最终结果。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
<?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"
>
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
界面展示:
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初级教程Activity小案例(计算器乘法运算)的更多相关文章
- Android初级教程理论知识(第三章测试&数据存储&界面展现)
首先介绍单元测试,我在javaweb部分有详细介绍单元测试框架的一篇文章. 可以先看在javaweb中的单元测试详解篇http://blog.csdn.net/qq_32059827/article/ ...
- Android初级教程调用手机拍照与摄像功能
这个小案例建议在手机上运行. package com.example.camera; import java.io.File; import android.net.Uri; import andro ...
- Android初级教程理论知识(第二章布局&读写文件)
常见布局 相对布局 RelativeLayout 组件默认左对齐.顶部对齐 设置组件在指定组件的右边 android:layout_toRightOf="@id/tv1" 设置在指 ...
- Android初级教程理论知识(第五章页面跳转和数据传递)
总体概述: Android四大组件 Activity BroadCastReceiver Service ContentProvider 创建第二个activity 新创建的activity,必须在清 ...
- Android初级教程三个Dialog对话框小案例
这里把三个对话框形式写在一个项目程序里面,用三个按钮控制显示什么样式的对话框. 先看布局文件代码: <LinearLayout xmlns:android="http://schema ...
- Android初级教程小案例之单选框RadioGroup与复选框CheckBox
Android里面的单选框和html中的其实是一样的效果.这里用到两个控件:CheckBox和RadioGroup.直接上代码: radio.xml布局文件: <?xml version=&qu ...
- Android初级教程人品计算器
先看布局: main_activity.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andr ...
- Android初级教程:RatingBar的使用
记得淘宝里面买家给卖家评分的时候会有一个星星状的评分条,其实就是基于RatingBar做了自定义使用了.那么本篇文章就对RatingBar的使用做一个基本的认识. 接下来就是正题,那就是对于Ratin ...
- Android初级教程通过简要分析“土司”源码,来自实现定义土司理论探讨
由于系统自带的土司瞬间即逝,而且非常难看.因此我们就希望自定义自己的土司风格.有些实例就是基于自定义土司完成的,例如金山卫士的火箭发射,基本原理就是个土司.但是在做出自己的土司风格之前,还是要简要分析 ...
随机推荐
- 四种方式实现子goroutine与主线程的同步
如何实现子goroutine与主线程的同步 第一种方式: 这种方式很太死板,就不演示了. 第二种方式:使用 channel机制,每个 goroutine传一个 channel进去然后往里写数据,在再主 ...
- CSS实现元素居中原理解析
在 CSS 中要设置元素水平垂直居中是一个非常常见的需求了.但就是这样一个从理论上来看似乎实现起来极其简单的,在实践中,它往往难住了很多人. 让元素水平居中相对比较简单:如果它是一个行内元素,就对它的 ...
- A quike guide teaching you how to use matlab to read netCDF file and plot a figure
1. Preparation 2. A brief introduce to netCDF. 4 3. Data Structure. 4 3.1 Attrib ...
- 实验-使用VisualVM或JConsole进行对程序进行性能分析
参考资料: 性能分析神器VisualVM java可视化监控工具 完成下列任务: 1.分析内存堆 使用+进行频繁的字符串拼接 2.CPU性能分析 3.线程分析 编程比较以下几个方法所创建的线程 Exe ...
- Linux下的有用命令
在之前的博客<Linux下常用命令与使用技巧>中,介绍了Linux的常用命令,在今天的博客中,给大家介绍其他的有用命令. 1.文本转换命令 在Linux下工作,我们不可避免地要和文件格式做 ...
- Android源码解析——Toast
简介 Toast是一种向用户快速展示少量信息的视图.当它显示时,它会浮在整个应用层的上面,并且不会获取到焦点.它的设计思想是能够向用户展示些信息,但又能尽量不显得唐突.本篇我们来研读一下Toast的源 ...
- 《An Industrial-Strength Audio Search Algorithm》译文
随着微信摇一摇逐渐被大众所广泛使用,听歌识曲功能也开始被关注.目前来看,像音乐雷达和微信摇一摇都采用了经典的shazam算法,为了使大家对shazam算法更加了解,我将其经典论文进行了翻译,希望对大家 ...
- ROS(indigo) 安装和使用更新版本的Gazebo----3,4,5,6,7 附:中国机器人大赛中型组仿真比赛说明
ROS(indigo) 安装和使用更新版本的Gazebo,本文以7为例. Gazebo7支持更多新的功能,如果使用下面命令安装ROS(indigo): ~$ sudo apt-get install ...
- JVM内存区域划分(JDK6/7/8中的变化)
前言 Java程序的运行是通过Java虚拟机来实现的.通过类加载器将class字节码文件加载进JVM,然后根据预定的规则执行.Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同 ...
- activiti 动态配置 activiti 监听引擎启动和初始化(高级源码篇)
1.1.1. 前言 用户故事:现在有这样一个需求,第一个需求:公司的开发环境,测试环境以及线上环境,我们使用的数据库是不一样的,我们必须能够任意的切换数据库进行测试和发布,对数据库连接字符串我们需要加 ...