一个简易的计算器,边学边写,还有很多不足希望多多指教
先看布局,整体线性布局,上面两个TextView,一个存放计算过程,一个给出结果,然后一个table布局,button没有声明在xml里,用Java写的,好处是比较方便,坏处是我不会怎么让button适应屏幕,看起来有点丑。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textView1"
android:layout_width="382dp"
android:layout_height="67dp"
android:layout_columnSpan="4"
android:layout_gravity="right"
android:layout_marginLeft="4px"
android:layout_marginRight="4px"
android:background="#eee"
android:padding="5px"
android:textColor="#000" />
<TextView
android:id="@+id/textView2"
android:layout_width="382dp"
android:layout_height="67dp"
android:layout_columnSpan="4"
android:layout_gravity="right"
android:layout_marginLeft="4px"
android:layout_marginRight="4px"
android:background="#eee"
android:padding="5px"
android:textColor="#000" />
<Button
android:id="@+id/buttonclear"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_columnSpan="4"
android:text="clear" />
<TableLayout
android:id="@+id/tablelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
然后看Java部分,我用数组button建立在TableRow上,再加入TextView,同时监听方式是独立类(外部类)实现事件监听
package com.example.w.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
String[] strs = {"7", "8", "9", "/",
"4", "5", "6", "-",
"1", "2", "3", "+",
".", "0", "*", "="
};
TextView thequestion;
TextView theanswer;
Button butt;
private StringBuilder sb = new StringBuilder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
class mOnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
sb.append(((Button) view).getText().toString());
thequestion.setText(sb);
}
}
public double Count()
{
Character op='+';
sb=sb.deleteCharAt(sb.length()-1);
int j=0;
for (int i=0;i<sb.length();i++) {
switch (sb.charAt(i)) {
case '+':
case '-':
case '*':
case '/':op=sb.charAt(i);j=i;break;
default:
break;
}
}
String front=sb.substring(0,j);
String back=sb.substring(j+1,sb.length());
double res=0;
switch (op) {
case '+':res= Double.parseDouble(front)+Double.parseDouble(back);break;
case '-':res= Double.parseDouble(front)-Double.parseDouble(back);break;
case '*':res= Double.parseDouble(front)*Double.parseDouble(back);break;
case '/':res= Double.parseDouble(front)/Double.parseDouble(back);break;
default:
break;
}
return res;
}
public void initView() {
TableLayout TableLayout=( TableLayout)findViewById(R.id.tablelayout);
thequestion=findViewById(R.id.textView1);
theanswer=findViewById(R.id.textView2);
butt=findViewById(R.id.buttonclear);
butt=findViewById(R.id.buttonclear);
butt.setOnClickListener(new View.OnClickListener()//clear按钮初始化
{
public void onClick(View view) {
thequestion.setText("");
theanswer.setText("");
sb=new StringBuilder();
}
});
Button[] b = new Button[strs.length];
int k=0;
// TableRow []row = { findViewById(R.id.tablerow1), findViewById(R.id.tablerow2),findViewById(R.id.tablerow3), findViewById(R.id.tablerow4)
// };
for (int i = 0; i < 4; i++) {
TableRow row=new TableRow(getApplicationContext());
// row.setMinimumHeight(120);
for (int j = 0; j < 4; j++) {
b[k] = new Button(getApplicationContext());
b[k].setText(strs[k]);
if(k==strs.length-1)//“=”单独处理
{
b[k].setOnClickListener(new mOnClickListener() {
public void onClick(View view) {
super.onClick(view);
theanswer.setText(Count()+"");//结果输出
}
});
}
else {
b[k].setOnClickListener(new mOnClickListener() {
});
}
row.addView(b[k]);
k++;
}
TableLayout.addView(row);
}
}
}
计算部分等于是计算StringBuilder里的一串式子,先去掉最后加入的等号,再寻找运算符,将两边的数字分开。
这个计算器没有考虑到带括号的运算和各种非常规使用的情况,后面有时间再完善。