重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
這篇文章給大家分享的是有關Android Studio如何實現簡易計算器的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
創新互聯-專業網站定制、快速模板網站建設、高性價比北川羌族網站開發、企業建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式北川羌族網站制作公司更省心,省錢,快速模板網站建設找我們,業務覆蓋北川羌族地區。費用合理售后完善,十年實體公司更值得信賴。
Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。
這是一個運用網格布局來做的簡易計算器,可能沒有那么美觀,大家可以繼續完善
首先先看看成果吧
首先先建一個新的Project Calculator
然后先編寫顏色背景文件
創建一個gray.xml,哪里創建呢?如圖
在drawable下右擊,選擇new–Drawable resource file
第一個是文件名字,第二個屬性可以自己選擇,我們這里前兩個文件選擇shape,第三個文件選selector,附上顏色背景代碼
gray.xml
orange.xml
// 圓角 //顏色
white.xml
change.xml
- //默認顏色
- //按下的改變的顏色
這個是當你按下按鍵的時候按鍵會改變顏色
接下來就是布局文件了
activity_main.xml
我用的是表格布局,大家也可以用表格布局來寫,效果會好一些
接下來就是MainActivity.java
package com.example.calculator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button btn_clean,btn_del,btn_divide,btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9, btn_multiply,btn_add,btn_minus,btn_point,btn_equal; TextView textView; boolean clear_flag; //清空標識 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); btn_0 = findViewById(R.id.btn_0); //初始化 btn_1 = findViewById(R.id.btn_1); btn_2 = findViewById(R.id.btn_2); btn_3 = findViewById(R.id.btn_3); btn_4 = findViewById(R.id.btn_4); btn_5 = findViewById(R.id.btn_5); btn_6 = findViewById(R.id.btn_6); btn_7 = findViewById(R.id.btn_7); btn_8 = findViewById(R.id.btn_8); btn_9 = findViewById(R.id.btn_9); btn_multiply = findViewById(R.id.btn_multiply); btn_divide = findViewById(R.id.btn_divide); btn_add = findViewById(R.id.btn_add); btn_minus = findViewById(R.id.btn_minus); btn_point = findViewById(R.id.btn_point); btn_del =findViewById(R.id.btn_del); btn_equal = findViewById(R.id.btn_equal); btn_clean = findViewById(R.id.btn_clean); textView = findViewById(R.id.textView); 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_minus.setOnClickListener(this); btn_multiply.setOnClickListener(this); btn_del.setOnClickListener(this); btn_divide.setOnClickListener(this); btn_point.setOnClickListener(this); btn_add.setOnClickListener(this); btn_equal.setOnClickListener(this); btn_clean.setOnClickListener(this); } public void onClick(View v) { String str = textView.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_point: if(clear_flag){ clear_flag=false; str=""; textView.setText (""); } textView.setText(str+((Button)v).getText ()); break; case R.id.btn_add: case R.id.btn_minus: case R.id.btn_multiply: case R.id.btn_divide: if(clear_flag){ clear_flag=false; textView.setText(""); } textView.setText(str+" "+((Button)v).getText()+" "); break; case R.id.btn_del: if(clear_flag){ clear_flag=false; textView.setText (""); }else if (str != null && !str.equals ("")){ textView.setText(str.substring(0,str.length()-1)); //刪除一個字符 } break; case R.id.btn_clean: clear_flag=false; str = ""; textView.setText(""); //清空文本內容 break; case R.id.btn_equal: getResult(); //獲取結果 break; } } private void getResult() { //算法 String s = textView.getText().toString(); if(s == null || s.equals ("")){ return; } if (!s.contains ("")){ return; } if (clear_flag){ clear_flag=false; return; } clear_flag=true; String str1 = s.substring(0,s.indexOf(" ")); // 獲取到運算符前面的字符 String str_y = s.substring(s.indexOf(" ")+1,s.indexOf(" ")+2); //獲取到運算符 String str2 = s.substring(s.indexOf(" ")+ 3); //獲取到運算符后面的字符 double result = 0; if (!str1.equals ("") && !str2.equals ("")){ double num1 = Double.parseDouble(str1); //將str1、str2強制轉化為double類型 double num2 = Double.parseDouble(str2); if (str_y.equals ("+")){ result = num1 + num2; }else if (str_y.equals ("-")){ result = num1 - num2; }else if (str_y.equals ("÷")){ if (num2 == 0){ result = 0; }else { result = num1/num2; } }else if (str_y.equals ("*")){ result = num1*num2; } if (!str1.contains (".") && !str2.contains (".") && !s.equals ("÷")){ int k = (int) result; //強制轉換 textView.setText (k); }else{ textView.setText (result+""); } }else if (!str1.equals ("") && str2.equals ("")){ textView.setText (s); }else if (str1.equals ("") && !str2.equals ("")){ double num2 = Double.parseDouble(str2); if (s.equals ("+")){ result = 0 + num2; }else if (s.equals("-")){ result = 0 - num2; }else if (s.equals("×")){ result = 0; }else if (s.equals("÷")){ result = 0; } if (!str2.contains (".")) { int r = (int) result; textView.setText (r + ""); } else { textView.setText (result + ""); } } else { textView.setText (""); } } }
感謝各位的閱讀!關于“Android Studio如何實現簡易計算器”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!