首页 > 代码库 > andriod first app-computer

andriod first app-computer

andriod first app-computer

个人信息:就读于燕大本科软件工程专业 目前大三;

本人博客:google搜索“cqs_2012”即可;

个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;

编程语言:java ;

编程坏境:Windows 7 专业版 x64;

编程工具:jdk,eclipse,AVD;

制图工具:office 2010 powerpoint;

硬件信息:7G-3 笔记本;

自己想说的话

以前没有接触过andriod开发,突然接触后感觉很喜欢,难度不是很大,今天自己写了一个andriod小应用,名为计算器,实现计算功能。

自己程序截图

开始时,输入框为0,点击1 等待输入

继续操作,点击2


然后点击操作,如+

这时候数据框会自动清零,然后等待输入,点击5

然后点击=

其他功能不再显示。

本程序见面布局XML文件

<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"     android:background="#000">        <EditText            android:id="@+id/computerfirstedt"            android:layout_width="fill_parent"            android:layout_height="50dp"            android:layout_x="0dp"            android:layout_y="0dp"            android:ems="10">        </EditText>         <Button            android:id="@+id/btnnumber1"            android:layout_width="50dp"            android:layout_height="wrap_content"			 			android:layout_x="0dp" 			android:layout_y="50dp"            android:text="1" />        <Button            android:id="@+id/btnnumber2"            android:layout_width="50dp"            android:layout_height="wrap_content" 			android:layout_x="60dp" 			android:layout_y="50dp"            android:text="2" />        <Button            android:id="@+id/btnnumber3"            android:layout_width="50dp"            android:layout_height="wrap_content"             android:layout_x="120dp" 			android:layout_y="50dp"            android:text="3" />        <Button            android:id="@+id/btnnumber4"            android:layout_width="50dp"            android:layout_height="wrap_content"             android:layout_x="180dp" 			android:layout_y="50dp"            android:text="4" />                     <Button            android:id="@+id/btnnumber5"            android:layout_width="50dp"            android:layout_height="wrap_content"             android:layout_x="0dp" 			android:layout_y="110dp"            android:text="5" />        <Button            android:id="@+id/btnnumber6"            android:layout_width="50dp"            android:layout_height="wrap_content"             android:layout_x="60dp" 			android:layout_y="110dp"            android:text="6" />        <Button            android:id="@+id/btnnumber7"            android:layout_width="50dp"            android:layout_height="wrap_content"             android:layout_x="120dp" 			android:layout_y="110dp"            android:text="7" />           <Button            android:id="@+id/btnnumber8"            android:layout_width="50dp"            android:layout_height="wrap_content"             android:layout_x="180dp" 			android:layout_y="110dp"            android:text="8" />          <Button            android:id="@+id/btnnumber9"            android:layout_width="50dp"            android:layout_height="wrap_content"             android:layout_x="0dp" 			android:layout_y="170dp"            android:text="9" />           <Button            android:id="@+id/btnnumber0"            android:layout_width="50dp"            android:layout_height="wrap_content"             android:layout_x="60dp" 			android:layout_y="170dp"            android:text="0" />            <Button            android:id="@+id/btnnumberC"            android:layout_width="50dp"            android:layout_height="wrap_content"               android:layout_x="120dp" 			android:layout_y="170dp"            android:text="清空" />           <Button            android:id="@+id/btnnumberR"            android:layout_width="50dp"            android:layout_height="wrap_content"             android:layout_x="180dp" 			android:layout_y="170dp"            android:text="=" />                     <Button            android:id="@+id/btnnumberM"            android:layout_width="50dp"            android:layout_height="wrap_content"             android:layout_x="0dp" 			android:layout_y="230dp"            android:text="*" />           <Button            android:id="@+id/btnnumberA"            android:layout_width="50dp"            android:layout_height="wrap_content"             android:layout_x="60dp" 			android:layout_y="230dp"            android:text="+" />            <Button            android:id="@+id/btnnumberS"            android:layout_width="50dp"            android:layout_height="wrap_content"               android:layout_x="120dp" 			android:layout_y="230dp"            android:text="-" />           <Button            android:id="@+id/btnnumberD"            android:layout_width="50dp"            android:layout_height="wrap_content"             android:layout_x="180dp" 			android:layout_y="230dp"            android:text="/" />   </AbsoluteLayout>


本程序后台java activity处理程序

