这是很久之前做的,方法很简单,都是一些基础的,不足的是还有很多功能不够。

activity_main.xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" > <requestFocus />
</EditText> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClick"
android:text="+"
android:textColor="#000fff"
android:textSize="20dp" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClick1"
android:text="-" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClick2"
android:text="*" /> <Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClick3"
android:text="/" /> </LinearLayout> <EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" /> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="=" /> <EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:ems="10" /> </LinearLayout>

  

MainActivity:

package com.example.jisuanqi;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //加法
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText ed1=(EditText)findViewById(R.id.editText1);
EditText ed2=(EditText)findViewById(R.id.editText2);
EditText ed3=(EditText)findViewById(R.id.editText3);
int num1=Integer.parseInt(ed1.getText().toString());
int num2=Integer.parseInt(ed2.getText().toString());
int num3=num1+num2;
ed3.setText(String.valueOf(num3));
}
});
//减法
Button button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText ed1=(EditText)findViewById(R.id.editText1);
EditText ed2=(EditText)findViewById(R.id.editText2);
EditText ed3=(EditText)findViewById(R.id.editText3);
int num1=Integer.parseInt(ed1.getText().toString());
int num2=Integer.parseInt(ed2.getText().toString());
int num3=num1-num2;
ed3.setText(String.valueOf(num3));
}
});
//乘法
Button button3=(Button)findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText ed1=(EditText)findViewById(R.id.editText1);
EditText ed2=(EditText)findViewById(R.id.editText2);
EditText ed3=(EditText)findViewById(R.id.editText3);
int num1=Integer.parseInt(ed1.getText().toString());
int num2=Integer.parseInt(ed2.getText().toString());
int num3=num1*num2;
ed3.setText(String.valueOf(num3));
}
});
//除法
Button button4=(Button)findViewById(R.id.button4);
button4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText ed1=(EditText)findViewById(R.id.editText1);
EditText ed2=(EditText)findViewById(R.id.editText2);
EditText ed3=(EditText)findViewById(R.id.editText3);
int num1=Integer.parseInt(ed1.getText().toString());
int num2=Integer.parseInt(ed2.getText().toString());
int num3=num1/num2;
ed3.setText(String.valueOf(num3));
}
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

  

If reference to indicate the source:蔡都平

Android简单计算器的更多相关文章

  1. Android 简单计算器源码....

    PS:今天算是闲着没事做了一个小型的计算器...顺便熟悉一下Android的布局,组件,以及时间监听的方法...就当是做了一个小小的练习吧...     顺便去对比了一下别人写的代码...有的使用到了 ...

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

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

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

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

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

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

  5. 第一个Android项目——计算器

    第一个Android项目——计算器 效果 开始学Android一两个星期了,学习了一下基本的Activity.简单控件及几个简单布局,打算找个东西来练练手,于是就选择发计算器.关于计算器中用到的四则运 ...

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

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

  7. PAT 06-1 简单计算器

    想看一般简单计算器实现的看客不好意思了,这不是你想要点东西,此处题设为“只能进行加减乘除”.“都是整数”.”优先级相同“和"从左到右".此题来自PAT(http://www.pat ...

  8. php大力力 [005节] php大力力简单计算器001

    2015-08-22 php大力力005. php大力力简单计算器001: 上网看视频,看了半天,敲击代码,如下: <html> <head> <title>简单计 ...

  9. PHP实现简单计算器

    <!--简单的计算器--> <!DOCTYPE html> <html> <head> <title>PHP实现简单计算器</titl ...

随机推荐

  1. VS~单步调试DLL

    有时我们从第三方下载DLL库之后,在使用VS进行调试时还是很麻烦的,现在我总结一下,在开发过过程中调试DLL的方法,希望对各位在开发中有帮助. 1 VS下载插件.Net Refector 2 引用你的 ...

  2. Vieta定理

    一元$n$次方程$$P(x)=a_{n}x^{n}+a_{n-1}x^{n-1}+\cdots+a_{a}x+a_{0}=a_{n}(x-x_{1})(x-x_{2})\cdots (x-x_{n}) ...

  3. 使用 Tomcat 7 新的连接池 —— Tomcat jdbc pool

    Tomcat 在 7.0 以前的版本都是使用 commons-dbcp 做为连接池的实现,但是 dbcp 饱受诟病,原因有: dbcp 是单线程的,为了保证线程安全会锁整个连接池 dbcp 性能不佳 ...

  4. PostgreSQL的 initdb 源代码分析之二十四

    继续分析: make_template0(); 展开: 无需再作解释,就是创建template0数据库 /* * copy template1 to template0 */ static void ...

  5. WIM更新命令(打补丁)

    在D盘新建3个文件夹:win7(install.wim).updates(补丁).win7ultra 1.先打开ISO文件,然后加载映像到D:\win7ultra文件夹dism /mount-wim ...

  6. TcxDBLookupCombobox 级联时第二级不显示正确内容的处理方法

    在使用两个级联的 TcxDBLookupCombobox 时,会出现这种情况:当第一级的内容变更后,第二级的控件在界面上显示的文本不变化,即使数据集已经通过 Properites.OnChange 事 ...

  7. 定义member【C++】cstddef中4个定义

    最近研究定义member,稍微总结一下,以后继续补充: size_t size_t corresponds to the integral data type returned by the lang ...

  8. apacheserver下载、安装、配置

     1.下载 下载地址:apache下载地址 点击左側"Download"下的链接,打开例如以下页面 这里能够选择版本号.我选择2.4.16.单击"2.4.16" ...

  9. [Jest] Track project code coverage with Jest

    Jest comes pre-packaged with the ability to track code coverage for the modules you're testing, but ...

  10. [AngularJS] Lazy Loading modules with ui-router and ocLazyLoad

    We've looked at lazy loading with ocLazyLoad previously, but what if we are using ui-router and want ...