??Android Studio 專(zhuān)欄正在持續(xù)更新中,案例的原理圖解析、各種模塊分析??這里都有哦,同時(shí)也歡迎大家訂閱專(zhuān)欄,獲取更多詳細(xì)信息哦???
?個(gè)人主頁(yè):零小唬的博客主頁(yè)
??歡迎大家 ??點(diǎn)贊 ??評(píng)論 ??收藏
?作者簡(jiǎn)介:20級(jí)計(jì)算機(jī)專(zhuān)業(yè)學(xué)生一枚,來(lái)自寧夏,可能會(huì)去做大前端,目前還在努力學(xué)習(xí)并記錄博客中??
??本系列專(zhuān)欄: Android Studio
??希望本文對(duì)你在學(xué)習(xí)Android Studio的過(guò)程中有所幫助,如有不足請(qǐng)指正一起學(xué)習(xí),一起進(jìn)步??
?座右銘:只要你還愿意努力,世界一定會(huì)給你驚喜
實(shí)驗(yàn)說(shuō)明
設(shè)計(jì)一個(gè)移動(dòng)端的計(jì)算器App。
- 要求向一般計(jì)算器一樣可以進(jìn)行加、減、乘、除、清零操作。
- 按下等號(hào)顯示區(qū)將顯示結(jié)果,其中顯示區(qū)在輸入時(shí)將實(shí)時(shí)顯示來(lái)自虛擬鍵盤(pán)胡輸入信息。
- 按鍵需要添加交互式功能,其中包括當(dāng)按下虛擬鍵盤(pán)時(shí),被按下胡按鍵背景變亮,當(dāng)離開(kāi)按鍵后,按鍵背景正常。
效果圖如下(僅供參考, 布局不需要一致,JAVA計(jì)算器視頻已上傳到“課堂派”資料區(qū),供大家參考):![]()
???????注:本實(shí)驗(yàn)來(lái)自帥帥的作業(yè)實(shí)驗(yàn)
簡(jiǎn)單的移動(dòng)端計(jì)算器 App 的設(shè)計(jì)和實(shí)現(xiàn)的步驟和要點(diǎn):
- 界面設(shè)計(jì)和布局
??根據(jù)要求,需要
創(chuàng)建一個(gè)包括操作區(qū)和顯示區(qū)的界面
。操作區(qū)應(yīng)該包括虛擬鍵盤(pán)和清零按鈕,而顯示區(qū)應(yīng)該用于顯示輸入和輸出??梢允褂?Android 提供的 UI 元素來(lái)設(shè)計(jì)和布局界面,如 LinearLayout、TextView 和 Button。
- 實(shí)現(xiàn)基本計(jì)算功能
??要實(shí)現(xiàn)基本的計(jì)算功能,可以創(chuàng)建一個(gè)類(lèi),該類(lèi)包含了執(zhí)行基本操作的方法,通過(guò)在 Activity 中調(diào)用這些方法,可以執(zhí)行所需的計(jì)算并更新顯示區(qū)域來(lái)顯示結(jié)果。
- 實(shí)現(xiàn)虛擬鍵盤(pán)按鈕的交互
??
可以通過(guò)添加按鈕狀態(tài)監(jiān)聽(tīng)器來(lái)實(shí)現(xiàn)按下、釋放和單擊事件的交互式功能
。這可以通過(guò)為每個(gè)按鈕創(chuàng)建一個(gè)類(lèi)來(lái)實(shí)現(xiàn),該類(lèi)實(shí)現(xiàn) View.OnClickListener 接口并處理所有虛擬鍵盤(pán)按鈕的單擊事件。
??在上面創(chuàng)建的 類(lèi)中,可以使用 setOnTouchListener() 方法來(lái)設(shè)置按鈕狀態(tài)的監(jiān)聽(tīng)器
。在按下按鈕時(shí),可以更改按鈕的背景顏色以指示其處于活動(dòng)狀態(tài)。當(dāng)釋放按鈕時(shí),可以將按鈕背景顏色恢復(fù)為常規(guī)顏色。
- 實(shí)現(xiàn)清零操作
??可以
創(chuàng)建一個(gè)方法來(lái)執(zhí)行清零操作
,并將該方法與清零按鈕的單擊事件相關(guān)聯(lián)。清零操作應(yīng)該將輸入?yún)^(qū)和顯示區(qū)的所有變量重置為其默認(rèn)狀態(tài)。
- 實(shí)現(xiàn)實(shí)時(shí)顯示輸入的信息
??
可以使用 TextView 來(lái)實(shí)現(xiàn)在用戶(hù)輸入時(shí)實(shí)時(shí)更新顯示區(qū)的內(nèi)容
。在輸入期間更新文本,并在最后顯示結(jié)果。
??這些步驟和功能的實(shí)現(xiàn)
可以使用 Java/Android 和 XML 來(lái)實(shí)現(xiàn)
。通過(guò)正確設(shè)計(jì)和實(shí)現(xiàn)這些功能,可以創(chuàng)建一個(gè)簡(jiǎn)單但實(shí)用的移動(dòng)端計(jì)算器 App
。
??基本布局推薦使用帥帥說(shuō)的布局格式??
??如上圖所示一共有18個(gè)Button按鈕
和一個(gè)Text View文本框
XML代碼
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:layout_marginStart="8dp"
android:backgroundTint="#EA0303"
android:text="AC"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.524"
app:rippleColor="#CB0808"
tools:ignore="HardcodedText" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:layout_marginStart="8dp"
android:text="DEL"
app:layout_constraintStart_toEndOf="@+id/btn_ac"
app:layout_constraintTop_toTopOf="@+id/btn_ac"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:layout_marginStart="8dp"
android:text="*"
app:layout_constraintStart_toEndOf="@+id/btn_del"
app:layout_constraintTop_toTopOf="@+id/btn_del"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="4dp"
android:text="/"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/btn_mul"
app:layout_constraintTop_toTopOf="@+id/btn_mul"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:text="7"
app:layout_constraintStart_toStartOf="@+id/btn_ac"
app:layout_constraintTop_toBottomOf="@+id/btn_ac"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:layout_marginStart="8dp"
android:text="8"
app:layout_constraintStart_toEndOf="@+id/btn_7"
app:layout_constraintTop_toBottomOf="@+id/btn_del"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:layout_marginStart="8dp"
android:text="9"
app:layout_constraintStart_toEndOf="@+id/btn_8"
app:layout_constraintTop_toBottomOf="@+id/btn_mul"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:layout_marginStart="8dp"
android:text="+"
app:layout_constraintStart_toEndOf="@+id/btn_9"
app:layout_constraintTop_toBottomOf="@+id/btn_div"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:text="4"
app:layout_constraintStart_toStartOf="@+id/btn_7"
app:layout_constraintTop_toBottomOf="@+id/btn_7"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:layout_marginStart="8dp"
android:text="5"
app:layout_constraintStart_toEndOf="@+id/btn_4"
app:layout_constraintTop_toBottomOf="@+id/btn_8"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:layout_marginStart="8dp"
android:text="6"
app:layout_constraintStart_toEndOf="@+id/btn_5"
app:layout_constraintTop_toBottomOf="@+id/btn_9"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:layout_marginStart="8dp"
android:text="-"
app:layout_constraintStart_toEndOf="@+id/btn_6"
app:layout_constraintTop_toBottomOf="@+id/btn_add"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:text="1"
app:layout_constraintStart_toStartOf="@+id/btn_4"
app:layout_constraintTop_toBottomOf="@+id/btn_4"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:layout_marginStart="8dp"
android:text="2"
app:layout_constraintStart_toEndOf="@+id/btn_1"
app:layout_constraintTop_toBottomOf="@+id/btn_5"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:layout_marginStart="8dp"
android:text="3"
app:layout_constraintStart_toEndOf="@+id/btn_2"
app:layout_constraintTop_toBottomOf="@+id/btn_6"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="142dp"
android:layout_marginStart="8dp"
android:backgroundTint="#DD4211"
android:text="="
app:layout_constraintStart_toEndOf="@+id/btn_3"
app:layout_constraintTop_toBottomOf="@+id/btn_sub"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="188dp"
android:layout_height="71dp"
android:text="0"
app:layout_constraintStart_toStartOf="@+id/btn_1"
app:layout_constraintTop_toBottomOf="@+id/btn_1"
tools:ignore="HardcodedText,TextContrastCheck" />
<Button
android:layout_width="90dp"
android:layout_height="71dp"
android:layout_marginStart="8dp"
android:text="."
app:layout_constraintStart_toEndOf="@+id/btn_0"
app:layout_constraintTop_toBottomOf="@+id/btn_3"
tools:ignore="HardcodedText,TextContrastCheck" />
<TextView
android:layout_width="398dp"
android:layout_height="278dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:paddingTop="240dp"
android:textSize="24sp"
android:textAlignment="textEnd"
app:layout_constraintBottom_toTopOf="@+id/btn_ac"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.483"
tools:ignore="HardcodedText" />
???????注:自己為Button添加id屬性
Java代碼
對(duì)于Button0的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
double value = Double.parseDouble(operand);
if (value == (long) value) {
long intValue = (long) value;
if (intValue == 0 || !operand.isEmpty()) {
operand += "0";
} else {
operand = "0";
}
textView.setText(operand);
} else {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {}
}
});
對(duì)于Button1的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (operand.equals("0")) {
operand = "1";
} else {
operand += "1";
}
updateResult();
}
});
對(duì)于Button2的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (operand.equals("0")) {
operand = "2";
} else {
operand += "2";
}
updateResult();
}
});
對(duì)于Button3的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (operand.equals("0")) {
operand = "3";
} else {
operand += "3";
}
updateResult();
}
});
對(duì)于Button4的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (operand.equals("0")) {
operand = "4";
} else {
operand += "4";
}
updateResult();
}
});
對(duì)于Button5的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (operand.equals("0")) {
operand = "5";
} else {
operand += "5";
}
updateResult();
}
});
對(duì)于Button6的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (operand.equals("0")) {
operand = "6";
} else {
operand += "6";
}
updateResult();
}
});
對(duì)于Button7的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (operand.equals("0")) {
operand = "7";
} else {
operand += "7";
}
updateResult();
}
});
對(duì)于Button8的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (operand.equals("0")) {
operand = "8";
} else {
operand += "8";
}
updateResult();
}
});
對(duì)于Button9的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (operand.equals("0")) {
operand = "9";
} else {
operand += "9";
}
updateResult();
}
});
對(duì)于=按鈕的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (operation.equals("/") && operand.equals("0")) {
textView.setText("error");
operand = "";
errorState = true;
} else {
double result = 0;
if (operation.equals("+")) {
result = operand1 + Double.parseDouble(operand);
} else if (operation.equals("-")) {
result = operand1 - Double.parseDouble(operand);
} else if (operation.equals("*")) {
result = operand1 * Double.parseDouble(operand);
} else if (operation.equals("/")) {
result = operand1 / Double.parseDouble(operand);
}
textView.setText(String.valueOf(result));
operand = "";
operation = "";
errorState = false;
}
}
});
對(duì)于+按鈕的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!operand.isEmpty() || operation.isEmpty()) {
if (!operation.isEmpty()) {
performOperation();
}
operation = "+";
operand1 = Double.parseDouble(operand);
operand = "";
errorState = false;
} else if (operand.equals("0")) {
operand1 = Double.parseDouble(operand);
operand = "";
errorState = false;
}
}
});
對(duì)于-按鈕的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!operand.isEmpty() || operation.isEmpty()) {
if (!operation.isEmpty()) {
performOperation();
}
operation = "-";
operand1 = Double.parseDouble(operand);
operand = "";
errorState = false;
} else if (operand.equals("0")) {
operand1 = Double.parseDouble(operand);
operand = "";
errorState = false;
}
}
});
對(duì)于*按鈕的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!operand.isEmpty() || operation.isEmpty()) {
if (!operation.isEmpty()) {
performOperation();
}
operation = "*";
operand1 = Double.parseDouble(operand);
operand = "";
errorState = false;
} else if (operand.equals("0")) {
operation = "*";
operand1 = Double.parseDouble(operand);
operand = "";
errorState = false;
}
}
});
對(duì)于/按鈕的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operand1 = Double.parseDouble(operand);
operation = "/";
operand = "";
}
});
對(duì)于清除按鈕的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clear();
}
});
對(duì)于退格按鈕的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (operand.length() > 0) {
operand = operand.substring(0, operand.length() - 1);
textView.setText(operand);
}
}
});
對(duì)于小數(shù)點(diǎn)按鈕的操作
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!operand.contains(".")) {
operand += ".";
}
updateResult();
}
});
??
上面用到三個(gè)方法需要自己定義三個(gè)方法實(shí)現(xiàn)計(jì)算并顯示的功能
- performOperation():用于實(shí)現(xiàn)加減乘除計(jì)算
- updateResult():用于實(shí)時(shí)更新用戶(hù)的輸入或者執(zhí)行結(jié)果
- clear():當(dāng)按下清除按鈕時(shí)執(zhí)行清零操作
??上面的代碼基本可以實(shí)現(xiàn)部分功能,bug問(wèn)題幾乎沒(méi)有怎么改,用測(cè)試案例測(cè)試有幾個(gè)肯定會(huì)崩,后面有時(shí)間會(huì)改一下bug的,沒(méi)時(shí)間就自己修改嘍~~~
??????????BUG滿(mǎn)天飛??????文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-428632.html
????記錄是為了不停的思考,創(chuàng)作更是為了更好的思考
,有人說(shuō)過(guò):2023年以后的中國(guó)市場(chǎng)將永遠(yuǎn)屬于長(zhǎng)期主義者,bug是改不完的也是寫(xiě)不完的,只能說(shuō)這次遇到了希望下次不會(huì)在出現(xiàn)同樣的bug就行,如果你覺(jué)得對(duì)您有幫助,歡迎一起交流學(xué)習(xí),本人也是一名計(jì)算機(jī)小白,目前還在努力中~文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-428632.html
到了這里,關(guān)于Android Studio:一個(gè)簡(jiǎn)單的計(jì)算器app的實(shí)現(xiàn)過(guò)程<初級(jí)>的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!