欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

android计算器原创【安卓进化三】

来源: 开发者 投稿于  被查看 13384 次 评论:64

android计算器原创【安卓进化三】


 

上个例子我做了个简单的计算器,现在抽空弄个了复杂点的。 具体步骤如下:

先看效果:点击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 div = (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; 

 

     } 

 

    } 

 

   } 

 

  }); 

 

  // 除 

 

  div.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 = "div"; 

 

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

 

      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 == "div") { 

 

      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学习历程

相关文章

    暂无相关文章

用户评论