android的计算器
今天我闲着无聊,便想仿照Iphone的计算器自己写一个出来玩玩,于是就开动脑经,来场头脑风暴了!我拿什么了写呢?wp?这是个不错的选择,但是我最近在研究安卓的开发(求各位MSP不要烧我,我买不起MAC,你们要搞应该找昌豪,他有MAC,先烧死他再来烧我~~~)搞安卓开发其实还是蛮简单的!不就是监听,然后实现功能吗?跟WP的开发过程有点像 <Button click = "clickaction"\> 在后台process中会自动生成触发函数 这是wp的开发过程,而安卓呢?在xml中定义<Button android:id = "@+id\button"\> 然后在主函数里面写一个监听类,注意class ButtonListener implements OnClickListener 继承监听类,然后new一个 找到button的id 就可以设置它的监听了!好了废话不多说,让我们直接看安卓的界面吧!
有木有很像iphone的计算器啊?哈哈 不过我发现最后一行的权重在不同的手机上分配的不一样吧,这是我比较郁闷的地方,不知道有没有大牛能够帮小弟解决呢?
首先先上我们的xml代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <LinearLayout android:id="@+id/linear" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="5dp" > <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="100dp" android:gravity="right" android:hint="@string/insert" android:maxLength="26" android:textSize="40sp" /> </LinearLayout> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/linear" > <TableRow android:layout_weight="1" android:paddingLeft="5dp" android:paddingRight="5dp" > <Button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/clear" /> <Button android:id="@+id/as" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/as" /> <Button android:id="@+id/mod" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/mod" /> <Button android:id="@+id/exc" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/exc" /> </TableRow> <TableRow android:layout_weight="1" android:paddingLeft="5dp" android:paddingRight="5dp" > <Button android:id="@+id/button7" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/num7" /> <Button android:id="@+id/button8" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/num8" /> <Button android:id="@+id/button9" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/num9" /> <Button android:id="@+id/rid" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/rid" /> </TableRow> <TableRow android:layout_weight="1" android:paddingLeft="5dp" android:paddingRight="5dp" > <Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/num4" /> <Button android:id="@+id/button5" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/num5" /> <Button android:id="@+id/button6" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/num6" /> <Button android:id="@+id/sub" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/sub" /> </TableRow> <TableRow android:layout_weight="1" android:paddingLeft="5dp" android:paddingRight="5dp" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/num1" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/num2" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/num3" /> <Button android:id="@+id/add" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/add" /> </TableRow> <TableRow android:layout_weight="1" android:paddingLeft="5dp" android:paddingRight="5dp" > <Button android:id="@+id/button0" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="2" android:background="@drawable/rectangle" android:text="@string/num0" /> <Button android:id="@+id/rad" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/rad" /> <Button android:id="@+id/result" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rectangle" android:text="@string/result" /> </TableRow> </TableLayout> </RelativeLayout>
在这里我使用的一个线性布局给我得textview框定了一个100dp的范围,然后使用表格布局把控件放进去。
下面是string.xml 主要存放的一些字符串的资源
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">T0913_mycal</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="insert">0</string> <string name="num0">0</string> <string name="num1">1</string> <string name="num2">2</string> <string name="num3">3</string> <string name="num4">4</string> <string name="num5">5</string> <string name="num6">6</string> <string name="num7">7</string> <string name="num8">8</string> <string name="num9">9</string> <string name="exc">/</string> <string name="rid">*</string> <string name="sub">-</string> <string name="add">+</string> <string name="result">=</string> <string name="clear">C</string> <string name="rad">.</string> <string name="as">+/-</string> <string name="mod">%</string> </resources>
在这里我把所有的内容全部做了链接,这也是一种软件工程的思考,希望大家多多使用这种方式。
然后大家看到我得控件都是圆角而且带边框的 这是在drawable中获取的效果!
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#ff9d77"/> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp" /> <stroke android:width="2dp" android:color="#dcdcdc" /> <corners android:radius="8dp" /> </shape>
让每个控件的背景都是有这个xml就可以了
下面就是这个程序的实现代码了!
package com.example.t0913_mycal; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private Button []num =new Button[10]; private Button rid,add,sub,result,exc,clear,as,mod,rad; private TextView str ; private int option = 0;//运算符状态 private boolean newdigital=true;//标记是否是新输入的数字 private double firstNum=0,secondNum=0;//两个相加的数 private double c;//表示取正负 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rad =(Button)findViewById(R.id.rad); str = (TextView)findViewById(R.id.textView); rid =(Button)findViewById(R.id.rid); add = (Button)findViewById(R.id.add); sub = (Button)findViewById(R.id.sub); result = (Button)findViewById(R.id.result); exc = (Button)findViewById(R.id.exc); clear = (Button)findViewById(R.id.clear); as = (Button)findViewById(R.id.as); mod = (Button)findViewById(R.id.mod); num[0] = (Button)findViewById(R.id.button0); num[1] = (Button)findViewById(R.id.button1); num[2] = (Button)findViewById(R.id.button2); num[3] = (Button)findViewById(R.id.button3); num[4] = (Button)findViewById(R.id.button4); num[5] = (Button)findViewById(R.id.button5); num[6] = (Button)findViewById(R.id.button6); num[7] = (Button)findViewById(R.id.button7); num[8] = (Button)findViewById(R.id.button8); num[9] = (Button)findViewById(R.id.button9); ButtonListener listener = new ButtonListener(); rad.setOnClickListener(listener); str.setOnClickListener(listener); rid.setOnClickListener(listener); add.setOnClickListener(listener); sub.setOnClickListener(listener); result.setOnClickListener(listener); exc.setOnClickListener(listener); clear.setOnClickListener(listener); as.setOnClickListener(listener); mod.setOnClickListener(listener); num[0].setOnClickListener(listener); num[1].setOnClickListener(listener); num[2].setOnClickListener(listener); num[3].setOnClickListener(listener); num[4].setOnClickListener(listener); num[5].setOnClickListener(listener); num[6].setOnClickListener(listener); num[7].setOnClickListener(listener); num[8].setOnClickListener(listener); num[9].setOnClickListener(listener); } class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Button btu = (Button)v; String s = str.getText().toString();//获取文本框显示的字符串 String t = (String)btu.getText().toString(); if(btu.getId() == R.id.button0 || btu.getId() == R.id.button1 || btu.getId() == R.id.button2 ||btu.getId() == R.id.button3 || btu.getId() == R.id.button4 || btu.getId() == R.id.button5 || btu.getId() == R.id.button6 ||btu.getId() == R.id.button7||btu.getId() == R.id.button8 || btu.getId() == R.id.button9 ){ if(newdigital){ str.setText(""); str.setText(t); newdigital = false; } else{ str.setText(s+t); newdigital = false; }return; } if(btu.getId()==R.id.as)//改变数的正负性 { if(s.length() == 0){ firstNum = 0; secondNum = 0; newdigital = true; option = 0; return ; } else if (s !=""){ c=Double.parseDouble(s); str.setText(String.valueOf(-c)); return; } } if(btu.getId() == R.id.add){ firstNum = Double.parseDouble(s); option=1; newdigital = true; return; } if(btu.getId() == R.id.sub){ firstNum = Double.parseDouble(s); option = 2; newdigital = true; return; } if(btu.getId() == R.id.rid){ firstNum = Double.parseDouble(s); option = 3; newdigital = true; return; } if(btu.getId() == R.id.exc){ firstNum = Double.parseDouble(s); option = 4; newdigital = true; return; } if(btu.getId() == R.id.mod){ firstNum = Double.parseDouble(s); option = 5; newdigital = true; return; } if(btu.getId() == R.id.clear){ firstNum = 0; secondNum = 0; newdigital = true; option = 0; str.setText(""); return; } if(btu.getId() == R.id.rad){ if(s.length() == 0) { str.setText("0."); newdigital=false; } else if(s.indexOf(".") == -1) { str.setText(s+"."); } return; } if(btu.getId() == R.id.result){ secondNum=Double.parseDouble(s); switch(option) { case 1: str.setText(String.valueOf(firstNum+secondNum));break; case 2: str.setText(String.valueOf(firstNum-secondNum));break; case 3: str.setText(String.valueOf(firstNum*secondNum));break; case 4: { if(secondNum!=0){ str.setText(String.valueOf(firstNum/secondNum));} else { Toast.makeText(MainActivity.this, "除数不能为0!", Toast.LENGTH_SHORT).show(); str.setText(""); firstNum=0; secondNum=0; option=0; newdigital=true; return; } break; } case 5: str.setText(String.valueOf(firstNum%secondNum)); break; } return; } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
大功告成了,我的计算器其实非常之简单!望各位多多指教!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。