今天没事干,做了个单击事件的练习。

截图如下:(一个小小的计算器)

XMl文件中的代码:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/startTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入NUMBER:" /> <EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<EditText
android:id="@+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" /> <Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" /> <Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="×" /> <Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="÷" />
</LinearLayout> <TextView
android:id="@+id/showResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/> </LinearLayout>

Activity代码:

package com.example.clickproject;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends Activity {
private EditText num1 = null;
private EditText num2 = null;
private TextView showResult = null;
private Button btnAdd = null;
private Button btnSub = null;
private Button btnMul = null;
private Button btnDiv = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
num1 = (EditText) super.findViewById(R.id.num1);
num2 = (EditText) super.findViewById(R.id.num2);
showResult = (TextView) super.findViewById(R.id.showResult);
btnAdd = (Button) super.findViewById(R.id.btnAdd);
btnSub = (Button) super.findViewById(R.id.btnSub);
btnMul = (Button) super.findViewById(R.id.btnMul);
btnDiv = (Button) super.findViewById(R.id.btnDiv); btnAdd.setOnClickListener(new AddOnclickListener());
btnSub.setOnClickListener(new SubOnclickListener());
btnMul.setOnClickListener(new MulOnclickListener());
btnDiv.setOnClickListener(new DivOnclickListener());
}
/**
* 加法监听子类
* ClassName AddOnclickListener
* Description TODO(这里用一句话描述这个类的作用)
* @author 石头杨 北科维拓公司
* date 2013-7-26 下午01:58:32
*
*/
private class AddOnclickListener implements OnClickListener{ @Override
public void onClick(View v) {
float a = Float.parseFloat(MainActivity.this.num1.getText().toString());
float b = Float.parseFloat(MainActivity.this.num2.getText().toString());
float c = a+b;
MainActivity.this.showResult.setText("结果:【"+a+"】 【+】 【"+b+"】 【=】"+c);
} }
/**
* 减法监听子类
* ClassName SubOnclickListener
* Description TODO(这里用一句话描述这个类的作用)
* @author 石头杨 北科维拓公司
* date 2013-7-26 下午01:58:56
*
*/
private class SubOnclickListener implements OnClickListener{ @Override
public void onClick(View v) {
float a = Float.parseFloat(MainActivity.this.num1.getText().toString());
float b = Float.parseFloat(MainActivity.this.num2.getText().toString());
float c = a-b;
MainActivity.this.showResult.setText("结果:【"+a+"】 【-】 【"+b+"】 【=】"+c);
} }
/**
* 乘法监听子类
* ClassName MulOnclickListener
* Description TODO(这里用一句话描述这个类的作用)
* @author 石头杨 北科维拓公司
* date 2013-7-26 下午01:59:11
*
*/
private class MulOnclickListener implements OnClickListener{ @Override
public void onClick(View v) {
float a = Float.parseFloat(MainActivity.this.num1.getText().toString());
float b = Float.parseFloat(MainActivity.this.num2.getText().toString());
float c = a*b;
MainActivity.this.showResult.setText("结果:【"+a+"】 【*】 【"+b+"】 【=】"+c);
} }
/**
* 除法监听子类
* ClassName DivOnclickListener
* Description TODO(这里用一句话描述这个类的作用)
* @author 石头杨 北科维拓公司
* date 2013-7-26 下午01:59:22
*
*/
private class DivOnclickListener implements OnClickListener{ @Override
public void onClick(View v) {
float a = Float.parseFloat(MainActivity.this.num1.getText().toString());
float b = Float.parseFloat(MainActivity.this.num2.getText().toString());
float c = a/b;
MainActivity.this.showResult.setText("结果:【"+a+"】 【/】 【"+b+"】 【=】"+c);
} } }

