專案目錄
- 一、專案概述
- 二、開發環境
- 三、詳細設計
- 1、布局設計
- 2、主函式
- 2.1、控制元件定義
- 2.2、控制元件系結
- 2.3、控制元件設定監聽器
- 2.4、控制元件設定點擊事件
- 2.5、運行結果
- 四、專案效果
- 五、專案總結
- 六、原始碼下載
一、專案概述
本次專案主要實作了簡單的計算器功能,包括加減乘除基本運算,還有小數點和清零功能,可以算得上是很好的Android界面和按鈕點擊事件學習實體,剛用模擬器顯示hello world的同學或者完全沒有接觸過Android的同學都可以直接上手,
二、開發環境
Android版本相對之前調整了一下,現在是4.2.1,當然版本不影響,代碼語法并不會變,只是gradle變了而已,到時候匯入我專案的時候改下gradle路徑就可以了,
如果遇到問題,可以參考這兩篇經驗:
快速解決Android專案的Gradle報錯問題
如何匯入gradle版本不同的Android專案

三、詳細設計
1、布局設計
在講布局前我們先介紹GridLayout(網格布局),來說下它的優點:
| 1、可以自己設定布局中組件的排列方式 |
|---|
| 2、可以自定義網格布局有多少行,多少列 |
|---|
| 3、可以直接設定組件位于某行某列 |
|---|
| 4、可以設定組件橫跨幾行或者幾列 |
|---|
下面我們看下計算器的layout代碼:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top">
<!--輸入的文本框-->
<EditText
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="100dp"
android:hint="請輸入數字"
android:textSize="30dp"/>
</GridLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="5"
android:columnCount="4"
android:layout_gravity="center|top">
<Button
android:id="@+id/btn1"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:layout_columnSpan="1"
android:text="1" />
<Button
android:id="@+id/btn2"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:text="2" />
<Button
android:id="@+id/btn3"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:text="3" />
<Button
android:id="@+id/divide"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:text="/" />
<Button
android:id="@+id/btn4"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:text="4" />
<Button
android:id="@+id/btn5"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:text="5" />
<Button
android:id="@+id/btn6"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:text="6" />
<Button
android:id="@+id/multi"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:text="*" />
<Button
android:id="@+id/btn7"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:text="7"
/>
<Button
android:id="@+id/btn8"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:text="8" />
<Button
android:id="@+id/btn9"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:text="9" />
<Button
android:id="@+id/sub"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:text="—" />
<Button
android:id="@+id/btn0"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:text="0" />
<Button
android:id="@+id/point"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:layout_columnSpan="1"
android:text="." />
<Button
android:id="@+id/clean"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:text="del" />
<Button
android:id="@+id/plus"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:layout_rowSpan="1"
android:text="+" />
<Button
android:id="@+id/equ"
android:layout_width="390dp"
android:layout_height="60dp"
android:layout_columnSpan="4"
android:layout_margin="5dp"
android:text="="
android:textSize="30dp"/>
</GridLayout>
</GridLayout>
代碼很簡單,每個Button設定了id,其中“=”按鈕橫跨一行,其他的都是直接添加的,默認每個組件都是占一行一列,
另外還有一點要注意的:我們通過android:layout_rowSpan與android:layout_columnSpan設定了組件橫跨多行或者多列的話;如果你要讓組件填滿橫越過的行或列的話,需要添加下面這個屬性: android:layout_gravity = “fill”,就像這個計算器顯示數字的部分(result輸入框),
我們來看下界面預覽:

