这是我的第一个andriod小程序,第一次写用了半个月,第二次修改用了一天,第三次修改用了两个小时,现在终于比较满意了。现在我就直接分享一下我的源代码,由于思路比较简单,注释加的不多。采用的是相对布局,但是此布局只适合480*800的分辨率,更高的分辨率界面会显示不正常,但是功能没有任何问题,你可以尝试用其他布局来实现界面,总之核心功能实现是不变的。
activity_main.xml文件如下:
复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.MainActivity"
tools:ignore="MergeRootFrame"
android:padding="15dp"
android:layout_gravity="center"
android:background="#111"
>
<Button
android:id="@+id/button4"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button13"
android:layout_alignBottom="@+id/button13"
android:layout_toLeftOf="@+id/button2"
android:background="#a10b39"
android:padding="10dp"
android:text="="
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginRight="3dp"/>
<Button
android:id="@+id/button9"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button10"
android:layout_alignBottom="@+id/button10"
android:layout_alignLeft="@+id/button6"
android:background="#666"
android:padding="10dp"
android:text="7"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginRight="3dp"
android:layout_width="70dp"
android:layout_marginBottom="3dp"
/>
<Button
android:id="@+id/button11"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button10"
android:layout_alignBottom="@+id/button10"
android:layout_toRightOf="@+id/button13"
android:background="#666"
android:padding="10dp"
android:text="9"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginRight="3dp"
android:layout_width="70dp"
android:layout_marginBottom="3dp"
/>
<Button
android:id="@+id/button10"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button18"
android:layout_below="@+id/button17"
android:background="#666"
android:padding="10dp"
android:text="8"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginRight="3dp"
android:layout_width="70dp"
android:layout_marginBottom="3dp"
/>
<Button
android:id="@+id/button20"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_above="@+id/button14"
android:layout_alignParentRight="true"
android:background="#a10b39"
android:padding="10dp"
android:text="←"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginBottom="3dp"/>
<Button
android:id="@+id/button19"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button20"
android:layout_alignBottom="@+id/button20"
android:layout_toLeftOf="@+id/button20"
android:background="#a10b39"
android:padding="10dp"
android:text="CE"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginRight="3dp"
android:layout_marginBottom="3dp"
/>
<Button
android:id="@+id/button18"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_above="@+id/button10"
android:layout_toLeftOf="@+id/button11"
android:background="#a10b39"
android:padding="10dp"
android:text="±"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginRight="3dp"
android:layout_marginBottom="3dp"
/>
<Button
android:id="@+id/button17"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginTop="5dp"
android:background="#a10b39"
android:padding="10dp"
android:text="√"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginRight="3dp"
android:layout_marginBottom="3dp" />
<Button
android:id="@+id/button14"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/button18"
android:background="#d89218"
android:padding="10dp"
android:text="÷"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginBottom="3dp" />
<Button
android:id="@+id/button7"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_below="@+id/button10"
android:layout_toRightOf="@+id/button6"
android:background="#666"
android:padding="10dp"
android:text="5"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginBottom="3dp"
android:layout_marginRight="3dp"/>
<Button
android:id="@+id/button6"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button7"
android:layout_alignBottom="@+id/button7"
android:layout_alignParentLeft="true"
android:background="#666"
android:padding="10dp"
android:text="4"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginRight="3dp"
android:layout_marginBottom="3dp" />
<Button
android:id="@+id/button15"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button7"
android:layout_alignBottom="@+id/button7"
android:layout_alignParentRight="true"
android:background="#d89218"
android:padding="10dp"
android:text="×"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginBottom="3dp" />
<Button
android:id="@+id/button8"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button7"
android:layout_alignBottom="@+id/button7"
android:layout_toRightOf="@+id/button10"
android:background="#666"
android:padding="10dp"
android:text="6"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginBottom="3dp"
android:layout_marginRight="3dp" />
<Button
android:id="@+id/button3"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_below="@+id/button7"
android:layout_toRightOf="@+id/button1"
android:background="#666"
android:padding="10dp"
android:text="2"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginBottom="3dp"
android:layout_marginRight="3dp" />
<Button
android:id="@+id/button1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_alignParentLeft="true"
android:background="#666"
android:padding="10dp"
android:text="1"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginBottom="3dp"
android:layout_marginRight="3dp" />
<Button
android:id="@+id/button5"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_toRightOf="@+id/button7"
android:background="#666"
android:padding="10dp"
android:text="3"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginBottom="3dp"
android:layout_marginRight="3dp"/>
<Button
android:id="@+id/button16"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button5"
android:layout_alignBottom="@+id/button5"
android:layout_alignLeft="@+id/button15"
android:background="#d89218"
android:padding="10dp"
android:text="-"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginBottom="3dp" />
<Button
android:id="@+id/button2"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button4"
android:layout_alignBottom="@+id/button4"
android:layout_alignParentRight="true"
android:background="#d89218"
android:padding="10dp"
android:text="+"
android:textColor="#fff"
android:textSize="28dp" />
<Button
android:id="@+id/button13"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:layout_toLeftOf="@+id/button4"
android:padding="10dp"
android:text="."
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginRight="3dp"
android:background="#d89218" />
<Button
android:id="@+id/button12"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button13"
android:layout_alignBottom="@+id/button13"
android:layout_alignParentLeft="true"
android:background="#666"
android:padding="10dp"
android:text="0"
android:textColor="#fff"
android:textSize="28dp"
android:layout_marginRight="3dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/editText1"
android:background="#666"
android:text=" "
android:textColor="#fff"
android:textSize="15dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignLeft="@+id/button17"
android:layout_below="@+id/textView1"
android:background="#666"
android:ems="10"
android:singleLine="true"
android:textColor="#000"
android:textSize="28dp" />
</RelativeLayout>
复制代码
MainActivity.java代码如下:
复制代码
package com.example.mycalculator;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13,btn14,btn15,btn16,btn17,btn18,btn19,btn20;
private EditText editText;//显示输入的数字
private String opt = "+";//操作符
private double n1 = 0.0, n2 = 0.0;//两个操作数
private TextView textView;//显示算式
//跟据被选择按钮的id设置监听器
private OnClickListener lisenter = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText = (EditText)findViewById(R.id.editText1);
textView = (TextView) findViewById(R.id.textView1);
String s = editText.getText().toString();//获取字符串
Button btn =(Button)v;
try{
switch(btn.getId())
{
case R.id.button1://1
{
String str = editText.getText().toString();
editText.setText(str + 1);
str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.button2://+
{
String str = editText.getText().toString();
n1 = Double.parseDouble(str);
opt = "+";
textView.setText(n1 + opt);
editText.setText("");
break;
}
case R.id.button3://2
{
String str = editText.getText().toString();
editText.setText(str + 2);
str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.button4://操作符=
{
if(opt == "+")
{
String str = editText.getText().toString();
n2 = Double.parseDouble(str);
textView.setText(n1 + opt + n2 + "=");
editText.setText((n1 + n2) + "");
}
else if(opt == "-")
{
String str = editText.getText().toString();
n2 = Double.parseDouble(str);
textView.setText(n1 + opt + n2 + "=");
editText.setText((n1 - n2) + "");
}
else if(opt == "*")
{
String str = editText.getText().toString();
n2 = Double.parseDouble(str);
textView.setText(n1 + opt + n2 + "=");
editText.setText((n1 * n2) + "");
}
else if(opt == "/")
{
String str = editText.getText().toString();
n2 = Double.parseDouble(str);
if(n2 == 0)
{
editText.setText("");
textView.setText("除数不能为0");
break;
}
else
{
textView.setText(n1 + opt + n2 + "=");
editText.setText((n1 / n2) + "");
}
}
break;
}
case R.id.button5://3
{
editText.setText(editText.getText().toString() + 3);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.button6://4
{
editText.setText(editText.getText().toString() + 4);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.button7://5
{
editText.setText(editText.getText().toString() + 5);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.button8://6
{
editText.setText(editText.getText().toString() + 6);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.button9://7
{
editText.setText(editText.getText().toString() + 7);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.button10://8
{
editText.setText(editText.getText().toString() + 8);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.button11://9
{
editText.setText(editText.getText().toString() + 9);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.button12://0
{
textView.setText(n1 + opt + 10);
editText.setText(editText.getText().toString() + 0);
String str = editText.getText().toString();
textView.setText(str);
break;
}
case R.id.button13://.
{
String str = editText.getText().toString();
if(str.indexOf(".") != -1) //判断字符串中是否已经包含了小数点,如果有就什么也不做
{
}
else //如果没有小数点
{
if(str.equals("0"))//如果开始为0,
editText.setText(("0" + ".").toString());
else if(str.equals(""))//如果初时显示为空,就什么也不做
{
}
else
editText.setText(str + ".");
}
break;
}
case R.id.button14://操作符 /
{
String str = editText.getText().toString();
n1 = Double.parseDouble(str);
opt = "/";
editText.setText("");
textView.setText(n1 + opt);
break;
}
case R.id.button15://操作符*
{
String str = editText.getText().toString();
n1 = Double.parseDouble(str);
opt = "*";
editText.setText("");
textView.setText(n1 + opt);
break;
}
case R.id.button16://操作符-
{
String str = editText.getText().toString();
n1 = Double.parseDouble(str);
opt = "-";
editText.setText("");
textView.setText(n1 + opt);
break;
}
case R.id.button17://genhao
{
String str = editText.getText().toString();
n1 = Double.parseDouble(str);
editText.setText(Math.sqrt(n1) + "");
textView.setText(n1 + "的平方根是");
break;
}
case R.id.button18://+/-
{
String str =editText.getText().toString();
n1 = Double.parseDouble(str);
if(str.length() > 0)
editText.setText(-n1 + "");
textView.setText(-n1 + "");
break;
}
case R.id.button19://CE
{
String str =editText.getText().toString();
if(str.length() > 0)
editText.setText("");
break;
}
case R.id.button20://<-
{
String str =editText.getText().toString();
if(str.length() > 0)
editText.setText(str.substring(0, str.length() - 1));
break;
}
}
}catch(Exception e){}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
//获取按钮的id
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn4 = (Button) findViewById(R.id.button4);
btn5 = (Button) findViewById(R.id.button5);
btn6= (Button) findViewById(R.id.button6);
btn7 = (Button) findViewById(R.id.button7);
btn8 = (Button) findViewById(R.id.button8);
btn9 = (Button) findViewById(R.id.button9);
btn10 = (Button) findViewById(R.id.button10);
btn11 = (Button) findViewById(R.id.button11);
btn12 = (Button) findViewById(R.id.button12);
btn13 = (Button) findViewById(R.id.button13);
btn14 = (Button) findViewById(R.id.button14);
btn15 = (Button) findViewById(R.id.button15);
btn16 = (Button) findViewById(R.id.button16);
btn17 = (Button) findViewById(R.id.button17);
btn18 = (Button) findViewById(R.id.button18);
btn19 = (Button) findViewById(R.id.button19);
btn20 = (Button) findViewById(R.id.button20);
//为按钮添加监听器
btn1.setOnClickListener(lisenter);
btn2.setOnClickListener(lisenter);
btn3.setOnClickListener(lisenter);
btn4.setOnClickListener(lisenter);
btn5.setOnClickListener(lisenter);
btn6.setOnClickListener(lisenter);
btn7.setOnClickListener(lisenter);
btn8.setOnClickListener(lisenter);
btn9.setOnClickListener(lisenter);
btn10.setOnClickListener(lisenter);
btn11.setOnClickListener(lisenter);
btn12.setOnClickListener(lisenter);
btn13.setOnClickListener(lisenter);
btn14.setOnClickListener(lisenter);
btn15.setOnClickListener(lisenter);
btn16.setOnClickListener(lisenter);
btn17.setOnClickListener(lisenter);
btn18.setOnClickListener(lisenter);
btn19.setOnClickListener(lisenter);
btn20.setOnClickListener(lisenter);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in
AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
- android简易计算器
activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
- 制作一个简易计算器——基于Android Studio实现
一个计算器Android程序的源码部分分为主干和细节两部分. 一.主干 1. 主干的构成 计算器的布局 事件(即计算器上的按钮.文本框)监听 实现计算 2. 详细解释 假设我们的项目名为Calcula ...
- Android简易实战教程--第一话《最简单的计算器》
转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/51707931 从今天开始,本专栏持续更新Android简易实战类博客文章.和以往专 ...
- JavaScript简易计算器
JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标 ...
- 自制c#简易计算器
这是一个课堂作业,我觉得作为一个简易的计算器不需要态度复杂的东西,可能还有一些bug,有空再慢慢加强. using System;using System.Collections.Generic;us ...
- 剖析简易计算器带你入门微信小程序开发
写在前面,但是重点在后面 这是教程,也不是教程. 可以先看Demo的操作动图,看看是个什么玩意儿,GitHub地址(https://github.com/dunizb/wxapp-sCalc) 自从微 ...
- PHP学习笔记02——简易计算器
<!DOCTYPE html> <html> <head> <title>PHP简易计算器</title> </head> &l ...
- JavaScript之简易计算器
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...
- 菜鸟学习Struts——简易计算器
这是学习Struts的一个简单的例子文件结构如下: 1.配置Struts环境 2.新建input.jsp,success.jsp,error.jsp input.jsp代码如下: <%@ pag ...
随机推荐
- samba 服务器搭建
为了能在两台机器上共享代码,方便测试不同平台性能和搭建分布式的web server,今天耗费半天时间搭建一个samba服务器共享数据,要求开放写权限,但多次实验均告失败,最终在 鸟哥 的提醒下 检查发 ...
- 用GDI+DrawImage画上去的图片会变大
问题: 用GDI+DrawImage画上去的图片会变大 解释: Status DrawImage(Image *image,const Point &point);两参数的这个接口是这么设计的 ...
- SharePoint 2013 Step by Step—— 为终端用户提供故障恢复的解决方案 Part I
Disaster Recovery,我把他直译"故障恢复",或者也可以翻译成 "灾难复原 ".光字面意思就可以领会到,当SharePoint Server发生了 ...
- CSS3选择器之学习笔记
首先说first-child与last-child,这两个选择器很容易明白,就是父元素下的第一个子元素和最后一个子元素.而nth-child和nth-last-child则是父元素下指定序号的子元素, ...
- Sql 查询当天、本周、本月记录
--查询当天: select * from info where DateDiff(dd,datetime,getdate())=0 --查询24小时内的: select * from info wh ...
- SQL SERVER 数据库被标记为“可疑”的解决办法
问题背景: 日常对Sql Server 2005关系数据库进行操作时,有时对数据库(如:Sharepoint网站配置数据库名Sharepoint_Config)进行些不正常操作如数据库在读写时而无故停 ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- 菜鸟教程之工具使用(二)——Maven打包非规范目录结构的Web项目
用过Maven的人都知道,Maven项目的目录结构跟传统的DynamicWeb项目有些不同.当然我们按照Maven的规范建项目最好,但是当你恰好没有按照Maven的规范来,又恰好需要使用Maven来打 ...
- Rocket MQ 源码解析
http://rocketmq.apache.org/rocketmq/the-design-of-transactional-message/ http://www.iocoder.cn/Rocke ...
- ASP.NET MVC 部分视图
[部分视图] ASP.NET MVC 里的部分视图,相当于 Web Form 里的 User Control.我们的页面往往会有许多重用的地方,可以进行封装重用.使用 部分视图 : 1. 可以简写代 ...