Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> andriod first app-computer

andriod first app-computer

編輯:關於Android編程

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文件




        
        
 


        


本程序後台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;
		}
	}
	
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved