簡(jiǎn)介
計(jì)算器的應(yīng)用在我們的日常生活中十分普遍炕倘,對(duì)于對(duì)安卓開(kāi)發(fā)有興趣的朋友,了解并掌握它的開(kāi)發(fā)是很重要的翰撑。對(duì)于入門(mén)新手來(lái)說(shuō)罩旋,此小項(xiàng)目也是非常有用的,一是熟悉開(kāi)發(fā)工具的使用眶诈,二是逐步入門(mén)Android開(kāi)發(fā)涨醋。接下來(lái)我將逐步總結(jié)一下該應(yīng)用的實(shí)現(xiàn)過(guò)程。
開(kāi)發(fā)工具
Android Studio
開(kāi)發(fā)環(huán)境
1)JDK(Java Development Kit)JDK是Java語(yǔ)言的軟件開(kāi)發(fā)工具包逝撬,主要用于移動(dòng)設(shè)備浴骂、嵌入設(shè)備上的java應(yīng)用程序。
2)SDK(software development kit) SDK是軟件開(kāi)發(fā)工具包宪潮。 被軟件開(kāi)發(fā)工程師用于為特定的軟件包溯警、軟件框架、硬件平臺(tái)狡相、操作系統(tǒng)等建立應(yīng)用軟件的開(kāi)發(fā)工具的集合
實(shí)現(xiàn)過(guò)程
Android自帶的硬件類(lèi)Camera在API21(5.0)之后被拋棄了梯轻,出現(xiàn)了Camera2,在這可以用CameraManager來(lái)打開(kāi)尽棕,實(shí)現(xiàn)硬件閃光燈的開(kāi)啟與關(guān)閉喳挑。
實(shí)現(xiàn)過(guò)程
分析
我認(rèn)為每個(gè)開(kāi)發(fā)android應(yīng)用前,我們肯定是考慮UI設(shè)計(jì)與后臺(tái)的邏輯處理這倆部分滔悉。在UI設(shè)計(jì)中蟀悦,要注意到程序的美觀以及可行性,后臺(tái)對(duì)相應(yīng)的操作進(jìn)行處理氧敢。
1.新建項(xiàng)目
使用Android Studio新建一個(gè)空項(xiàng)目。
2.添加布局
在這里询张,我運(yùn)用的是嵌套的線性布局孙乖,布局中再放置按鈕。UI是之前剛學(xué)Android做的,為了方便沒(méi)有去更改唯袄,其實(shí)可以運(yùn)用TableLayout來(lái)做弯屈,布局會(huì)更美觀。
效果圖如下:
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="right"
android:editable="false" />
//設(shè)置文本框不能編輯
//match parent 充滿父類(lèi)容器
<LinearLayout
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
>
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/btn_clear"
android:layout_margin="8dp"
android:text="C"
android:textSize="20dp"
/>
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/btn_del"
android:layout_margin="8dp"
android:text="DEL"
android:textSize="20dp"
/>
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/btn_divide"
android:layout_margin="8dp"
android:text="÷"
android:textSize="20dp"
/>
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/btn_multipy"
android:layout_margin="8dp"
android:text="×"
android:textSize="20dp"
/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_7"
android:layout_width="75dp"
android:layout_height="75dp"
android:text="7"
android:layout_gravity="right"
android:layout_margin="8dp"
android:textSize="20dp" />
<Button
android:id="@+id/btn_8"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="right"
android:layout_margin="8dp"
android:text="8"
android:textSize="20dp" />
<Button
android:id="@+id/btn_9"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="right"
android:layout_margin="8dp"
android:text="9"
android:textSize="20dp" />
<Button
android:id="@+id/btn_minus"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="right"
android:layout_margin="8dp"
android:text="-"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_4"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="right"
android:text="4"
android:layout_margin="8dp"
android:textSize="20dp" />
<Button
android:id="@+id/btn_5"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="right"
android:layout_margin="8dp"
android:text="5"
android:textSize="20dp" />
<Button
android:id="@+id/btn_6"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="right"
android:layout_margin="8dp"
android:text="6"
android:textSize="20dp" />
<Button
android:id="@+id/btn_plus"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="right"
android:layout_margin="8dp"
android:text="+"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_gravity="left"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_1"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="right"
android:text="1"
android:layout_margin="8dp"
android:textSize="20dp" />
<Button
android:id="@+id/btn_2"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="right"
android:layout_margin="8dp"
android:text="2"
android:textSize="20dp" />
<Button
android:id="@+id/btn_3"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="right"
android:layout_margin="10dp"
android:text="3"
android:textSize="20dp" />
<Button
android:id="@+id/btn_a"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="right"
android:text="="
android:layout_margin="8dp"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="left"
android:orientation="horizontal">
<Button
android:id="@+id/btn_0"
android:layout_width="166dp"
android:layout_height="75dp"
android:layout_gravity="right"
android:layout_margin="10dp"
android:text="0"
android:textSize="20dp" />
<Button
android:id="@+id/btn_b"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="right"
android:layout_margin="8dp"
android:text="."
android:textSize="20dp" />
<Button
android:id="@+id/button"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="right"
android:layout_margin="8dp"
android:text="空"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
3.MainActivity的編寫(xiě)
這部分的話就是對(duì)各個(gè)按鈕點(diǎn)擊事件的處理了恋拷,也就是對(duì)鍵入的數(shù)字進(jìn)行運(yùn)算资厉,判斷之類(lèi)的,由于是出學(xué)者蔬顾,我只是做了簡(jiǎn)單的處理宴偿,還有一些BUG沒(méi)能處理好,當(dāng)然诀豁,不影響簡(jiǎn)單運(yùn)算的使用窄刘。
代碼如下:
package com.zewei_w.caculator;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements View.OnClickListener {
Button btn_1; Button btn_0;
Button btn_2; Button btn_7;
Button btn_3; Button btn_8; Button btn_plus;
Button btn_4; Button btn_9; Button btn_minus;
Button btn_5; Button btn_a; Button btn_multipy;
Button btn_divide; Button btn_del;
Button btn_6; Button btn_b;
Button btn_clear;
EditText et_input;
boolean clear_flag;
//clearflag默認(rèn)為false
// 定義這些按鈕和文本框
//通過(guò)接口方式實(shí)現(xiàn)事件的監(jiān)聽(tīng)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_0=(Button) findViewById(R.id.btn_0);
btn_1=(Button) findViewById(R.id.btn_1);
btn_2=(Button) findViewById(R.id.btn_2);
btn_3=(Button) findViewById(R.id.btn_3);
btn_4=(Button) findViewById(R.id.btn_4); btn_9=(Button) findViewById(R.id.btn_9);
btn_5=(Button) findViewById(R.id.btn_5);
btn_6=(Button) findViewById(R.id.btn_6);
btn_7=(Button) findViewById(R.id.btn_7);
btn_8=(Button) findViewById(R.id.btn_8);
btn_clear=(Button) findViewById(R.id.btn_clear);
btn_del=(Button) findViewById(R.id.btn_del);
btn_divide=(Button) findViewById(R.id.btn_divide);
btn_multipy=(Button) findViewById(R.id.btn_multipy);
btn_minus=(Button) findViewById(R.id.btn_minus);
btn_plus=(Button) findViewById(R.id.btn_plus);
btn_a=(Button) findViewById(R.id.btn_a);
btn_b=(Button) findViewById(R.id.btn_b);
//實(shí)例化按鈕
// 返回的是view類(lèi)型,需要轉(zhuǎn)換成button類(lèi)型
et_input=(EditText)findViewById(R.id.et_input);
btn_0.setOnClickListener(this);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_4.setOnClickListener(this);
btn_5.setOnClickListener(this);
btn_6.setOnClickListener(this);
btn_7.setOnClickListener(this);
btn_8.setOnClickListener(this);
btn_9.setOnClickListener(this);
btn_a.setOnClickListener(this);
btn_b.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_multipy.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_plus.setOnClickListener(this);
btn_clear.setOnClickListener(this);
//設(shè)置按鈕的點(diǎn)擊事件
//大概是獲取點(diǎn)擊事件
}
@Override
public void onClick(View v) {
String str = et_input.getText().toString();
switch (v.getId()) {
case R.id.btn_0:
case R.id.btn_1:
case R.id.btn_2:
case R.id.btn_3:
case R.id.btn_4:
case R.id.btn_5:
case R.id.btn_6:
case R.id.btn_7:
case R.id.btn_8:
case R.id.btn_9:
case R.id.btn_b:
if(clear_flag)
{
clear_flag=false;
str="";
et_input.setText("");
}
et_input.setText(str + ((Button) v).getText());
break;
case R.id.btn_plus:
case R.id.btn_multipy:
case R.id.btn_divide:
case R.id.btn_minus:
if(clear_flag)
{
clear_flag=false;
str="";
et_input.setText("");
}
//判斷用
et_input.setText(str + " " + ((Button) v).getText() + " ");
break;
case R.id.btn_clear:
if(clear_flag)
{
clear_flag=false;
str="";
et_input.setText("");
}
et_input.setText("");
break;
case R.id.btn_del:
if(clear_flag)
{
clear_flag=false;
str="";
et_input.setText("");
}
if (str != null && !str.equals("")) {
et_input.setText(str.substring(0, str.length() - 1));
}
break;
case R.id.btn_a:
getresult();
break;
}
}
private void getresult()
{ String exp=et_input.getText().toString();
if(exp==null||exp.equals(""))
{
return;
}
if(!exp.contains(" "))
{
return;
}
if(clear_flag)
{
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);
String s2=exp.substring(exp.indexOf(" ")+3);
if(!s1.equals("")&&!s2.equals("")) {
double d1= Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
//強(qiáng)制類(lèi)型轉(zhuǎn)換
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(d1==0)
{result=0;}else result=d1/d2;
}
if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")){
int r = (int)result;
et_input.setText(r + "");
}
else
{et_input.setText(result + "");}
}else if(!s1.equals("")&&s2.equals(""))
{
et_input.setText(exp);
}else if(s1.equals("")&&!s2.equals(""))
{ double d2= Double.parseDouble(s2);
if (op.equals("+")) {
result = 0 + d2;
} else if (op.equals("-")) {
result = 0 - d2;
} else if (op.equals("×")) {
result=0*d2;
} else if (op.equals("÷")) {
result=0;
}
}
else {
et_input.setText("");
}
}
}
代碼中有適當(dāng)?shù)淖⑨屨f(shuō)明舷胜。
4.運(yùn)行程序
運(yùn)行程序娩践,調(diào)試錯(cuò)誤,可更改APP名字烹骨,圖標(biāo)等
這里我在AndroidMa'nifest.xml文件中更改了主題
效果如果:
(編寫(xiě)手機(jī)截圖)
可拓展指出:
1.程序界面美化
2.功能完善化-檢查BUG
3.增加運(yùn)算功能
個(gè)人總結(jié)與感悟:
這個(gè)應(yīng)用同手電筒一樣翻伺,都是入門(mén)級(jí)的,但是我仍舊極力推薦沮焕,對(duì)于初學(xué)者非常有用吨岭,在編寫(xiě)過(guò)程中,學(xué)到了不少知識(shí)點(diǎn)遇汞,同時(shí)我發(fā)現(xiàn)未妹,隨著Android的不斷更新,很多接口以及寫(xiě)法都有些許改變空入,所以我認(rèn)為络它,遇到了問(wèn)題,查找百度歪赢、谷歌等固然可以化戳,但一定不能照搬,因?yàn)楹芏鄦?wèn)題其實(shí)是很久之前的埋凯,而很多知識(shí)的用法卻一直在更新改變点楼,所以要漸漸地學(xué)會(huì)去研讀開(kāi)發(fā)文檔,找到用法白对,這點(diǎn)接下來(lái)我會(huì)開(kāi)始去做掠廓,因?yàn)榇_實(shí)有很多的不同,比如在主題的運(yùn)用這點(diǎn)甩恼,因?yàn)橛梅▎?wèn)題蟀瞧,我就被干擾了不久沉颂。
編者剛開(kāi)始寫(xiě)簡(jiǎn)書(shū)不久,也是Android的初學(xué)者悦污,很多問(wèn)題還不懂铸屉,寫(xiě)下這些,對(duì)一部分讀者有用自然是好切端,更多地是想記錄下自己學(xué)習(xí)的過(guò)程彻坛,逐步提高自己,當(dāng)然踏枣,有不好的地方昌屉,非常歡迎高手指點(diǎn)批評(píng),非常歡迎相互討論椰于,相互提高怠益!
今天到這里,謝謝瘾婿!