很早以前刚学编程的时候也写过计算器 代码之复杂..令人发寒
然后今日从慕课网上看来的逻辑 就跟着学了学 感觉还不错
package com.hk.demo.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mEtInput;
/**
* C
*/
private Button mBtC;
/**
* 7
*/
private Button mBt7;
/**
* 4
*/
private Button mBt4;
/**
* 1
*/
private Button mBt1;
/**
* %
*/
private Button mBtPercent;
/**
* DEL
*/
private Button mBtDel;
/**
* 8
*/
private Button mBt8;
/**
* 5
*/
private Button mBt5;
/**
* 2
*/
private Button mBt2;
/**
* 0
*/
private Button mBt0;
/**
* /
*/
private Button mBtDivider;
/**
* 9
*/
private Button mBt9;
/**
* 6
*/
private Button mBt6;
/**
* 3
*/
private Button mBt3;
/**
* .
*/
private Button mBtPoint;
/**
* X
*/
private Button mBtMult;
/**
* -
*/
private Button mBtMinus;
/**
* +
*/
private Button mBtAdd;
/**
* =
*/
private Button mBtEq;
private LinearLayout mLlKey;
private boolean clear_flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//这个方法用来初始化视图
initView();
//这个方法用来初始化逻辑
initData();
}
private void initData() {
}
private void initView() {
mEtInput = (EditText) findViewById(R.id.et_input);
mBtC = (Button) findViewById(R.id.bt_c);
mBtC.setOnClickListener(this);
mBt7 = (Button) findViewById(R.id.bt_7);
mBt7.setOnClickListener(this);
mBt4 = (Button) findViewById(R.id.bt_4);
mBt4.setOnClickListener(this);
mBt1 = (Button) findViewById(R.id.bt_1);
mBt1.setOnClickListener(this);
mBtPercent = (Button) findViewById(R.id.bt_percent);
mBtPercent.setOnClickListener(this);
mBtDel = (Button) findViewById(R.id.bt_del);
mBtDel.setOnClickListener(this);
mBt8 = (Button) findViewById(R.id.bt_8);
mBt8.setOnClickListener(this);
mBt5 = (Button) findViewById(R.id.bt_5);
mBt5.setOnClickListener(this);
mBt2 = (Button) findViewById(R.id.bt_2);
mBt2.setOnClickListener(this);
mBt0 = (Button) findViewById(R.id.bt_0);
mBt0.setOnClickListener(this);
mBtDivider = (Button) findViewById(R.id.bt_divider);
mBtDivider.setOnClickListener(this);
mBt9 = (Button) findViewById(R.id.bt_9);
mBt9.setOnClickListener(this);
mBt6 = (Button) findViewById(R.id.bt_6);
mBt6.setOnClickListener(this);
mBt3 = (Button) findViewById(R.id.bt3);
mBt3.setOnClickListener(this);
mBtPoint = (Button) findViewById(R.id.bt_point);
mBtPoint.setOnClickListener(this);
mBtMult = (Button) findViewById(R.id.bt_mult);
mBtMult.setOnClickListener(this);
mBtMinus = (Button) findViewById(R.id.bt_minus);
mBtMinus.setOnClickListener(this);
mBtAdd = (Button) findViewById(R.id.bt_add);
mBtAdd.setOnClickListener(this);
mBtEq = (Button) findViewById(R.id.bt_eq);
mBtEq.setOnClickListener(this);
mLlKey = (LinearLayout) findViewById(R.id.ll_key);
}
@Override
public void onClick(View v) {
String str = mEtInput.getText().toString();
switch (v.getId()) {
default:
break;
case R.id.bt_7:
case R.id.bt_4:
case R.id.bt_1:
case R.id.bt_8:
case R.id.bt_5:
case R.id.bt_2:
case R.id.bt_0:
case R.id.bt_9:
case R.id.bt_6:
case R.id.bt3:
case R.id.bt_point:
mEtInput.setText(str + ((Button) v).getText().toString());
break;
case R.id.bt_mult:
case R.id.bt_minus:
case R.id.bt_add:
case R.id.bt_percent:
case R.id.bt_divider:
getOperator(str,v);
break;
case R.id.bt_eq:
getResult();
break;
case R.id.bt_del:
if (!TextUtils.isEmpty(str)) {
mEtInput.setText(str.substring(0, str.length() - 1));
}
break;
case R.id.bt_c:
mEtInput.setText("");
break;
}
}
private void getOperator(String str, View v) {
mEtInput.setText(str + " " + ((Button) v).getText().toString() + " ");
//todo 实现更复杂的计算
}
//单独运算结果
public void getResult() {
String exp = mEtInput.getText().toString();
if (TextUtils.isEmpty(exp)) {
return;
}
if (!exp.contains(" ")) {
return;
}
//运算符前面的字符串
String s1 = exp.substring(0, exp.indexOf(" "));
//运算符号
String op = exp.substring(exp.indexOf(" ") + 1, exp.indexOf(" ") + 2);
//运算符后面的字符串
String s2 = exp.substring(exp.indexOf(" ") + 3,exp.length());
double result = 0;
if (!TextUtils.isEmpty(s1) &&! TextUtils.isEmpty(s2) ) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
if (op.equals("+")) {
result = d1 + d2;
} else if (op.equals("-")) {
result = d1 - d2;
} else if (op.equals("*")) {
result = d1 * d2;
} else if (op.equals("%")) {
result = d1 % d2;
} else if (op.equals("/")) {
if (d2 == 0) {
result = 0;
} else {
result = d1 / d2;
}
}
Toast.makeText(this, d1+" "+d2+" "+result+" "+exp, Toast.LENGTH_SHORT).show();
if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {
int r = (int) result;
mEtInput.setText(r + "");
} else {
mEtInput.setText(result + "");
}
}
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="计算器"
android:textColor="@android:color/white"
android:textSize="22sp" />
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="10dp"
android:background="@drawable/white_bg"
android:editable="false"
android:gravity="right|bottom" />
<LinearLayout
android:id="@+id/ll_key"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:orientation="horizontal">
<!--第一s部分741%-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/bt_c"
style="@style/MainBtn"
android:background="@drawable/white_bg_border_1_topleft_selector"
android:text="C" />
<Button
android:id="@+id/bt_7"
style="@style/MainBtnNormal"
android:text="7" />
<Button
android:id="@+id/bt_4"
style="@style/MainBtnNormal"
android:text="4" />
<Button
android:id="@+id/bt_1"
style="@style/MainBtnNormal"
android:text="1" />
<Button
android:id="@+id/bt_percent"
style="@style/MainBtn"
android:background="@drawable/white_bg_border_1_bottomleft_selector"
android:text="%" />
</LinearLayout>
<!--第二部分del 8 52 0-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/bt_del"
style="@style/MainBtnNormal"
android:text="DEL" />
<Button
android:id="@+id/bt_8"
style="@style/MainBtnNormal"
android:text="8" />
<Button
android:id="@+id/bt_5"
style="@style/MainBtnNormal"
android:text="5" />
<Button
android:id="@+id/bt_2"
style="@style/MainBtnNormal"
android:text="2" />
<Button
android:id="@+id/bt_0"
style="@style/MainBtnNormal"
android:text="0" />
</LinearLayout>
<!--地三部分/ 9 6 3 .-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/bt_divider"
style="@style/MainBtnNormal"
android:text="/" />
<Button
android:id="@+id/bt_9"
style="@style/MainBtnNormal"
android:text="9" />
<Button
android:id="@+id/bt_6"
style="@style/MainBtnNormal"
android:text="6" />
<Button
android:id="@+id/bt3"
style="@style/MainBtnNormal"
android:text="3" />
<Button
android:id="@+id/bt_point"
style="@style/MainBtnNormal"
android:text="." />
</LinearLayout>
<!--第四部分x-+=-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/bt_mult"
style="@style/MainBtn"
android:background="@drawable/white_bg_border_1_topright_selector"
android:text="*" />
<Button
android:id="@+id/bt_minus"
style="@style/MainBtnNormal"
android:text="-" />
<Button
android:id="@+id/bt_add"
style="@style/MainBtnNormal"
android:text="+" />
<Button
android:id="@+id/bt_eq"
style="@style/MainBtn_weight2"
android:background="@drawable/white_bg_border_1_bottomright_selector"
android:text="=" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="FullScreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowFullscreen">true</item>
</style>
<style name="MainBtnNormal" >
<item name="android:background">@drawable/white_bg_border_1_selcetor</item>
<item name="android:layout_weight">1</item>
<item name="android:textSize">20sp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">0dp</item>
</style>
<style name="MainBtn" >
<item name="android:layout_weight">1</item>
<item name="android:textSize">20sp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">0dp</item>
</style>
<style name="MainBtn_weight2" >
<item name="android:layout_weight">2</item>
<item name="android:textSize">20sp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">0dp</item>
</style>
</resources>
清单文件里面记得加上
<activity android:name=".MainActivity"
android:theme="@style/FullScreen"
>
white_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"></corners>
<solid android:color="@android:color/white"></solid>
</shape>
下面样式重复性太高我仅贴出来两个
white_bg_border_1_norml.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="0.5dp"
android:color="#cccccc"
/>
<solid android:color="@android:color/white"></solid>
</shape>
white_bg_border_1_press.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="0.5dp"
android:color="@android:color/holo_orange_dark"
/>
<solid android:color="@android:color/holo_orange_light"></solid>
</shape>
white_bg_border_1_selcetor.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/white_bg_border_1_press" android:state_pressed="true"></item>
<item android:drawable="@drawable/white_bg_border_1_norml"></item>
</selector>
white_bg_border_1_topleft_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:topLeftRadius="5dp"></corners>
<!--<gradient-->
<!--android:startColor="@android:color/white"-->
<!--android:endColor="@android:color/holo_red_dark"/>-->
<stroke
android:width="0.5dp"
android:color="#cccccc"
/>
<solid android:color="@android:color/white"></solid>
</shape>
white_bg_border_1_topleft_press.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:topLeftRadius="5dp"></corners>
<!--<gradient-->
<!--android:startColor="@android:color/white"-->
<!--android:endColor="@android:color/holo_red_dark"/>-->
<stroke
android:width="0.5dp"
android:color="@android:color/holo_orange_dark"
/>
<solid android:color="@android:color/holo_orange_light"></solid>
</shape>
white_bg_border_1_topleft_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/white_bg_border_1_topleft_press" android:state_pressed="true"></item>
<item android:drawable="@drawable/white_bg_border_1_topleft_normal"></item>
</selector>
大家想学习的话可以点击慕课网视频教学