Android實現(xiàn)簡易計算器小程序
本文實例為大家分享了Android實現(xiàn)簡易計算器小程序的具體代碼,供大家參考,具體內(nèi)容如下
目標(biāo)效果:
通過編寫代碼,可以實現(xiàn)整數(shù)和小數(shù)的加減乘除運算,以及刪除和清空的功能。
1.頁面中Button使用的是線性布局,最外邊一個是父布局,第一行C,DEL,/,*為第一個子布局,第二行7,8,9,-為第二個子布局,第三行4,5,6,+為第三個子布局,第四五行為第四個子布局,第四個子布局中還有兩個相當(dāng)于是孫布局的級別,1,2,3為第一個孫布局,0和.為第二個孫布局,=在兩個孫布局之外第四個子布局以內(nèi)。因為計算器的水平豎直排列十分鮮明,所以可以用線性布局,當(dāng)然也可以用表格布局來進行排布。
2.activity_main.xml頁面用于存放所有控件。
activity_main.xml頁面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_marginTop="40dp" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/etInput" android:layout_width="310dp" android:layout_height="60dip" android:editable="false" //代表不能進行鍵盤輸入 android:gravity="right" //文字靠右邊 android:layout_gravity="center" android:background="@drawable/white_bg"/> <!-- 設(shè)置輸入框的背景,為一個xml文件 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:gravity="center_horizontal" android:orientation="horizontal" > <!-- 代表水平排布 --> <Button android:id="@+id/btClear" android:background="@drawable/white_select" //設(shè)置按鈕的背景,為一個xml文件 android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="C" /> <Button android:id="@+id/btDel" android:background="@drawable/white_select" android:layout_marginLeft="5dp" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="DEL" /> <Button android:id="@+id/btDivide" android:background="@drawable/white_select" android:layout_marginLeft="5dp" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="/" /> <Button android:id="@+id/btMul" android:background="@drawable/white_select" android:layout_marginLeft="5dp" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="*" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:gravity="center_horizontal" android:orientation="horizontal" > <Button android:id="@+id/btSeven" android:background="@drawable/white_select" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="7" /> <Button android:id="@+id/btEight" android:background="@drawable/white_select" android:layout_marginLeft="5dp" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="8" /> <Button android:id="@+id/btNine" android:background="@drawable/white_select" android:layout_marginLeft="5dp" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="9" /> <Button android:id="@+id/btJian" android:background="@drawable/white_select" android:layout_marginLeft="5dp" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="-" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:gravity="center_horizontal" android:orientation="horizontal" > <Button android:id="@+id/btFour" android:background="@drawable/white_select" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="4" /> <Button android:id="@+id/btFive" android:background="@drawable/white_select" android:layout_marginLeft="5dp" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="5" /> <Button android:id="@+id/btSix" android:background="@drawable/white_select" android:layout_marginLeft="5dp" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="6" /> <Button android:id="@+id/btJia" android:background="@drawable/white_select" android:layout_marginLeft="5dp" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="+" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:gravity="center_horizontal" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btOne" android:background="@drawable/white_select" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="1" /> <Button android:id="@+id/btTwo" android:background="@drawable/white_select" android:layout_marginLeft="5dp" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="2" /> <Button android:id="@+id/btThree" android:background="@drawable/white_select" android:layout_marginLeft="5dp" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="3" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:orientation="horizontal" > <Button android:id="@+id/btZero" android:background="@drawable/white_select" android:layout_width="155dp" android:layout_height="60dp" android:textSize="20sp" android:text="0" /> <Button android:id="@+id/btPoint" android:background="@drawable/white_select" android:layout_marginLeft="5dp" android:layout_width="75dp" android:layout_height="60dp" android:textSize="20sp" android:text="." /> </LinearLayout> </LinearLayout> <Button android:layout_width="75dp" android:background="@drawable/orange_select" android:layout_height="125dp" android:text="=" android:layout_marginLeft="5dp" android:textSize="20sp" android:gravity="center" android:id="@+id/btEqu"> </Button> </LinearLayout> </LinearLayout>
2.等號按鈕和其余按鈕的背景及點擊效果不同。orange_select.xml頁面是等號按鈕的背景頁面。
orange_select.xml頁面:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/orange_bg" android:state_pressed="false"></item> <!-- 不點擊時背景為orange_bg.xml頁面的效果 --> <item android:drawable="@drawable/khaki_bg" android:state_pressed="true"></item> <!-- 點擊時背景為khaki_bg.xml頁面的效果 --> </selector>
3.orange_bg.xml頁面:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="5dp"/> <solid android:color="#ff9900"/> </shape>
4.khaki.xml頁面:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="5dp"/> <solid android:color="#cc6600"/> </shape>
5.white_select.xml頁面是其余按鈕的背景頁面。
white_select頁面:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/white_bg" android:state_pressed="false"></item> <item android:drawable="@drawable/gray_bg" android:state_pressed="true"></item> </selector>
6.white_bg.xml頁面:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="5dp"/> <solid android:color="#ffffff"/> </shape>
7.gray_bg.xml頁面:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="5dp"/> <solid android:color="#cccccc"/> </shape>
8.MainActivity.java處理按鈕的點擊事件以及數(shù)值運算。
MainActivity.java頁面:
package com.example.jisuanqi;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
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 implements OnClickListener{
private EditText etInput; //實例輸入框
private Button btOne; //實例所有按鈕
private Button btTwo;
private Button btThree;
private Button btFour;
private Button btFive;
private Button btSix;
private Button btSeven;
private Button btEight;
private Button btNine;
private Button btZero;
private Button btPoint;
private Button btJia;
private Button btJian;
private Button btMul;
private Button btDivide;
private Button btEqu;
private Button btClear;
private Button btDel;
boolean clear_flag; //清空標(biāo)識,當(dāng)用戶點擊等號完成一次運算時進行清空
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etInput=(EditText) findViewById(R.id.etInput); //獲取輸入框的id
btOne=(Button) findViewById(R.id.btOne); //獲取所有按鈕的id
btTwo=(Button) findViewById(R.id.btTwo);
btThree=(Button) findViewById(R.id.btThree);
btFour=(Button) findViewById(R.id.btFour);
btFive=(Button) findViewById(R.id.btFive);
btSix=(Button) findViewById(R.id.btSix);
btSeven=(Button) findViewById(R.id.btSeven);
btEight=(Button) findViewById(R.id.btEight);
btNine=(Button) findViewById(R.id.btNine);
btZero=(Button) findViewById(R.id.btZero);
btPoint=(Button) findViewById(R.id.btPoint);
btJia=(Button) findViewById(R.id.btJia);
btJian=(Button) findViewById(R.id.btJian);
btMul=(Button) findViewById(R.id.btMul);
btDivide=(Button) findViewById(R.id.btDivide);
btEqu=(Button) findViewById(R.id.btEqu);
btClear=(Button) findViewById(R.id.btClear);
btDel=(Button) findViewById(R.id.btDel);
btOne.setOnClickListener(this); //設(shè)置點擊事件,因為MainActivity已經(jīng)實現(xiàn)了OnClickListener接口,所以只需要參數(shù)只需要傳this
btTwo.setOnClickListener(this);
btThree.setOnClickListener(this);
btFour.setOnClickListener(this);
btFive.setOnClickListener(this);
btSix.setOnClickListener(this);
btSeven.setOnClickListener(this);
btEight.setOnClickListener(this);
btNine.setOnClickListener(this);
btZero.setOnClickListener(this);
btPoint.setOnClickListener(this);
btJia.setOnClickListener(this);
btJian.setOnClickListener(this);
btMul.setOnClickListener(this);
btDivide.setOnClickListener(this);
btEqu.setOnClickListener(this);
btClear.setOnClickListener(this);
btDel.setOnClickListener(this);
}
@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 void onClick(View v) {
// TODO Auto-generated method stub
String etinput=etInput.getText().toString(); //獲取輸入框中的內(nèi)容并轉(zhuǎn)化為String類型
switch(v.getId()){ //判斷點擊按鈕的id
case R.id.btZero:
case R.id.btOne:
case R.id.btTwo:
case R.id.btThree:
case R.id.btFour:
case R.id.btFive:
case R.id.btSix:
case R.id.btSeven:
case R.id.btEight:
case R.id.btNine:
case R.id.btPoint:
if(clear_flag){
clear_flag=false;
etinput="";
etInput.setText("");
}
etInput.setText(etinput+((Button)v).getText()); //點擊數(shù)字和小數(shù)點直接顯示內(nèi)容
break;
case R.id.btJia:
case R.id.btJian:
case R.id.btMul:
case R.id.btDivide:
if(clear_flag){
clear_flag=false;
etinput="";
etInput.setText(""); //當(dāng)clear_flag為true時進入if語句,并可以清空,代表用戶點擊了等于號完成一次運算,并使clear_flag變成了true
}
etInput.setText(etinput+" "+((Button)v).getText()+" "); //點擊運算符也是直接顯示,但是為了后邊運算要在運算符兩側(cè)加上空格
break;
case R.id.btDel:
if(clear_flag){
clear_flag=false;
etinput="";
etInput.setText("");
}else if(etinput!=null&&!etinput.equals("")){
etInput.setText(etinput.substring(0,etinput.length()-1)); //如果輸入框內(nèi)容不為空,則去掉最后一位
}
break;
case R.id.btClear:
clear_flag=false;
etinput="";
etInput.setText(""); //直接設(shè)置輸入框為空
break;
case R.id.btEqu:
getResult(); //點擊等號調(diào)用getResult()方法
break;
}
}
public void getResult(){
String exp=etInput.getText().toString(); //獲取輸入框的內(nèi)容
if(exp==null||exp.equals("")){
return;
}
if(!exp.contains(" ")){ //判斷輸入框是否包含空格,也就是沒有點擊運算符
return;
}
if(clear_flag){ //點擊等號clear_flag為true,當(dāng)再點擊別的數(shù)字或運算符時才會變?yōu)閒alse,如果連續(xù)點擊等號,則第二次點擊無效,直接返回
clear_flag=false;
return;
}
clear_flag=true;
double result=0;
String s1=exp.substring(0,exp.indexOf(" ")); //運算符前面的字符串
String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2); //運算符,是根據(jù)運算符前邊的空格計算的
String s2=exp.substring(exp.indexOf(" ")+3); //運算符后邊的字符串
if(!s1.equals("")&&!s2.equals("")){
double d1=Double.parseDouble(s1); //將字符串轉(zhuǎn)換為double類型
double d2=Double.parseDouble(s2);
if(op.equals("+")){
result=d1+d2;
}else if(op.equals("-")){
result=d1-d2;
}else if(op.equals("*")){
result=d1*d2;
}else if(op.equals("/")){
if(d2==0){ //判斷除數(shù)為0的情況
result=0;
}else{
result=d1/d2;
}
}
if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("/")){ //如果兩個數(shù)都是整形,那么結(jié)果就需要顯示為整數(shù)
int r=(int)result; //將String型計算結(jié)果強制轉(zhuǎn)換為整形
etInput.setText(r+"");
}else{
etInput.setText(result+"");
}
}else if(!s1.equals("")&&s2.equals("")){ //如果用戶輸入一個數(shù)字就點擊運算符,那么將不計算
etInput.setText(exp);
}else if(s1.equals("")&&!s2.equals("")){ //如果一上來就點擊運算符并輸入第二個數(shù),那么第一個數(shù)默認(rèn)為0
double d2=Double.parseDouble(s2);
if(op.equals("+")){
result=0+d2;
}else if(op.equals("-")){
result=0-d2;
}else if(op.equals("*")){
result=0;
}else if(op.equals("/")){
result=0;
}
if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("/")){
int r=(int)result;
etInput.setText(r+"");
}else{
etInput.setText(result+"");
}
}else{
etInput.setText("");
}
}
}
9.程序完成就可以運行了。因為是簡易計算器,所以還不能進行連續(xù)的加減乘除。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實現(xiàn)網(wǎng)絡(luò)多線程斷點續(xù)傳下載功能
這篇文章主要為大家詳細介紹了Android實現(xiàn)網(wǎng)絡(luò)多線程斷點續(xù)傳下載功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
Android開發(fā)實現(xiàn)高仿優(yōu)酷的客戶端圖片左右滑動切換功能實例【附源碼下載】
這篇文章主要介紹了Android開發(fā)實現(xiàn)高仿優(yōu)酷的客戶端圖片左右滑動切換功能,結(jié)合實例形式分析了Android基于ViewPager實現(xiàn)圖片切換效果的相關(guān)操作技巧,并附帶完整工程源碼供讀者下載參考,需要的朋友可以參考下2017-11-11
android自定義控件和自定義回調(diào)函數(shù)步驟示例
這篇文章主要介紹了android自定義控件步驟示例,包括為View類增加屬性、響應(yīng)用戶消息、自定義回調(diào)函數(shù)等方法2014-01-01
Android用MVP實現(xiàn)一個簡單的類淘寶訂單頁面的示例
本篇文章主要介紹了Android用MVP實現(xiàn)一個簡單的類淘寶訂單頁面的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Android開發(fā)中Google為什么不讓用Handler的runWithScissors()
這篇文章主要介紹了Android開發(fā)中Google為什么不讓用Handler的runWithScissors(),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09

