您的位置:首页 > 移动开发 > Android开发

Android实现简单的计算器

2016-07-07 20:14 453 查看

android的布局和配置文件

1.下面就是android实现的计算器的布局配置,可以看见基本是线性布局,这样的好处是界面简洁明了

<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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<!-- 任务:    要求datapick,监听用户选择的日期 -->
<!--         android:inputType="text" -->

<EditText
android:layout_width="280dp"
android:layout_height="80dp"
android:background="@android:drawable/edit_text"
android:inputType="text"
android:id="@+id/result"

/>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_ce"
android:id="@+id/c_ce"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_c"
android:id="@+id/c_c"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_xx"
android:id="@+id/c_xx"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_div"
android:id="@+id/c_div"
/>
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_7"
android:id="@+id/c_7"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_8"
android:id="@+id/c_8"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_9"
android:id="@+id/c_9"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_X"
android:id="@+id/c_X"
/>
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_4"
android:id="@+id/c_4"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_5"
android:id="@+id/c_5"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_6"
android:id="@+id/c_6"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_delete"
android:id="@+id/c_delete"
/>
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_1"
android:id="@+id/c_1"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_2"
android:id="@+id/c_2"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_3"
android:id="@+id/c_3"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_add"
android:id="@+id/c_add"
/>
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_aord"
android:id="@+id/c_aord"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_0"
android:id="@+id/c_0"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_point"
android:id="@+id/c_point"
/>
<Button
android:layout_width="70dp"
android:layout_height="60dp"
android:text="@string/c_equal"
android:id="@+id/c_equal"
/>
</LinearLayout>

</LinearLayout>


2.按钮对应的字符串配置:

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

<string name="app_name">计算器</string>
<string name="action_settings">Settings</string>
<string name="c_ce">CE</string>
<string name="c_c">C</string>
<string name="c_xx">Xx</string>
<string name="c_div">÷</string>

<string name="c_7">7</string>
<string name="c_8">8</string>
<string name="c_9">9</string>
<string name="c_X">X</string>

<string name="c_4">4</string>
<string name="c_5">5</string>
<string name="c_6">6</string>
<string name="c_delete">-</string>

<string name="c_1">1</string>
<string name="c_2">2</string>
<string name="c_3">3</string>
<string name="c_add">+</string>

<string name="c_aord">±</string>
<string name="c_0">0</string>
<string name="c_point">·</string>
<string name="c_equal">=</string>

</resources>


实现的界面效果如下





3.具体操作细节:

这里由于是个练习没有按照很严格的分类但功能基本实现了:

以下是逻辑需要用运算接口和加减乘除算法:

package com.example.control;

public interface Calculate {
/**
* 计算的方法
* @param x 输入的要被计算的参数x
* @param y 输入的要被计算的参数y
*/
public float calculate(double x,double y);
}

package com.example.control;

public class Add implements Calculate{

@Override
public float calculate(double x, double y) {
return (float) (x+y);
}

}

package com.example.control;

public class Delete implements Calculate{

public float calculate(double x, double y) {
return (float) (x-y);
}

}

package com.example.control;

public class Mulitply implements Calculate{

public float calculate(double x, double y) {
return (float) (x*y);
}

}

package com.example.control;

public class Div implements Calculate{

public float calculate(double x, double y) {
if(y==0){
try {
throw new Exception("被除数不能为0");
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
return (float) (x/y);
}

}


以上这些供具体使用的时候调用

4.以下为activity和监听的内容:

package com.example.caculate;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.control.Add;
import com.example.control.Calculate;
import com.example.control.Delete;
import com.example.control.Div;
import com.example.control.Mulitply;

public class MainActivity extends Activity {

private float x,y;
private String text="";
private int tagremeber=0;
private EditText textview;
private boolean eqstatus=false;
private boolean zestatus=false;
private int count=0;

private Calculate cl;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=(EditText) findViewById(R.id.result);
textview.setText("0.0");
textview.requestFocus();

Button bt_0=(Button) findViewById(R.id.c_0);
Button bt_1=(Button) findViewById(R.id.c_1);
Button bt_2=(Button) findViewById(R.id.c_2);
Button bt_3=(Button) findViewById(R.id.c_3);
Button bt_4=(Button) findViewById(R.id.c_4);
Button bt_5=(Button) findViewById(R.id.c_5);
Button bt_6=(Button) findViewById(R.id.c_6);
Button bt_7=(Button) findViewById(R.id.c_7);
Button bt_8=(Button) findViewById(R.id.c_8);
Button bt_9=(Button) findViewById(R.id.c_9);
Button bt_add=(Button) findViewById(R.id.c_add);
Button bt_delete=(Button) findViewById(R.id.c_delete);
Button bt_mul=(Button) findViewById(R.id.c_X);
Button bt_div=(Button) findViewById(R.id.c_div);
Button bt_c=(Button) findViewById(R.id.c_c);
Button bt_xx=(Button) findViewById(R.id.c_xx);
Button bt_ce=(Button) findViewById(R.id.c_ce);
Button bt_aord=(Button) findViewById(R.id.c_aord);
Button bt_equal=(Button) findViewById(R.id.c_equal);
Button bt_point=(Button) findViewById(R.id.c_point);

//其中1-10为数字  11-20位运算符
bt_0.setTag(20);
bt_1.setTag(1);
bt_2.setTag(2);
bt_3.setTag(3);
bt_4.setTag(4);
bt_5.setTag(5);
bt_6.setTag(6);
bt_7.setTag(7);
bt_8.setTag(8);
bt_9.setTag(9);
bt_add.setTag(10);
bt_delete.setTag(11);
bt_mul.setTag(12);
bt_div.setTag(13);

bt_c.setTag(14);
bt_xx.setTag(15);
bt_ce.setTag(16);
bt_aord.setTag(17);
bt_equal.setTag(18);
bt_point.setTag(19);
//给0-9和.加上数值对应的监听
bt_0.setOnClickListener(ol);
bt_1.setOnClickListener(ol);
bt_2.setOnClickListener(ol);
bt_3.setOnClickListener(ol);
bt_4.setOnClickListener(ol);
bt_5.setOnClickListener(ol);
bt_6.setOnClickListener(ol);
bt_7.setOnClickListener(ol);
bt_8.setOnClickListener(ol);
bt_9.setOnClickListener(ol);
bt_point.setOnClickListener(ol);
//运算符类按钮加上运算法类的监听
bt_add.setOnClickListener(cal_listener);
bt_delete.setOnClickListener(cal_listener);
bt_mul.setOnClickListener(cal_listener);
bt_div.setOnClickListener(cal_listener);
bt_equal.setOnClickListener(cal_listener);
//清除等按钮
bt_c.setOnClickListener(setzero_listener);
bt_xx.setOnClickListener(setzero_listener);
bt_ce.setOnClickListener(setzero_listener);
bt_aord.setOnClickListener(setzero_listener);
}

OnClickListener ol=new OnClickListener() {
public void onClick(View view) {
int tag=(Integer) view.getTag();
if(eqstatus){
text="";
textview.setSelection(text.length());
eqstatus=false;
}

if(zestatus){
text="";
textview.setSelection(text.length());
zestatus=false;
}

switch(tag){

case 20:
text=text+"0";
break;
case 1:
text=text+"1";
break;
case 2:
text=text+"2";
break;
case 3:
text=text+"3";
break;
case 4:
text=text+"4";
break;
case 5:
text=text+"5";
break;
case 6:
text=text+"6";
break;
case 7:
text=text+"7";
break;
case 8:
text=text+"8";
break;
case 9:
text=text+"9";
break;
case 19:
text=text+".";
}
textview.setText(text);
textview.setSelection(text.length());
}
};

OnClickListener cal_listener=new OnClickListener() {

public void onClick(View view) {
int tag=(Integer) view.getTag();
//当单击运算按钮不为=时
if(tag!=18){
//保存x并清除文本域
x=Float.parseFloat(text);
tagremeber=tag;
text="";
textview.setText(text);
textview.setSelection(text.length());
}
//点击=运算符时
else if(tag==18){

y=Float.parseFloat(text);
switch(tagremeber){
case 10:
cl=new Add();
break;
case 11:
cl=new Delete();
break;
case 12:
cl=new Mulitply();
break;
case 13:
cl=new Div();
break;
}
float result=cl.calculate(x, y);
text=String.valueOf(result);
textview.setText(text);
textview.setSelection(text.length());

//表示当前状态为结果状态,下次点击数字时会自动清除这次结果
eqstatus=true;
}

}
};

OnClickListener setzero_listener=new OnClickListener() {
//
@Override
public void onClick(View view) {
int tag=(Integer) view.getTag();

switch(tag){
//全部清除
case 14:
x=0;
y=0;
text="0.0";
zestatus=true;
break;
case 15:
text=text.substring(0,text.length()-1);
break;
case 16:
x=0;
text="0.0";
zestatus=true;
break;
case 17:
count++;
if(count!=0&&count%2==0){
text=text.substring(1);
}
else if(count%2==1){
text="-"+text;
}
break;
}
textview.setText(text);
textview.setSelection(text.length());

}
};

/**
* 配置菜单组件
*/
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 1,"退出" );
menu.add(0,1,2,"关于");
return true;
}

/**
* 触发相应的事件
*/
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case 0:
finish();
case 1:
Toast.makeText(MainActivity.this, "这是浩哥的计算机", 1).show();
}
return super.onOptionsItemSelected(item);
}
}


以上的按钮的功能包括删除,正负和置零功能以及加减乘除基本都实现了,感兴趣的朋友可以自己实际测试一下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: