版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android如何實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小程序
這篇文章將為大家詳細(xì)講解有關(guān)Android如何實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小程序,在下覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。具體內(nèi)容如下目標(biāo)效果:
通過(guò)編寫(xiě)代碼,可以實(shí)現(xiàn)整數(shù)和小數(shù)的加減乘除運(yùn)算,以及刪除和清空的功能。1.頁(yè)面中Button使用的是線性布局,最外邊一個(gè)是父布局,第一行C,DEL,/,*為第一個(gè)子布局,第二行7,8,9,-為第二個(gè)子布局,第三行4,5,6,+為第三個(gè)子布局,第四五行為第四個(gè)子布局,第四個(gè)子布局中還有兩個(gè)相當(dāng)于是孫布局的級(jí)別,1,2,3為第一個(gè)孫布局,0和.為第二個(gè)孫布局,=在兩個(gè)孫布局之外第四個(gè)子布局以內(nèi)。因?yàn)橛?jì)算器的水平豎直排列十分鮮明,所以可以用線性布局,當(dāng)然也可以用表格布局來(lái)進(jìn)行排布。2.activity_main.xml頁(yè)面用于存放所有控件。activity_main.xml頁(yè)面:<LinearLayout
xmlns:android="/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"
//代表不能進(jìn)行鍵盤(pán)輸入
android:gravity="right"
//文字靠右邊
android:layout_gravity="center"
android:background="@drawable/white_bg"/>
<!--
設(shè)置輸入框的背景,為一個(gè)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è)置按鈕的背景,為一個(gè)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.等號(hào)按鈕和其余按鈕的背景及點(diǎn)擊效果不同。orange_select.xml頁(yè)面是等號(hào)按鈕的背景頁(yè)面。orange_select.xml頁(yè)面:<?xml
version="1.0"
encoding="utf-8"?>
<selector
xmlns:android="/apk/res/android"
>
<item
android:drawable="@drawable/orange_bg"
android:state_pressed="false"></item>
<!--
不點(diǎn)擊時(shí)背景為orange_bg.xml頁(yè)面的效果
-->
<item
android:drawable="@drawable/khaki_bg"
android:state_pressed="true"></item>
<!--
點(diǎn)擊時(shí)背景為khaki_bg.xml頁(yè)面的效果
-->
</selector>3.orange_bg.xml頁(yè)面:<?xml
version="1.0"
encoding="utf-8"?>
<shape
xmlns:android="/apk/res/android"
>
<corners
android:radius="5dp"/>
<solid
android:color="#ff9900"/>
</shape>4.khaki.xml頁(yè)面:<?xml
version="1.0"
encoding="utf-8"?>
<shape
xmlns:android="/apk/res/android"
>
<corners
android:radius="5dp"/>
<solid
android:color="#cc6600"/>
</shape>5.white_select.xml頁(yè)面是其余按鈕的背景頁(yè)面。white_select頁(yè)面:<?xml
version="1.0"
encoding="utf-8"?>
<selector
xmlns:android="/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頁(yè)面:<?xml
version="1.0"
encoding="utf-8"?>
<shape
xmlns:android="/apk/res/android"
>
<corners
android:radius="5dp"/>
<solid
android:color="#ffffff"/>
</shape>7.gray_bg.xml頁(yè)面:<?xml
version="1.0"
encoding="utf-8"?>
<shape
xmlns:android="/apk/res/android"
>
<corners
android:radius="5dp"/>
<solid
android:color="#cccccc"/>
</shape>8.MainActivity.java處理按鈕的點(diǎn)擊事件以及數(shù)值運(yùn)算。MainActivity.java頁(yè)面: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;
//實(shí)例輸入框
private
Button
btOne;
//實(shí)例所有按鈕
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)識(shí),當(dāng)用戶點(diǎn)擊等號(hào)完成一次運(yùn)算時(shí)進(jìn)行清空
@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è)置點(diǎn)擊事件,因?yàn)镸ainActivity已經(jīng)實(shí)現(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類(lèi)型
switch(v.getId()){
//判斷點(diǎn)擊按鈕的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());
//點(diǎn)擊數(shù)字和小數(shù)點(diǎn)直接顯示內(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時(shí)進(jìn)入if語(yǔ)句,并可以清空,代表用戶點(diǎn)擊了等于號(hào)完成一次運(yùn)算,并使clear_flag變成了true
}
etInput.setText(etinput+"
"+((Button)v).getText()+"
");
//點(diǎn)擊運(yùn)算符也是直接顯示,但是為了后邊運(yùn)算要在運(yùn)算符兩側(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è)置輸入框?yàn)榭?/p>
break;
case
R.id.btEqu:
getResult();
//點(diǎn)擊等號(hào)調(diào)用getResult()方法
break;
}
}
public
void
getResult(){
String
exp=etInput.getText().toString();
//獲取輸入框的內(nèi)容
if(exp==null||exp.equals("")){
return;
}
if(!exp.contains("
")){
//判斷輸入框是否包含空格,也就是沒(méi)有點(diǎn)擊運(yùn)算符
return;
}
if(clear_flag){
//點(diǎn)擊等號(hào)clear_flag為true,當(dāng)再點(diǎn)擊別的數(shù)字或運(yùn)算符時(shí)才會(huì)變?yōu)閒alse,如果連續(xù)點(diǎn)擊等號(hào),則第二次點(diǎn)擊無(wú)效,直接返回
clear_flag=false;
return;
}
clear_flag=true;
double
result=0;
String
s1=exp.substring(0,exp.indexOf("
"));
//運(yùn)算符前面的字
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 網(wǎng)吧包月上網(wǎng)合同樣本
- 動(dòng)力電池訂購(gòu)合同格式
- 加氣塊產(chǎn)品供應(yīng)商協(xié)議
- 管道配件采購(gòu)合同范本
- 自來(lái)水管道安裝工程合同模板
- 牛犢選購(gòu)協(xié)議范例
- 臨時(shí)電力服務(wù)招標(biāo)文件
- 商業(yè)貸款合同保證條款
- 股權(quán)轉(zhuǎn)讓協(xié)議涉及的法律法規(guī)
- 信用卡借款協(xié)議范本
- 航海英語(yǔ)大副批注
- 新高考形勢(shì)下高中生物學(xué)科核心素養(yǎng)培養(yǎng)探析(精編版)
- 電梯檢驗(yàn)員個(gè)人總結(jié)五篇
- 利達(dá)信TK832-A型數(shù)字集團(tuán)電話說(shuō)明書(shū)
- 視頻監(jiān)控項(xiàng)目日常巡檢表
- 北師大版小學(xué)四年級(jí)數(shù)學(xué)上冊(cè)第四單元《運(yùn)算律》單元教學(xué)設(shè)計(jì)
- 中考英語(yǔ)聽(tīng)說(shuō)之口頭作文
- KYN28A-12說(shuō)明書(shū)模板
- 貓的介紹-英文PPT_圖文.ppt
- 腹股溝疝臨床路徑表單
- 學(xué)校違紀(jì)萬(wàn)能檢討書(shū)1000字
評(píng)論
0/150
提交評(píng)論