1.XML布局文件源码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
指定该视图边缘与内部内容之间的空白距离
android:padding="5dp"
指定布局内部视图与本线性布局的对齐方式。而layout_gravity指定该视图与上级视图的对齐方式。
android:gravity="top|center"
指定线性布局的方向,horizontal表示水平布局,vertical表示垂直布局。
android:orientation="vertical">
ScrollView滚动视图,其中:垂直方向滚动,layout_width设为match_parent,layout_height设为wrap_parent。水平方向滚动正好相反。
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
内部text布局在该布局中间
android:gravity="center"
android:text="简单计算器"
android:textColor="#000000"
android:textSize="22sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_white_with_stroke"
android:orientation="vertical">
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
内部text显示在该布局右下角
android:gravity="right|bottom"
android:lines="3"
android:maxLines="3"
指定滚动条的方向,取值vertical,如果不指定将不显示滚动条
android:scrollbars="vertical"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_cancel"
Button的风格引用的是style中的btu_cal
style="@style/btn_cal"
android:text="CE" />
<Button
android:id="@+id/btn_divide"
style="@style/btn_cal"
android:text="÷" />
<Button
android:id="@+id/btn_multiply"
style="@style/btn_cal"
android:text="×" />
<Button
android:id="@+id/btn_clear"
style="@style/btn_cal"
android:text="C" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_seven"
style="@style/btn_cal"
android:text="7" />
<Button
android:id="@+id/btn_eight"
style="@style/btn_cal"
android:text="8" />
<Button
android:id="@+id/btn_nine"
style="@style/btn_cal"
android:text="9" />
<Button
android:id="@+id/btn_plus"
style="@style/btn_cal"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_four"
style="@style/btn_cal"
android:text="4" />
<Button
android:id="@+id/btn_five"
style="@style/btn_cal"
android:text="5" />
<Button
android:id="@+id/btn_six"
style="@style/btn_cal"
android:text="6" />
<Button
android:id="@+id/btn_minus"
style="@style/btn_cal"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_one"
style="@style/btn_cal"
android:text="1" />
<Button
android:id="@+id/btn_two"
style="@style/btn_cal"
android:text="2" />
<Button
android:id="@+id/btn_three"
style="@style/btn_cal"
android:text="3" />
ImageButton图像视图,这里用图片显示开根号运算符。
<ImageButton
android:id="@+id/ib_sqrt"
android:layout_width="0dp"
android:layout_height="match_parent"
占上级视图一个权重。
android:layout_weight="1"
android:scaleType="centerInside"
android:src="@drawable/sqrt"
android:background="@drawable/btn_nine_selector"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_zero"
style="@style/btn_cal"
占上级视图两个权重。
android:layout_weight="2"
android:text="0" />
<Button
android:id="@+id/btn_dot"
style="@style/btn_cal"
android:text="." />
<Button
android:id="@+id/btn_equal"
style="@style/btn_cal"
android:text="=" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
2.style.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="btn_cal">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
指定当前视图的宽或高占上级线性布局的权重(在下级视图的节点中设置)。
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
<item name="android:textColor">@color/black</item>
<item name="android:textSize">30sp</item>
<item name="android:background">@drawable/btn_nine_selector</item>
</style>
</resources>
3.java源码
package com.example.junior;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.junior.util.Arith;
/**
* Created by ouyangshen on 2016/9/15.
*/
//
public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
private final static String TAG = "CalculatorActivity";
private TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//savedInstanceState是保存当前Activity的状态信息
setContentView(R.layout.activity_calculator);
tv_result = (TextView) findViewById(R.id.tv_result);//获取tv_result
tv_result.setMovementMethod(new ScrollingMovementMethod());//这个TextView使用setMovementMethod来实现滑动
//设置监听器
findViewById(R.id.btn_cancel).setOnClickListener(this);
findViewById(R.id.btn_divide).setOnClickListener(this);
findViewById(R.id.btn_multiply).setOnClickListener(this);
findViewById(R.id.btn_clear).setOnClickListener(this);
findViewById(R.id.btn_seven).setOnClickListener(this);
findViewById(R.id.btn_eight).setOnClickListener(this);
findViewById(R.id.btn_nine).setOnClickListener(this);
findViewById(R.id.btn_plus).setOnClickListener(this);
findViewById(R.id.btn_four).setOnClickListener(this);
findViewById(R.id.btn_five).setOnClickListener(this);
findViewById(R.id.btn_six).setOnClickListener(this);
findViewById(R.id.btn_minus).setOnClickListener(this);
findViewById(R.id.btn_one).setOnClickListener(this);
findViewById(R.id.btn_two).setOnClickListener(this);
findViewById(R.id.btn_three).setOnClickListener(this);
findViewById(R.id.btn_zero).setOnClickListener(this);
findViewById(R.id.btn_dot).setOnClickListener(this);
findViewById(R.id.btn_equal).setOnClickListener(this);
findViewById(R.id.ib_sqrt).setOnClickListener(this);
}
@Override
public void onClick(View v) {
int resid = v.getId();
String inputText;
if (resid == R.id.ib_sqrt) {
inputText = "√";
} else {
inputText = ((TextView) v).getText().toString();
}
Log.d(TAG, "resid="+resid+",inputText="+inputText);
if (resid == R.id.btn_clear) {
clear("");
} else if (resid == R.id.btn_cancel) {
if (operator.equals("") == true) {
if (firstNum.length() == 1) {
firstNum = "0";
} else if (firstNum.length() > 0) {
firstNum = firstNum.substring(0, firstNum.length() - 1);
} else {
Toast.makeText(this, "没有可取消的数字了", Toast.LENGTH_SHORT).show(); //Toast 是一个 View 视图,快速的为用户显示少量的信息。
return;
}
showText = firstNum;
tv_result.setText(showText);
} else {
if (nextNum.length() == 1) {
nextNum = "";
} else if (nextNum.length() > 0) {
nextNum = nextNum.substring(0, nextNum.length() - 1);
} else {
Toast.makeText(this, "没有可取消的数字了", Toast.LENGTH_SHORT).show();
return;
}
showText = showText.substring(0, showText.length() - 1);
tv_result.setText(showText);
}
} else if (resid == R.id.btn_equal) {
if (operator.length() == 0 || operator.equals("=") == true) {
Toast.makeText(this, "请输入运算符", Toast.LENGTH_SHORT).show();
return;
} else if (nextNum.length() <= 0) {
Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
return;
}
if (caculate() == true) {
operator = inputText;
showText = showText + "=" + result;
tv_result.setText(showText);
} else {
return;
}
} else if (resid == R.id.btn_plus || resid == R.id.btn_minus
|| resid == R.id.btn_multiply || resid == R.id.btn_divide ) {
if (firstNum.length() <= 0) {
Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
return;
}
if (operator.length() == 0 || operator.equals("=") == true
|| operator.equals("√") == true) {
operator = inputText;// 操作符
showText = showText + operator;
tv_result.setText(showText);
} else {
Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
return;
}
} else if (resid == R.id.ib_sqrt) {
if (firstNum.length() <= 0) {
Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
return;
}
if (Double.parseDouble(firstNum) < 0) {
Toast.makeText(this, "开根号的数值不能小于0", Toast.LENGTH_SHORT).show();
return;
}
result = String.valueOf(Math.sqrt(Double.parseDouble(firstNum)));
firstNum = result;
nextNum = "";
operator = inputText;
showText = showText + "√=" + result;
tv_result.setText(showText);
Log.d(TAG, "result="+result+",firstNum="+firstNum+",operator="+operator);
} else {
if (operator.equals("=") == true) {
operator = "";
firstNum = "";
showText = "";
}
if (resid == R.id.btn_dot) {
inputText = ".";
}
if (operator.equals("") == true) {
firstNum = firstNum + inputText;
} else {
nextNum = nextNum + inputText;
}
showText = showText + inputText;
tv_result.setText(showText);
}
return;
}
private String operator = ""; // 操作符
private String firstNum = ""; // 前一个操作数
private String nextNum = ""; // 后一个操作数
private String result = ""; // 当前的计算结果
private String showText = ""; // 显示的文本内容
// 开始加减乘除四则运算
private boolean caculate() {
if (operator.equals("+") == true) {
result = String.valueOf(Arith.add(firstNum, nextNum));
} else if (operator.equals("-") == true) {
result = String.valueOf(Arith.sub(firstNum, nextNum));
} else if (operator.equals("×") == true) {
result = String.valueOf(Arith.mul(firstNum, nextNum));
} else if (operator.equals("÷") == true) {
if ("0".equals(nextNum)) {
Toast.makeText(this, "被除数不能为零", Toast.LENGTH_SHORT).show();
return false;
} else {
result = String.valueOf(Arith.div(firstNum, nextNum));
}
}
firstNum = result;
nextNum = "";
return true;
}
// 清空并初始化
private void clear(String text) {
showText = text;
tv_result.setText(showText);
operator = "";
firstNum = "";
nextNum = "";
result = "";
}
}
最后在配置文件中配置一下就OK了,
注:代码中的注释是我写博客时加的,复制后将注释删除或者加上注释符。