Android開發實現計算器

首先layout佈局配置

計算器名稱組件(可省略):

<TextView
       android:id="@+id/title"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="計算器"
       android:textSize="30sp"
       android:textColor="#912F2F"
       android:layout_gravity="center"/>

顯示過程和結果的文本框:

<TextView
       android:id="@+id/resultview"
       android:layout_width="match_parent"
       android:layout_height="150dp"
       android:background="#0A0909"
       android:layout_marginLeft="0dp"/>

各種Button組件:

 <!--第一行-->
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:layout_weight="1"
       android:background="#2196F3">
  <!--回滾-->
      <Button
          android:id="@+id/CE_btn"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:text="CE"
          android:textSize="20sp" />
 <!--符號“/”-->
      <Button
          android:id="@+id/divid_btn"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:text="/"
          android:textSize="20sp" />
           <!--符號“*”->
      <Button
          android:id="@+id/multi_btn"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:text="*"
          android:textSize="20sp" />
          <!--符號“C”(清空所有顯示框中的內容)-->
      <Button
          android:id="@+id/C_btn"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:text="C"
          android:textSize="20sp" />
   </LinearLayout>
   <!--其他四行"7\8\9\+","4\5\6\-","1\2\3\√",“0\.\=”同上省略,-->

實例化各個組件對象

  private TextView resultview;
    private Button CE_btn,divid_btn,multi_btn,C_btn;
    private Button seven_btn,eight_btn,nine_btn,plus_btn;
    private Button four_btn,five_btn,six_btn,sub_btn;
    private Button one_btn,two_btn,three_btn,sqr_btn;
    private Button zero_btn,dot_btn,equal_btn;
    private String content="";
    private char[] ch;
    ...
    one_btn = findViewById(R.id.one_btn);
        two_btn = findViewById(R.id.two_btn);
        three_btn = findViewById(R.id.three_btn);
        four_btn = findViewById(R.id.four_btn);
        five_btn = findViewById(R.id.five_btn);
        ...
          dot_btn = findViewById(R.id.dot_btn);
        CE_btn = findViewById(R.id.CE_btn);
        sqr_btn = findViewById(R.id.sqr_btn);
    //註冊監聽器
     one_btn.setOnClickListener(l);
        two_btn.setOnClickListener(l);
        three_btn.setOnClickListener(l);
        ...
         dot_btn.setOnClickListener(l);
        CE_btn.setOnClickListener(l);
        sqr_btn.setOnClickListener(l);
View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch(view.getId()){
                case R.id.one_btn:
                    numberSqrt(content,one_btn);
                    content = resultview.getText().toString();
                    resultview.setText(content);
                    break;
                case R.id.two_btn:
                    numberSqrt(content,two_btn);
                    content = resultview.getText().toString();
                    resultview.setText(content);
                    break;
                    ...
                                  case R.id.C_btn:
                    content = "";
                    resultview.setText(content);
                    break;
                default:
                    break;
            }
        }
    };

實現計算器的主要方法遞歸運算(這個方法並不好一些功能幾乎不可能實現):


//通過遞歸調用實現+-*/√運算
    public  double calculate(String content) {
        int index = content.indexOf("+");
        if (index != -1) {
            return calculate(content.substring(0, index)) + calculate(content.substring(index + 1));
        }
        index = content.indexOf("-");
        if (index != -1){
            if(index == 0)
                return 0-calculate(content.substring(index+1));
            return calculate(content.substring(0, index)) - calculate(content.substring(index+1));
        }
        index = content.indexOf("*");
        if (index != -1){
            return calculate(content.substring(0,index)) * calculate(content.substring(index+1));
        }
        index = content.indexOf("/");
        if (index != -1){
                return calculate(content.substring(0,index)) / calculate(content.substring((index + 1)));
        }
        index = content.indexOf("√");
        if (index !=-1){
            return Math.sqrt(calculate(content.substring(0,index)));
        }
           return  parseDouble(content);
    }

基本過程就是這樣,另外,沒有實現直接加、減、乘以,除以負數的情況,除以0後會顯示Infinity等。
鏈接:https://pan.baidu.com/s/1LPwLd5T8a8YwbNZiuaPgew
提取碼:lmgw

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章