新手初学安卓一周,勿喷。
在看完Activity之后决定做一个计算器。根据网上的资料和慕课网计算器的教材,仿照着做了一个iPone计算器。
先看看效果图
好了,直接给源码,XML用的是LinearLayout, 试过用TableLayout,发现总是会有间距,做出来不像。
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <EditText android:gravity="right|bottom" android:id="@+id/output" android:textSize="60dp" android:background="#000000" android:textColor="#ffffff" android:editable="false" android:layout_width="match_parent" android:layout_height="100dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/clear" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:layout_margin="0.1dp" android:background="@drawable/grey_selector" android:text="AC" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:layout_margin="0.1dp" android:background="@drawable/grey_selector" android:text="+/-" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:layout_margin="0.1dp" android:background="@drawable/grey_selector" android:text="%" /> <Button android:id="@+id/div" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:layout_margin="0.1dp" android:background="@drawable/orange_seletor" android:text="÷" /> LinearLayout> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/seven" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.1dp" android:layout_weight="1" android:background="@drawable/grey_selector" android:text="7" /> <Button android:id="@+id/eight" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.1dp" android:layout_weight="1" android:background="@drawable/grey_selector" android:text="8" /> <Button android:id="@+id/nine" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.1dp" android:layout_weight="1" android:background="@drawable/grey_selector" android:text="9" /> <Button android:id="@+id/mul" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.1dp" android:layout_weight="1" android:background="@drawable/orange_seletor" android:text="×" /> LinearLayout> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/four" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.1dp" android:layout_weight="1" android:background="@drawable/grey_selector" android:text="4" /> <Button android:id="@+id/five" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.1dp" android:layout_weight="1" android:background="@drawable/grey_selector" android:text="5" /> <Button android:id="@+id/six" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.1dp" android:layout_weight="1" android:background="@drawable/grey_selector" android:text="6" /> <Button android:id="@+id/sub" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.1dp" android:layout_weight="1" android:background="@drawable/orange_seletor" android:text="-" /> LinearLayout> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/one" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.1dp" android:layout_weight="1" android:background="@drawable/grey_selector" android:text="1" /> <Button android:id="@+id/two" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.1dp" android:layout_weight="1" android:background="@drawable/grey_selector" android:text="2" /> <Button android:id="@+id/three" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.1dp" android:layout_weight="1" android:background="@drawable/grey_selector" android:text="3" /> <Button android:id="@+id/add" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.1dp" android:layout_weight="1" android:background="@drawable/orange_seletor" android:text="+" /> LinearLayout> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/zero" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/grey_selector" android:text="0" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/grey_selector" /> <Button android:id="@+id/dot" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.1dp" android:layout_weight="1" android:background="@drawable/grey_selector" android:text="." /> <Button android:id="@+id/equal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.1dp" android:layout_weight="1" android:background="@drawable/orange_seletor" android:text="=" /> LinearLayout> LinearLayout> LinearLayout>
下面是Java的源代码,其思想是慕课网加上自己的一些修改。
package com.example.administrator.calculatoriphone; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends ActionBarActivity implements View.OnClickListener{ /* 数字按钮 */ private Button zero; private Button one; private Button two; private Button three; private Button four; private Button five; private Button six; private Button seven; private Button eight; private Button nine; //加减乘除 private Button add; private Button sub; private Button mul; private Button div; //等号,清楚 private Button dot; private Button equal; private Button clear; private EditText output; String op; int needclear; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActionBar actionBar = getSupportActionBar(); actionBar.hide(); setContentView(R.layout.activity_main); //初始化 zero = (Button)findViewById(R.id.zero); one = (Button)findViewById(R.id.one); two = (Button)findViewById(R.id.two); three = (Button)findViewById(R.id.three); four = (Button)findViewById(R.id.four); five = (Button)findViewById(R.id.five); six = (Button)findViewById(R.id.six); seven = (Button)findViewById(R.id.seven); eight = (Button)findViewById(R.id.eight); nine = (Button)findViewById(R.id.nine); add = (Button)findViewById(R.id.add); sub = (Button)findViewById(R.id.sub); mul = (Button)findViewById(R.id.mul); div = (Button)findViewById(R.id.div); dot = (Button)findViewById(R.id.dot); equal = (Button)findViewById(R.id.equal); clear = (Button)findViewById(R.id.clear); output = (EditText)findViewById(R.id.output); //设置监听器 zero.setOnClickListener(this); one.setOnClickListener(this); two.setOnClickListener(this); three.setOnClickListener(this); four.setOnClickListener(this); five.setOnClickListener(this); six.setOnClickListener(this); seven.setOnClickListener(this); eight.setOnClickListener(this); nine.setOnClickListener(this); add.setOnClickListener(this); sub.setOnClickListener(this); mul.setOnClickListener(this); div.setOnClickListener(this); dot.setOnClickListener(this); equal.setOnClickListener(this); clear.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onClick(View v) { String str = output.getText().toString(); switch (v.getId()) { case R.id.zero: case R.id.one: case R.id.two: case R.id.three: case R.id.four: case R.id.five: case R.id.six: case R.id.seven: case R.id.eight: case R.id.nine: case R.id.dot: if(needclear == 1){ output.setText(((Button) v).getText()); needclear = 0; }else { output.setText(str + ((Button) v).getText()); } break; case R.id.add: op = "add"; output.setText(str + " " + ((Button) v).getText() + " "); break; case R.id.sub: op = "sub"; output.setText(str + " " + ((Button)v).getText() + " "); break; case R.id.mul: op = "mul"; output.setText(str + " " + ((Button)v).getText() + " "); break; case R.id.div: op = "div"; output.setText(str + " " + ((Button)v).getText() + " "); break; case R.id.equal: getResult(); break; case R.id.clear: output.setText(""); default: break; } } private void getResult() { String exp = output.getText().toString(); int space = exp.indexOf(' '); //以空格为标志,截取运算符号前后的数字 String s1 = exp.substring(0, space); String s2 = exp.substring(space + 3); double arg1 = Double.parseDouble(s1); double arg2 = Double.parseDouble(s2); double result = 0; if (op.equals("add")){ result = arg1 + arg2; }else if(op.equals("sub")){ result = arg1 - arg2; }else if (op.equals("mul")) { result = arg1 * arg2; }else if (op.equals("div")) { result = arg1 / arg2; }else { return; } output.setText(result+""); needclear = 1; } }
另外实现点击之后按钮颜色转换是在drawable下面写了资源文件
deep_grey.xml
xml version="1.0" encoding="utf-8"?> <shape xmlns:android ="http://schemas.android.com/apk/res/android"> <solid android:color="#c6c7c9" /> shape>
grey_seletor.xml
xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/deep_grey" android:state_pressed="true"/> <item android:drawable="@drawable/light_grey"/> selector>
当然只是实现普通的加减乘除,负号,百分号等其他按钮没有实现,并且里面还是会出现一些或多或少的bug,例如双击等号,会出现错误等。可见一个真正发行的计算器的设计和制作也是相当繁琐的。
原创博客,转载注明出处。参考了http://wenku.baidu.com/view/1284de8ca8956bec0875e349,以及慕课网的安卓入门教学视频。