import com.icss.myhello.R.array;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class FirstActivity extends Activity implements OnClickListener{	private Button btn0;	private Button btn1;	private Button btn2;	private Button btn3;	private Button btn4;	private Button btn5;	private Button btn6;	private Button btn7;	private Button btn8;	private Button btn9;	private Button btnM;	private Button btnD;	private Button btnA;	private Button btnS;		private Button btnR;	private Button btnC;		private Integer number1 = 0;	private Integer number2 = 0;	private char operation = ' ';		private EditText result ;									@Override	protected void onCreate(Bundle savedInstanceState) 	{		// TODO Auto-generated method stub		super.onCreate(savedInstanceState);		setContentView(R.layout.firstactivity);				btn0 = (Button) findViewById(R.id.btnnumber0);		btn1 = (Button) findViewById(R.id.btnnumber1);		btn2 = (Button) findViewById(R.id.btnnumber2);		btn3 = (Button) findViewById(R.id.btnnumber3);		btn4 = (Button) findViewById(R.id.btnnumber4);		btn5 = (Button) findViewById(R.id.btnnumber5);		btn6 = (Button) findViewById(R.id.btnnumber6);		btn7 = (Button) findViewById(R.id.btnnumber7);		btn8 = (Button) findViewById(R.id.btnnumber8);		btn9 = (Button) findViewById(R.id.btnnumber9);				btnA = (Button) findViewById(R.id.btnnumberA);		btnS = (Button) findViewById(R.id.btnnumberS);		btnM = (Button) findViewById(R.id.btnnumberM);		btnD = (Button) findViewById(R.id.btnnumberD);				btnR = (Button) findViewById(R.id.btnnumberR);		btnC = (Button) findViewById(R.id.btnnumberC);				result = (EditText)findViewById(R.id.computerfirstedt);				btn0.setOnClickListener(this);		btn1.setOnClickListener(this);		btn2.setOnClickListener(this);		btn3.setOnClickListener(this);		btn4.setOnClickListener(this);		btn5.setOnClickListener(this);		btn6.setOnClickListener(this);		btn7.setOnClickListener(this);		btn8.setOnClickListener(this);		btn9.setOnClickListener(this);				btnA.setOnClickListener(this);		btnD.setOnClickListener(this);		btnS.setOnClickListener(this);		btnM.setOnClickListener(this);						btnR.setOnClickListener(this);		btnC.setOnClickListener(this);															result.setText(number1.toString());			}		@Override	public void onClick(View view)	{		// TODO Auto-generated method stub		switch (view.getId()) {		case R.id.btnnumber0:			number1 = number1*10;			result.setText(number1.toString());						break;		case R.id.btnnumber1:			number1 = number1*10+1;			result.setText(number1.toString());			break;		case R.id.btnnumber2:			number1 = number1*10+2;			result.setText(number1.toString());			break;				case R.id.btnnumber3:			number1 = number1*10+3;			result.setText(number1.toString());			break;			case R.id.btnnumber4:			number1 = number1*10+4;			result.setText(number1.toString());			break;			case R.id.btnnumber5:			number1 = number1*10+5;			result.setText(number1.toString());			break;			case R.id.btnnumber6:			number1 = number1*10+6;			result.setText(number1.toString());			break;			case R.id.btnnumber7:			number1 = number1*10+7;			result.setText(number1.toString());			break;			case R.id.btnnumber8:			number1 = number1*10+8;			result.setText(number1.toString());			break;			case R.id.btnnumber9:			number1 = number1*10+9;			result.setText(number1.toString());			break;			case R.id.btnnumberA:			number2 = number1;			operation = '+';			number1 = 0;			result.setText(number1.toString());			break;			case R.id.btnnumberS:			number2 = number1;			operation = '-';			number1 = 0;			result.setText(number1.toString());			break;			case R.id.btnnumberM:			number2 = number1;			operation = '*';			number1 = 0;			result.setText(number1.toString());			break;			case R.id.btnnumberD:			number2 = number1;			operation = '/';			number1 = 0;			result.setText(number1.toString());			break;			case R.id.btnnumberC:			number1 = number2 = 0;			result.setText(number1.toString());			break;		case R.id.btnnumberR:			switch (operation) {			case '+':				number1 = number1+number2;				result.setText(number1.toString());				number1 = number2 = 0;				break;			case '-':				number1 = number1-number2;				result.setText(number1.toString());				number1 = number2 = 0;				break;					case '*':				number1 = number1*number2;				result.setText(number1.toString());				number1 = number2 = 0;				break;				case '/':				if(number2 != 0)				{					number1 = number1/number2;					result.setText(number1.toString());								}				else result.setText("error input");					number1 = number2 = 0;				break;				default:				break;			}			break;			default:			break;		}	}	}


 

andriod first app-computer