您的位置:首页 > 其它

计算器布局及其实现

2016-08-25 14:24 190 查看
 1.使用xml设置控件 的样式背景

edit_bg.xml:用于装饰计算器的显示框

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp"/>//设置边角
<solid android:color="#fff"/>//设置填充色
<stroke android:color="#000" android:width="2dp"/>//设置边框的宽度
</shape>
number_bk.xml:用于装饰数字按钮的样式背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp"/>
<solid android:color="#fff"/>
</shape>


operator.xml:用于装饰运算符按钮的样式背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"/>
<gradient android:angle="90"//设置渐变色
android:startColor="#888"
android:endColor="#ccc"/>
</shape>
style.xml:将上面的样式背景放入其中,我们可以通过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="baseButtonStyle">
<item name="android:gravity">center</item>//自建baseButtonStyle类型,用于设置具有相同属性的控件
<item name="android:textStyle">bold</item>
<item name="android:textSize">20sp</item>
<item name="android:layout_margin">10dp</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>

</style>
<style name="operatorStyle" parent="baseButtonStyle">//自建operatorStyle类型,通过parent = "baseButtonStyle" 的方式将baseButtonStyle作为父类
<item name="android:background">@drawable/operator</item>
</style>
<style name="numberStyle" parent="baseButtonStyle">
<item name="android:background">@drawable/number_bk</item>
</style>
</resources>
activity_main.xml:android.support.v7.widget.GridLayout装饰控件时,一般不使用源生控件,而是使用控件包

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout 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"
app:columnCount="4"
app:rowCount="6"
android:background="#ccc"
android:id="@+id/layout"//给GirdLayout布局设置id,通过遍历的方式获取布局里边的各种控件。
tools:context="com.example.wwj_fly.calculator.MainActivity">

<TextView
app:layout_columnSpan="4"
app:layout_columnWeight="1"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:textSize="30sp"
android:gravity="right"
android:padding="10dp"
android:background="@drawable/edit_bg"
android:id="@+id/edit_number"
tools:text="123" />
<TextView
style="@style/operatorStyle"
android:text="C"
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>
<TextView
style="@style/operatorStyle"
android:text="+/"
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>
<TextView
style="@style/operatorStyle"
android:text="/"
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>
<TextView
style="@style/operatorStyle"
android:text="*"
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>
<TextView
style="@style/numberStyle"
android:text="7"
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>
<TextView
style="@style/numberStyle"
android:text="8"
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>
<TextView
style="@style/numberStyle"
android:text="9"
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>
<TextView
style="@style/operatorStyle"
android:text="-"
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>
<TextView
style="@style/numberStyle"
android:text="4"
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>
<TextView
style="@style/numberStyle"
android:text="5"
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>
<TextView
style="@style/numberStyle"
android:text="6"
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>
<TextView
style="@style/operatorStyle"//通过style调用运算符样式背景
android:text="+"
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>

<TextView
style="@style/numberStyle"//同style调用数字按钮的样式背景
android:text="1"
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>
<TextView
style="@style/numberStyle"
android:text="2"
android:background="@drawable/number_bk"
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>
<TextView
style="@style/numberStyle"
android:text="3"
app:layout_rowWeight="1"//设置行权重
app:layout_columnWeight="1"/>//设置列权重
<TextView
android:text="="
app:layout_rowSpan="2"
app:layout_rowWeight="1"
android:background="#ff6f00"
style="@style/operatorStyle"
app:layout_columnWeight="1"/>
<TextView
style="@style/numberStyle"
android:text="0"
app:layout_rowWeight="1"
app:layout_columnSpan="2"
app:layout_columnWeight="1"/>
<TextView
style="@style/numberStyle"
android:text="."
app:layout_rowWeight="1"
app:layout_columnWeight="1"/>
</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;
}

}


运行后的效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: