Android實現(xiàn)簡易計算器小程序
本文實例為大家分享了Android實現(xiàn)簡易計算器小程序的具體代碼,供大家參考,具體內容如下
目標效果:
通過編寫代碼,可以實現(xiàn)整數(shù)和小數(shù)的加減乘除運算,以及刪除和清空的功能。
1.頁面中Button使用的是線性布局,最外邊一個是父布局,第一行C,DEL,/,*為第一個子布局,第二行7,8,9,-為第二個子布局,第三行4,5,6,+為第三個子布局,第四五行為第四個子布局,第四個子布局中還有兩個相當于是孫布局的級別,1,2,3為第一個孫布局,0和.為第二個孫布局,=在兩個孫布局之外第四個子布局以內。因為計算器的水平豎直排列十分鮮明,所以可以用線性布局,當然也可以用表格布局來進行排布。
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"/> <!-- 設置輸入框的背景,為一個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" //設置按鈕的背景,為一個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; //清空標識,當用戶點擊等號完成一次運算時進行清空
@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); //設置點擊事件,因為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(); //獲取輸入框中的內容并轉化為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ù)點直接顯示內容
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(""); //當clear_flag為true時進入if語句,并可以清空,代表用戶點擊了等于號完成一次運算,并使clear_flag變成了true
}
etInput.setText(etinput+" "+((Button)v).getText()+" "); //點擊運算符也是直接顯示,但是為了后邊運算要在運算符兩側加上空格
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)); //如果輸入框內容不為空,則去掉最后一位
}
break;
case R.id.btClear:
clear_flag=false;
etinput="";
etInput.setText(""); //直接設置輸入框為空
break;
case R.id.btEqu:
getResult(); //點擊等號調用getResult()方法
break;
}
}
public void getResult(){
String exp=etInput.getText().toString(); //獲取輸入框的內容
if(exp==null||exp.equals("")){
return;
}
if(!exp.contains(" ")){ //判斷輸入框是否包含空格,也就是沒有點擊運算符
return;
}
if(clear_flag){ //點擊等號clear_flag為true,當再點擊別的數(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); //將字符串轉換為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ù)都是整形,那么結果就需要顯示為整數(shù)
int r=(int)result; //將String型計算結果強制轉換為整形
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ù)默認為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ù)的加減乘除。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android連接MySQL數(shù)據(jù)庫詳細教程
在Android應用程序中連接 MySQL 數(shù)據(jù)庫可以幫助開發(fā)人員實現(xiàn)更豐富的數(shù)據(jù)管理功能,本教程將介紹如何在Android應用程序中使用低版本的MySQL Connector/J驅動程序來連接MySQL數(shù)據(jù)庫,需要的朋友可以參考下2023-05-05
kotlin Standard中的內聯(lián)函數(shù)示例詳解
這篇文章主要給大家介紹了關于kotlin Standard中內聯(lián)函數(shù)的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用kotlin具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-08-08
Input系統(tǒng)之InputReader處理合成事件詳解
這篇文章主要為大家介紹了Input系統(tǒng)之InputReader處理合成事件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android開發(fā)之Animations動畫用法實例詳解
這篇文章主要介紹了Android開發(fā)之Animations動畫用法,結合實例形式詳細分析了Animations動畫的類型、組成、模式及對應的使用技巧,需要的朋友可以參考下2016-02-02
詳解Android開發(fā)錄音和播放音頻的步驟(動態(tài)獲取權限)
這篇文章主要介紹了詳解Android開發(fā)錄音和播放音頻的步驟(動態(tài)獲取權限),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08

