1.使用xml设置控件 的样式背景
edit_bg.xml:用于装饰计算器的显示框
//设置边角
//设置填充色
//设置边框的宽度
number_bk.xml:用于装饰数字按钮的样式背景
style.xml:将上面的样式背景放入其中,我们可以通过style.xml文件定义通用控件的相同属性
activity_main.xml:
android.support.v7.widget.
GridLayout装饰控件时,一般不使用源生控件,而是使用控件包
//设置列权重
package com.example.wwj_fly.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayout;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = MainActivity.class.getSimpleName();
private TextView edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (TextView) findViewById(R.id.edit_number);
GridLayout gridLayout = (GridLayout) findViewById(R.id.layout);
for (int i = 0; i < gridLayout.getChildCount(); i++) {//得到GridLayout布局中控件的数量
TextView text = (TextView)gridLayout.getChildAt(i);//通过这种方式来获取GridLayout布局的所用控件
if (!text.equals(edit)) {
text.setOnClickListener(this);
}
}
}
@Override
public void onClick(View v) {
TextView text = (TextView) v;
String str = text.getText().toString();
Pattern compile;
Matcher matcher;
StringBuilder builder = new StringBuilder(edit.getText());
switch (str) {
case "C":
builder.delete(0,builder.length());
break;
case "+/-":
break;
case "/":
builder.append(str);
builder = Jisuan(builder, edit);
// compile = Pattern.compile("([\\d.?]+)([*/+-])([\\d.?]+)");
// matcher = compile.matcher(edit.getText());
// while (matcher.find()){
// Log.d(TAG, "onClick: "+matcher.group());
// String num1 = matcher.group(1);
// String num2 = matcher.group(3);
// float one = Float.parseFloat(num1);
// float two = Float.parseFloat(num2);
// float result = 0;
// switch (matcher.group(2)) {
// case "*":
// result = one * two;
// break;
// case "/":
// result = one / two;
// break;
// case "+":
// result = one + two;
// break;
// case "-":
// result = one - two;
// break;
// }
// builder.replace(matcher.start(),matcher.end(),String.valueOf(result));
// }
break;
case "*":
builder.append(str);
builder = Jisuan(builder,edit);
// compile = Pattern.compile("([\\d.?]+)([*/+-])([\\d.?]+)");
// matcher = compile.matcher(edit.getText());
// while (matcher.find()){
// Log.d(TAG, "onClick: "+matcher.group());
// String num1 = matcher.group(1);
// String num2 = matcher.group(3);
// float one = Float.parseFloat(num1);
// float two = Float.parseFloat(num2);
// float result = 0;
// switch (matcher.group(2)) {
// case "*":
// result = one * two;
// break;
// case "/":
// result = one / two;
// break;
// case "+":
// result = one + two;
// break;
// case "-":
// result = one - two;
// break;
// }
// builder.replace(matcher.start(),matcher.end(),String.valueOf(result));
// }
break;
case "+":
builder.append(str);
builder = Jisuan(builder,edit);
// compile = Pattern.compile("([\\d.?]+)([*/+-])([\\d.?]+)");
// matcher = compile.matcher(edit.getText());
// while (matcher.find()){
// Log.d(TAG, "onClick: "+matcher.group());
// String num1 = matcher.group(1);
// String num2 = matcher.group(3);
// float one = Float.parseFloat(num1);
// float two = Float.parseFloat(num2);
// float result = 0;
// switch (matcher.group(2)) {
// case "*":
// result = one * two;
// break;
// case "/":
// result = one / two;
// break;
// case "+":
// result = one + two;
// break;
// case "-":
// result = one - two;
// break;
// }
// builder.replace(matcher.start(),matcher.end(),String.valueOf(result));
// }
break;
case "-":
builder.append(str);
builder = Jisuan(builder,edit);
// compile = Pattern.compile("([\\-?][\\d.?]+)([*/+-])([\\d.?]+)");
// matcher = compile.matcher(edit.getText());
// while (matcher.find()){
// Log.d(TAG, "onClick: "+matcher.group());
// String num1 = matcher.group(1);
// String num2 = matcher.group(3);
// float one = Float.parseFloat(num1);
// float two = Float.parseFloat(num2);
// float result = 0;
// switch (matcher.group(2)) {
// case "*":
// result = one * two;
// break;
// case "/":
// result = one / two;
// break;
// case "+":
// result = one + two;
// break;
// case "-":
// result = one - two;
// break;
// }
// builder.replace(matcher.start(),matcher.end(),String.valueOf(result));
// }
// break;
// case ".":
// if (!builder.toString().contains(".")) {
// if (builder.length() == 0) {
// builder.append("0");
// }
// builder.append(str);
// }
// break;
case "=":
builder = Jisuan(builder,edit);
// compile = Pattern.compile("([\\d.?]+)([*/+-])([\\d.?]+)");
// matcher = compile.matcher(edit.getText());
// while (matcher.find()){
// Log.d(TAG, "onClick: "+matcher.group());
// String num1 = matcher.group(1);
// String num2 = matcher.group(3);
// float one = Float.parseFloat(num1);
// float two = Float.parseFloat(num2);
// float result = 0;
// switch (matcher.group(2)) {
// case "*":
// result = one * two;
// break;
// case "/":
// result = one / two;
// break;
// case "+":
// result = one + two;
// break;
// case "-":
// result = one - two;
// break;
// }
// builder.replace(matcher.start(),matcher.end(),String.valueOf(result));
// }
break;
default:
builder.append(str);
// if (builder.length() >= 3) {
// builder.replace(0,builder.length(),Float.toString(result));
// }
break;
}
edit.setText(builder.toString());
}
public static StringBuilder Jisuan(StringBuilder builder,TextView edit){
Pattern compile = Pattern.compile("([-]?[\\d.?]+)([*/+-])([- ]?[\\d.?]+)");
Matcher matcher = compile.matcher(edit.getText());
while (matcher.find()){
Log.d(TAG, "onClick: "+matcher.group());
String num1 = matcher.group(1);
String num2 = matcher.group(3);
float one = Float.parseFloat(num1);
float two = Float.parseFloat(num2);
float result = 0;
switch (matcher.group(2)) {
case "*":
result = one * two;
break;
case "/":
result = one / two;
break;
case "+":
result = one + two;
break;
case "-":
result = one - two;
break;
}
builder.replace(matcher.start(),matcher.end(),String.valueOf(result));
}
return builder;
}
}
运行后的效果: