2025-05-24

 

上個例子我做瞭個簡單的計算器,現在抽空弄個瞭復雜點的。 具體步驟如下:

先看效果:點擊96*96=9216,其他運算一樣,我這裡就不一一截圖瞭!

 

 

 

96    *                        96                     ==   9216

                     

  點擊菜單按鈕後:                       點擊關於選項後:

                                                                                             

 

 

一、xml代碼,main.xml代碼如下:

 

 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    > 

 <TextView   

     android:layout_width="fill_parent"  

     android:layout_height="wrap_content"  

     android:text="大明原創" 

     /> 

 <EditText 

 

    android:id="@+id/showText" 

     android:layout_width="150px" 

     android:layout_height="38px" 

 /> 

   <TableLayout  xmlns:android="http://schemas.android.com/apk/res/android" 

        android:layout_width="fill_parent" 

        android:layout_height="fill_parent"> 

        <TableRow> 

    <TableLayout  xmlns:android="http://schemas.android.com/apk/res/android" 

        android:layout_width="fill_parent" 

        android:layout_height="fill_parent"> 

        <TableRow> 

             <Button 

               android:id="@+id/num1" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="1" 

             /> 

             <Button 

               android:id="@+id/num2" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="2" 

             /> 

             <Button 

               android:id="@+id/num3" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="3" 

             /> 

             <Button 

               android:id="@+id/add" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="+" 

             /> 

        </TableRow> 

        <TableRow> 

             <Button 

               android:id="@+id/num4" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="4" 

             /> 

             <Button 

               android:id="@+id/num5" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="5" 

             /> 

             <Button 

               android:id="@+id/num6" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="6" 

             /> 

             <Button 

               android:id="@+id/jian" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="-" 

             /> 

        </TableRow> 

        <TableRow> 

             <Button 

               android:id="@+id/num7" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="7" 

             /> 

             <Button 

               android:id="@+id/num8" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="8" 

             /> 

             <Button 

               android:id="@+id/num9" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="9" 

             /> 

             <Button 

               android:id="@+id/cheng" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="*" 

             /> 

        </TableRow> 

        <TableRow> 

             <Button 

               android:id="@+id/zero" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="0" 

             /> 

             <Button 

               android:id="@+id/modi" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="+/-" 

             /> 

             <Button 

               android:id="@+id/point" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="." 

             /> 

             <Button 

               android:id="@+id/chu" 

               android:layout_width="38px" 

               android:layout_height="38px" 

               android:text="/" 

             /> 

        </TableRow> 

        <TableRow> 

        </TableRow> 

    </TableLayout> 

    </TableRow> 

        <TableRow> 

              <TableLayout  xmlns:android="http://schemas.android.com/apk/res/android" 

                      android:layout_width="fill_parent" 

                      android:layout_height="fill_parent"> 

                    <TableRow> 

                <Button 

                  android:id="@+id/ce" 

                  android:layout_width="76px" 

                  android:layout_height="40px" 

                  android:text="CE" 

                /> 

                <Button 

                  android:id="@+id/result" 

                  android:layout_width="76px" 

                  android:layout_height="40px" 

                  android:text="==" 

                /> 

                    </TableRow> 

             </TableLayout> 

        </TableRow> 

    </TableLayout> 

</LinearLayout> 

 

二、MainActivity.java中的代碼:

 

package com.cn.android; 

 

import android.app.Activity; 

import android.os.Bundle; 

import android.os.Vibrator; 

import android.view.View; 

import android.widget.Button; 

import android.widget.TextView; 

 

public class MainActivity extends Activity { 

    /** Called when the activity is first created. */ 

  

 private Vibrator vibrator; 

  

 private Double num_a; 

 

 private Double num_b; 

  

 private String temp = null;// 計算符號 

 

 private boolean isDot = true;// 小數點控制 

 

 private boolean clickable = true;// 標志是否按過計算按鈕 

 

 private double memoryd; // 使用內存中存儲的數字 

  

 private EditText text = null; 

   

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

        final Button num1 = (Button)findViewById(R.id.num1); 

        final Button num2 = (Button)findViewById(R.id.num2); 

        final Button num3 = (Button)findViewById(R.id.num3); 

        final Button num4 = (Button)findViewById(R.id.num4); 

        final Button num5 = (Button)findViewById(R.id.num5); 

        final Button num6 = (Button)findViewById(R.id.num6); 

        final Button num7 = (Button)findViewById(R.id.num7); 

