<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Android Studio實現簡易計算器App的具體程式碼,供大家參考,具體內容如下
效果演示
佈局檔案
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:orientation="vertical"> <TextView android:id="@+id/mini" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="40dp" android:text="" android:textDirection="rtl"/> <TextView android:id="@+id/max" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="100dp" android:text="" android:textDirection="rtl"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="%" android:textSize="30dp" android:onClick="surplus"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="CE" android:textSize="30dp" android:onClick="clearce"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="C" android:textSize="30dp" android:onClick="clearc"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="⇦" android:textSize="30dp" android:onClick="backsprce"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="1/x" android:textSize="30dp" android:onClick="reciprocal"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="x²" android:textSize="30dp" android:onClick="square"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="²√x" android:textSize="30dp" android:onClick="squareroot"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="÷" android:textSize="60dp" android:onClick="division"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="7" android:textSize="30dp" android:onClick="number_7"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="8" android:textSize="30dp" android:onClick="number_8"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="9" android:textSize="30dp" android:onClick="number_9"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="×" android:textSize="30dp" android:onClick="multiplication"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="4" android:textSize="30dp" android:onClick="number_4"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="5" android:textSize="30dp" android:onClick="number_5"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="6" android:textSize="30dp" android:onClick="number_6"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="-" android:textSize="30dp" android:onClick="subtraction"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="1" android:textSize="30dp" android:onClick="number_1"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="2" android:textSize="30dp" android:onClick="number_2"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="3" android:textSize="30dp" android:onClick="number_3"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="+" android:textSize="30dp" android:onClick="addition"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="±" android:textSize="30dp" android:onClick="change"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="0" android:textSize="30dp" android:onClick="number_0"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="." android:textSize="30dp"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="=" android:textSize="30dp" android:onClick="equal"/> </LinearLayout> </LinearLayout> </LinearLayout>
Java檔案
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.Nullable; public class MainActivity extends Activity { private TextView tv1; private TextView tv2; private StringBuilder viewStr; private int index = 0; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_calc); tv1 = (TextView) findViewById(R.id.mini); tv2 = (TextView) findViewById(R.id.max); viewStr = new StringBuilder(); index = 0; } //加減乘除、求餘簡單運算方法 public String calc(StringBuilder strB) { String allS = strB.toString(); String[] asmd = {"+","-","×","÷","%"}; int x = 0; int y = 0; String result = null; for (int i = 0; i < 5; i ++) { int inde = strB.indexOf(asmd[i]); if (inde > 0) { String a = allS.split(asmd[i])[0]; String b = allS.split(asmd[i])[1]; if (i == 0) {result = String.valueOf(Integer.parseInt(a) + Integer.parseInt(b));} if (i == 1) {result = String.valueOf(Integer.parseInt(a) - Integer.parseInt(b));} if (i == 2) {result = String.valueOf(Integer.parseInt(a) * Integer.parseInt(b));} if (i == 3) { if (Integer.parseInt(b) == 0) { Toast.makeText(this,"0不能為除數",Toast.LENGTH_SHORT).show(); result = String.valueOf(Integer.parseInt(a)); }else { result = String.valueOf(Integer.parseInt(a) / Integer.parseInt(b)); } } if (i == 4) {result = String.valueOf(Integer.parseInt(a) % Integer.parseInt(b));} } } return result; } //數位按鈕事件 public void number_0(View view) { viewStr.append("0"); index ++ ; tv2.setText(viewStr); } public void number_1(View view) { viewStr.append("1"); index ++ ; tv2.setText(viewStr); } public void number_2(View view) { viewStr.append("2"); index ++ ; tv2.setText(viewStr); } public void number_3(View view) { viewStr.append("3"); index ++ ; tv2.setText(viewStr); } public void number_4(View view) { viewStr.append("4"); index ++ ; tv2.setText(viewStr); } public void number_5(View view) { viewStr.append("5"); index ++ ; tv2.setText(viewStr); } public void number_6(View view) { viewStr.append("6"); index ++ ; tv2.setText(viewStr); } public void number_7(View view) { viewStr.append("7"); index ++ ; tv2.setText(viewStr); } public void number_8(View view) { viewStr.append("8"); index ++ ; tv2.setText(viewStr); } public void number_9(View view) { viewStr.append("9"); index ++ ; tv2.setText(viewStr); } //回退按鈕事件 public void backsprce(View view) { if (viewStr.length() == 0) return; index = viewStr.length(); viewStr.deleteCharAt(--index); tv2.setText(viewStr); } //符號改變按鈕事件 public void change(View view) { String allS = viewStr.toString(); String[] asmd = {"+","-","×","÷","%"}; int inde = 0; String a = null; //儲存字串中運運算元部分 String b = null; //儲存字串中數位部分 for (int i = 0; i < 5; i ++) { inde = viewStr.indexOf(asmd[i]); if (inde != -1) { a = asmd[i]; break; } } //A 字元形 改變 A 正負值 if (inde == -1) { clearc(view); int c = - Integer.parseInt(allS); viewStr.append(String.valueOf(c)); index = String.valueOf(c).length(); tv2.setText(viewStr); } //A + 字串形 改變 A 正負值 暫時無法實現,待後續優化 if (inde == index - 1) { return; // clearc(view); // int c = - Integer.valueOf(allS.split(a)[0]); // viewStr.append(String.valueOf(c)); // index = String.valueOf(c).length(); // tv2.setText(viewStr); } //A + B 字串形 改變 B 正負值 if (inde >= 0 && inde < index) { clearc(view); b = allS.split(a)[0]; int c = - Integer.parseInt(allS.split(a)[1]); viewStr.append(b).append(a).append(String.valueOf(c)); index = String.valueOf(c).length(); tv2.setText(viewStr); } } //清空按鈕事件 public void clearc(View view) { StringBuilder temp = new StringBuilder(); viewStr = temp.append(""); tv2.setText(viewStr); index = 0; } public void clearce(View view) { StringBuilder temp = new StringBuilder(); viewStr = temp.append(""); tv1.setText(""); tv2.setText(viewStr); index = 0; } //等於按鈕事件 public void equal(View view) { String[] asmd = {"+","-","×","÷","%"}; StringBuilder temp = new StringBuilder(); for (int i = 0; i < 5; i ++) { int inde = viewStr.indexOf(asmd[i]); if (inde > 0 && inde != index-1) { tv1.setText(calc(viewStr)); String a = calc(viewStr); viewStr = temp.append(a); tv2.setText(viewStr); index = viewStr.length(); return; }else if (inde > 0 && inde == index-1) { viewStr.deleteCharAt(--index); tv1.setText(viewStr); tv2.setText(viewStr); } } tv1.setText(viewStr); tv2.setText(viewStr); } //加減乘除按鈕事件 public void addition(View view) { if (viewStr.length() == 0) return; String[] asmd = {"+","-","×","÷","%"}; StringBuilder temp = new StringBuilder(); for (int i = 0; i < 5; i ++) { int inde = viewStr.indexOf(asmd[i]); if (inde > 0 && viewStr.charAt(index-1) >= '0' && viewStr.charAt(index-1) <= '9') { tv1.setText(calc(viewStr)); String a = calc(viewStr); viewStr = temp.append(a).append('+'); tv2.setText(viewStr); index = viewStr.length(); return; } } char a = viewStr.charAt(index-1); if (a == '+') { return; } if (a == '-' || a == '×' || a == '÷' || a == '%') { viewStr.setCharAt(index-1,'+'); }else { viewStr.append("+"); index ++ ; } tv2.setText(viewStr); } public void subtraction(View view) { if (viewStr.length() == 0) return; String[] asmd = {"+","-","×","÷","%"}; StringBuilder temp = new StringBuilder(); for (int i = 0; i < 5; i ++) { int inde = viewStr.indexOf(asmd[i]); if (inde > 0 && viewStr.charAt(index-1) >= '0' && viewStr.charAt(index-1) <= '9') { tv1.setText(calc(viewStr)); String a = calc(viewStr); viewStr = temp.append(a).append('-'); tv2.setText(viewStr); index = viewStr.length(); return; } } char a = viewStr.charAt(index-1); if (a == '-') { return; } if (a == '+' || a == '×' || a == '÷' || a == '%') { viewStr.setCharAt(index-1,'-'); }else { viewStr.append("-"); index ++ ; } tv2.setText(viewStr); } public void multiplication(View view) { if (viewStr.length() == 0) return; String[] asmd = {"+","-","×","÷","%"}; StringBuilder temp = new StringBuilder(); for (int i = 0; i < 5; i ++) { int inde = viewStr.indexOf(asmd[i]); if (inde > 0 && viewStr.charAt(index-1) >= '0' && viewStr.charAt(index-1) <= '9') { tv1.setText(calc(viewStr)); String a = calc(viewStr); viewStr = temp.append(a).append('×'); tv2.setText(viewStr); index = viewStr.length(); return; } } char a = viewStr.charAt(index-1); if (a == '×') { return; } if (a == '+' || a == '-' || a == '÷' || a == '%') { viewStr.setCharAt(index-1,'×'); }else { viewStr.append("×"); index ++ ; } tv2.setText(viewStr); } public void division(View view) { if (viewStr.length() == 0) return; String[] asmd = {"+","-","×","÷","%"}; StringBuilder temp = new StringBuilder(); for (int i = 0; i < 5; i ++) { int inde = viewStr.indexOf(asmd[i]); if (inde > 0 && viewStr.charAt(index-1) >= '0' && viewStr.charAt(index-1) <= '9') { tv1.setText(calc(viewStr)); String a = calc(viewStr); viewStr = temp.append(a).append('÷'); tv2.setText(viewStr); index = viewStr.length(); return; } } char a = viewStr.charAt(index-1); if (a == '÷') { return; } if (a == '+' || a == '-' || a == '×' || a == '%') { viewStr.setCharAt(index-1,'÷'); }else { viewStr.append("÷"); index ++ ; } tv2.setText(viewStr); } //求餘按鈕事件 public void surplus(View view) { if (viewStr.length() == 0) return; String[] asmd = {"+","-","×","÷","%"}; StringBuilder temp = new StringBuilder(); for (int i = 0; i < 5; i ++) { int inde = viewStr.indexOf(asmd[i]); if (inde > 0 && viewStr.charAt(index-1) >= '0' && viewStr.charAt(index-1) <= '9') { tv1.setText(calc(viewStr)); String a = calc(viewStr); viewStr = temp.append(a).append('%'); tv2.setText(viewStr); index = viewStr.length(); return; } } char a = viewStr.charAt(index-1); if (a == '%') { return; } if (a == '+' || a == '-' || a == '×' || a == '÷') { viewStr.setCharAt(index-1,'%'); }else { viewStr.append("%"); index ++ ; } tv2.setText(viewStr); } //求倒數按鈕事件 public void reciprocal(View view) { if (viewStr.length() == 0) return; String[] asmd = {"+","-","×","÷","%"}; for (int i = 0; i < 5; i ++) { int inde = viewStr.indexOf(asmd[i]); if (inde > -1) { return; } } int a = Integer.parseInt(viewStr.toString().trim()); if (a == 0) { Toast.makeText(this,"0不能為除數",Toast.LENGTH_SHORT).show(); return; } clearc(view); viewStr.append(1/a); index = String.valueOf(1/a).length(); tv2.setText(viewStr); } //平方按鈕事件 public void square(View view) { if (viewStr.length() == 0) return; String[] asmd = {"+","-","×","÷","%"}; for (int i = 0; i < 5; i ++) { int inde = viewStr.indexOf(asmd[i]); if (inde > -1) { return; } } int a = Integer.parseInt(viewStr.toString().trim()); clearc(view); viewStr.append(a*a); index = String.valueOf(a*a).length(); tv2.setText(viewStr); } //開平方按鈕事件 public void squareroot(View view) { if (viewStr.length() == 0) return; String[] asmd = {"+","-","×","÷","%"}; for (int i = 0; i < 5; i ++) { int inde = viewStr.indexOf(asmd[i]); if (inde > -1) { return; } } int a = Integer.parseInt(viewStr.toString().trim()); clearc(view); viewStr.append((int)Math.sqrt(a)); index = String.valueOf((int)Math.sqrt(a)).length(); tv2.setText(viewStr); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45