Android——例子:简单计算器的更多相关文章

  1. 菜鸟学Android编程——简单计算器《一》

    菜鸟瞎搞,高手莫进 本人菜鸟一枚,最近在学Android编程,网上看了一些视频教程,于是想着平时手机上的计算器应该很简单,自己何不尝试着做一个呢? 于是就冒冒失失的开撸了. 简单计算器嘛,功能当然很少 ...

  2. 每天2个android小例子----简单计算器源代码

    通过Android4.0 网格布局GridLayout来实现一个简单的计算器界面布局 package com.android.xiong.gridlayoutTest; import java.mat ...

  3. android实现简单计算器

    前台代码如下 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...

  4. 简单计算器 安卓 Android 作业

    Android老师布置的课程作业——简单计算器 功能要求实现四则运算,参考界面见下图: 首先给各位老铁提供apk安装包以及项目压缩包,略表诚意~ github地址:https://github.com ...

  5. Android 简单计算器实现源码

    1.string.xml代码 <?xml version="1.0" encoding="utf-8"?> <resources> &l ...

  6. Android发展简单介绍

    Android一词的本义指“机器人”,同一时候也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成,号称是首个为移动 ...

  7. android Glide简单使用

    版权声明:大家可以转载,请写明转载申明 https://blog.csdn.net/bzlj2912009596/article/details/81702367 今天,简单讲讲Android里Gli ...

  8. C语言,简单计算器【上】

    由于工作需要最近在研究PHP扩展,无可避免的涉及到了C语言.从出了学校以后C语言在实际工作中还没有用到过,所以必须要先进行一点复习工作.个人认为对于熟悉一样东西说最好的方法是上手实践.于是便想起了当时 ...

  9. hdu1237 简单计算器[STL 栈]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu1237 题干 代码和解释 解本题时使用了STL 栈,要记得使用#include<stack>. 解本题时使用了isdigit()函 ...

  10. 1.C#WinForm基础制作简单计算器

    利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号 MessageBox.S ...

随机推荐

  1. android 项目学习随笔一(闪屏 )

    1.取标题栏且全屏 <activity android:name="com.ecollab.zhsh66.SplashActivity" android:label=&quo ...

  2. android 学习随笔十五(Activity的生命周期与摧毁时返回数据 )

    1.Activity的生命周期 onCreate:创建时调用 onStart:在屏幕上可见,但是还没有获得焦点 onResume:可见并且获得焦点 onPause:可见,但是失去焦点 onStop:不 ...

  3. 【python cookbook】【数据结构与算法】8.与字典有关的计算问题

    问题:在字典上对数据执行各式各样的计算(比如求最小值.最大值.排序). 解决方案:利用zip()将字典的键-值对“反转”为值-键对序列. 例如:如下字典存放的股票名称和对应的价格: >>& ...

  4. 对比其它软件方法评估敏捷和Scrum

    一般来说,选择一种软件开发方法,更像是加入一个邪教组织,而不像是做出了一个技术决策.许多公司甚至从未试图去评估这些方法,而仅仅是盲目采用最流行的方法,这就造成了如今五花八门的各种敏捷方法.因此本文将使 ...

  5. javaWeb 在jsp中 使用自定义标签输出访问者IP

    1.java类,使用简单标签,jsp2.0规范, 继承 SimpleTagSupport public class ViewIpSimpleTag extends SimpleTagSupport { ...

  6. [转]jexus的安装

    转自http://www.cnblogs.com/xiaodiejinghong/p/3720921.html 这是一个集成了 mono3.4.0 和 jexus5.6.0 的 jexus+mono ...

  7. 原来样式改变不了(input type="file")

    label { background-color: #979fa8; color: #fff; display: inline-block; padding: .8rem 4rem; cursor: ...

  8. 64位系统安装ODBC驱动的方法

    为了更充分的利用硬件资源,我想很多人都开使用64位操作系统了,同时你可以也发现了在64位操作系统上ODBC的驱动找不到了,所以ODBC的东西都没法用了. 因为2007以前版本的Office只有32位版 ...

  9. C# PDF添加水印

    需要iTextSharp.dll, 下载地址http://sourceforge.net/projects/itextsharp/ using System;using System.Collecti ...

  10. Parencodings 分类: POJ 2015-06-28 22:00 7人阅读 评论(0) 收藏

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22757   Accepted: 13337 De ...