        final Button num8 = (Button)findViewById(R.id.num8); 

        final Button num9 = (Button)findViewById(R.id.num9); 

         

        Button num0 = (Button)findViewById(R.id.zero); 

        Button add = (Button)findViewById(R.id.add);//加 

        Button sub = (Button)findViewById(R.id.jian);//減 

        Button mul = (Button)findViewById(R.id.cheng);//乘 

        Button p = (Button)findViewById(R.id.chu);//除 

        Button point = (Button)findViewById(R.id.point);//小數點 

        Button ce = (Button)findViewById(R.id.ce);//清零 

        Button equal = (Button)findViewById(R.id.result);//等於 

        Button tf = (Button)findViewById(R.id.modi);//取反 

         

        text = (EditText)findViewById(R.id.showText); 

         

     // 0 

 

  num0.setOnClickListener(new Button.OnClickListener() { 

 

   public void onClick(View v) { 

 

    if (text.getText().toString().equalsIgnoreCase("0")) { 

 

    } else { 

 

     if (clickable == false) { 

 

      text.setText(""); 

 

      text.setText(text.getText().toString() + "0"); 

 

      clickable = true; 

 

     } else { 

 

      text.setText(text.getText().toString() + "0"); 

 

     } 

 

    } 

 

   } 

 

  });  

   

 //1 