2、主函式
2.1、控制元件定義
定義了很多button,分別代表0-9、加減乘除、小數點、等于和清零,這里變數名一定要簡潔易懂,不要隨意定義變數名,養成好習慣,
//運算子
private Button plus;//加號+
private Button sub;//減號-
private Button multi; //乘號*
private Button divide;// 除號/
private Button point; //小數點.
private Button equ; //等于=
private Button clean;//清除輸入框
2.2、控制元件系結
將剛剛定義的變數和已經設定好的控制元件通過id進行系結,這樣整個變數就代表了這個控制元件記住,寫Android一定要先寫layout,再寫Main函式,
plus = findViewById(R.id.plus);// +
sub = findViewById(R.id.sub);// -
multi = findViewById(R.id.multi);// *
divide = findViewById(R.id.divide); // /
point = findViewById(R.id.point);//小數點
equ = findViewById(R.id.equ);//=
clean = findViewById(R.id.clean);//清空
2.3、控制元件設定監聽器
將每個控制元件添加上點擊事件,這是最常用的方法,系統監聽到你的動作從而給出回應,
plus.setOnClickListener(this);
sub.setOnClickListener(this);
multi.setOnClickListener(this);
divide.setOnClickListener(this);
equ.setOnClickListener(this);
point.setOnClickListener(this);
clean.setOnClickListener(this);
2.4、控制元件設定點擊事件
這里我們通過變數clear_flag來判斷,它是清空標識,true就是清空,false就是未清空,
然后將數字、運算子、其他操作分類即可,
public void onClick(View view) {
//獲取文本內容
String input = editText.getText().toString();
switch (view.getId()){//選擇按鈕id
case R.id.btn0:
case R.id.btn1:
case R.id.btn2:
case R.id.btn3:
case R.id.btn4:
case R.id.btn5:
case R.id.btn6:
case R.id.btn7:
case R.id.btn8:
case R.id.btn9:
case R.id.point:
if(clear_flag){
clear_flag = false;
editText.setText("");//賦值為空
}
editText.setText(input + ((Button)view).getText());//結果集就為本身
break;
case R.id.plus:
case R.id.sub:
case R.id.multi:
case R.id.divide://加減乘除一起
if(clear_flag){
clear_flag = false;
input = "";
editText.setText("");
}
editText.setText(input + " " + ((Button)view).getText() + " ");
break;
case R.id.clean://清除輸入框
if(clear_flag){
clear_flag = false;
input = "";
editText.setText("");
}else if(input != null || !input.equals("")) {//如果獲取到的內容為空
editText.setText(input.substring(0, input.length() - 1));//結果集為空
}
break;
case R.id.equ://運算結果等于
getResult();//呼叫處理結果集的方法
break;
}
}
2.5、運行結果
這部分方法就是計算兩個數字運算結果,就是if-else陳述句,equals( )就是兩個物件比較,如果相同則為true,否則為false,contains( ) 就是包含關系,包含為ture,否則為false,
//運算結果的方法
private void getResult(){
String exp = editText.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型別
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) { //如果被除數是0
result = 0; //則結果是0
}
else {//否則執行正常是除法運算
result = d1 / d2;
}
}
if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {//如果是整數型別
int r = (int) result; //都是整形
editText.setText(r + "");
} else{
editText.setText(result + "");
}
}else if(!s1.equals("") && s2.equals("")){//如果是只輸入運算子前的數
editText.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;
} else if (op.equals("/")) {
result = 0;
}
if (!s1.contains(".") && !s2.contains(".")) {
int r = (int) result;
editText.setText(r + "");
} else{
editText.setText(result + "");
}
}else {
editText.setText("");
}
}
四、專案效果
1、打開模擬器運行,輸入兩個數字

2、按等于鍵,得出結果

3、選擇清空鍵,清除所有結果

五、專案總結
本次專案是比較基礎的考驗布局和控制元件的實體,對于初學者來說是非常好的example,可以作為Hello World之后第二個實體,認真消化里面的內容,以后做起來會非常快,
六、原始碼下載
需要原始碼學習的同學可以關注我的微信公眾號,回復:計算器,即可免費獲取原始碼,還有更多免費的Android專案等你來學習,送給大家一份精心整理的Android學習寶典:Android專案和Android教程總結(史詩級寶典)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/286283.html
標籤:java