  num1.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View v) { 

 

    String str1 = num1.getText().toString(); 

    show(str1); 

 

   } 

 

  }); 

   

 // 2 

 

  num2.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View v) { 

 

    show(num2.getText().toString()); 

 

   } 

 

  }); 

 

 // 3 

 

  num3.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View v) { 

 

    show(num3.getText().toString()); 

 

   } 

 

  }); 

 

 // 4 

 

  num4.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View v) { 

 

    show(num4.getText().toString()); 

 

   } 

 

  }); 

 

 // 5 

 

  num5.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View v) { 

 

    show(num5.getText().toString()); 

 

   } 

 

  }); 

 

 // 6 

 

  num6.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View v) { 

 

    show(num6.getText().toString()); 

 

   } 

 

  }); 

 

 // 7 

 

  num7.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View v) { 

 

    show(num7.getText().toString()); 

 

   } 

 

  }); 

 

 // 8 

 

  num8.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View v) { 

 

    show(num8.getText().toString()); 

 

   } 

 

  }); 

 

 // 9 

 

  num9.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View v) { 

 

    show(num9.getText().toString()); 

 

   } 

 

  }); 

   

   

 // .小數點 

  point.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View arg0) { 

 

    if (text.getText().toString().equalsIgnoreCase("")) { 

 

    } else { 

 

     if (text.getText().toString() != "" && isDot == true) { 

 

      text.setText(text.getText() + "."); 

 

      isDot = false; 

 

     } else { 

 

      text.setText(text.getText().toString()); 

 

     } 

 

    } 

 

   } 

 

  }); 

   

  // 加 

 

  add.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View v) { 

 

    if (text.getText().toString().equalsIgnoreCase("")) { 

 

    } else { 

 

     if (text.getText() != null) { 

 

      num_a = Double.parseDouble(text.getText().toString()); 

 

      temp = "add"; 

 

      clickable = false; 

 

      isDot = true; 

 

      text.setText(text.getText().toString()+"+"); 

 

     } 

 

    } 

 

   } 

 

  }); 

 

  // 減 

 

  sub.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View v) { 

     

    if (text.getText().toString().equalsIgnoreCase("")) { 

 

    } else { 

 

     if (text.getText() != null) { 

 

      num_a = Double.parseDouble(text.getText().toString()); 

 

      temp = "sub"; 

 

      clickable = false; 

 

      text.setText(text.getText().toString()+"—"); 

 

     } else { 

 

      text.setText(text.getText().toString()+"—"); 

 

     } 

 

     isDot = true; 

 

    } 

 

   } 

 

  }); 

 

  // 乘 

 

  mul.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View v) { 

 

    if (text.getText().toString().equalsIgnoreCase("")) { 

 

    } else { 

 

     if (text.getText() != null) { 

 

      num_a = Double.parseDouble(text.getText().toString()); 

 

      temp = "mul"; 

 

      text.setText(text.getText().toString()+"×"); 

 

      clickable = false; 

 

      isDot = true; 

 

     } 

 

    } 

 

   } 

 

  }); 

 

  // 除 

 

  p.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View v) { 

 

    if (text.getText().toString().equalsIgnoreCase("")) { 

 

    } else { 

 

     if (text.getText() != null) { 

 

      num_a = Double.parseDouble(text.getText().toString()); 

 

      temp = "p"; 

 

      text.setText(text.getText().toString()+"&pide;"); 

 

      clickable = false; 

 

      isDot = true; 

 

     } 

 

    } 

 

   } 

 

  }); 

   

   // 等於 

  equal.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View v) { 

 

    if (temp != null && text.getText() != null) { 

 

     num_b = (Double.parseDouble(text.getText().toString())); 

 

     if (temp == "add") { 

 

      text.setText(Float.toString((float) (num_a + num_b))); 

 

      temp = null; 

 

     } else if (temp == "sub") { 

 

      text.setText(Float.toString((float) (num_a – num_b))); 

 

      temp = null; 

 

     } else if (temp == "mul") { 

 

      text.setText(Float.toString((float) (num_a * num_b))); 

 

      temp = null; 

 

     } else if (temp == "p") { 

 

      text.setText(Float.toString((float) (num_a / num_b))); 

 

      temp = null; 

 

     } 

 

     clickable = false; 

 

     if (text.getText().toString() == "") { 

 

      isDot = true; 

 

     } else { 

 

      isDot = false; 

 

     } 

 

    } 

 

   } 

 

  }); 

   

  // 取反 

 

  tf.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View arg0) { 

 

    if (text.getText().toString().equalsIgnoreCase("")) { 

 

    } else { 

 

     boolean isNumber = true; 

 

     String s = text.getText().toString(); 

 

     for (int i = 0; i < s.length(); i++) 

 

      if (!(s.charAt(i) >= '0' && s.charAt(i) <= '9' 

 

      || s.charAt(i) == '.' || s.charAt(i) == '-')) { 

 

       isNumber = false; 

 

       break; 

 

      } 

 

     if (isNumber == true) { 

 

      // 如果當前字符串首字母有'-'號,代表現在是個負數,再按下時,則將首符號去掉 

 

      if (s.charAt(0) == '-') { 

 

       text.setText(""); 

 

       for (int i = 1; i < s.length(); i++) { 

 

        char a = s.charAt(i); 

 

        text.setText(text.getText().toString() + a); 

 

       } 

 

      } 

 

      // 如果當前字符串第一個字符不是符號,則添加一個符號在首字母處 

 

      else 

 

       text.setText('-' + s); 

 

     } 

 

    } 

   } 

 

  }); 

   

  // 清零 

 

  ce.setOnClickListener(new View.OnClickListener() { 

 

   public void onClick(View arg0) { 

 

    text.setText(""); 

 

    num_a = 0.0; 

 

    temp = null; 

 

    clickable = false; 

 

    isDot = true; 

 

   } 

 

  }); 

 

    } 

     

    public void show(String i) { // 1-9的數 

 

     if(i==null){ 

      System.out.println("wrong message!"); 

     } 

      

//  zd(); 

 

  if (clickable == false) { 

 

   text.setText(""); 

 

   text.setText(text.getText() + i); 

 

   clickable = true; 

 

  } else { 

 

   text.setText(text.getText() + i); 

 

  } 

    } 

 

public boolean onOptionsItemSelected(MenuItem item) { // 設置菜單事件 

 

  if (item.getItemId() == 1) { 

 

   finish(); 

  } 

 

  else { 

 

   AlertDialog.Builder dialog = new AlertDialog.Builder(this); 

 

   dialog.setTitle("關於") 

     .setMessage( 

       "This is a better jisuanqi!\nBy:daming\nQQ:544725635") 

     .show(); 

 

  } 

 

  return super.onOptionsItemSelected(item); 

 

 } 

     

 

三、 多國語言支持,支持中文和英文

(1)values中的strings.xml的代碼

 

<?xml version="1.0" encoding="utf-8"?> 

<resources> 

    <string name="hello">Hello World, MainActivity!</string> 

    <string name="app_name">jsq by daming</string> 

    <string name="exit">exit</string> 

    <string name="about">about</string> 

</resources> 

 

(2)values-zh-rCN中的strings.xml的代碼

 

<?xml version="1.0" encoding="utf-8"?> 

<resources> 

    <string name="app_name">大名原創計算器</string> 

    <string name="exit">退出</string> 

    <string name="about">關於</string> 

</resources> 

 

摘自 大明zeroson的android學習歷程

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *