android---仿oppo简单计算器

界面设计仿oppo,里面用到了xml中的shape定义.源码解释很详细

1.第一种按键效果

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"
    >
    <!-- 填充颜色 -->
    <solid android:color="#FFFFFF"/>
    <!-- 设置四个角为弧形 -->
    <!-- android:radius 弧形的半径 -->
    <corners android:radius="360dip"/>
    <!-- 设置描边宽度和颜色 -->
    <stroke android:width="1dip" android:color="#FFCCCC"/>

    <!-- padding: Button 里面的文字与Button边界的间隔 -->
    <padding 
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp"
    />  
</shape>

2.第二种按键效果

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#ffffff"/>
    <stroke android:width="1dp" android:color="#FFCCCC"/>

</shape>

3.主布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.niuli.calculator.MainActivity" >

    <EditText
        android:id="@+id/editinput"
        android:layout_width="match_parent"
        android:singleLine="true"
        android:layout_height="70dp"
        android:background="@drawable/btnshape2" />

    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">
    <Button
        android:id="@+id/btnbackspace"
        android:layout_width="220dp"
        android:layout_height="50dp"
        android:layout_marginLeft="6dp"
        android:background="@drawable/btnshape2"
        android:text="backspace" />

    <Button
        android:id="@+id/btndiv"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btnshape1"
        android:layout_marginLeft="53dp"
        android:text="/" />

    </LinearLayout>

    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:layout_marginLeft="6dp"
        android:layout_marginTop="10dp"
        android:orientation="vertical">
    <!-- 1 -->
     <TableRow 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:layout_marginTop="20dp">
    <Button
        android:id="@+id/btn1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btnshape1"
        android:text="1" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="45dp"
        android:background="@drawable/btnshape1"
        android:text="2" />
    <Button
        android:id="@+id/btn3"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="45dp"
        android:background="@drawable/btnshape1"
        android:text="3" />
    <Button
        android:id="@+id/btnX"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/btnshape1"
        android:text="*" />
     </TableRow>
        <!-- 2 -->
     <TableRow 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:layout_marginTop="20dp">
    <Button
        android:id="@+id/btn4"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btnshape1"
        android:text="4" />

    <Button
        android:id="@+id/btn5"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="45dp"
        android:background="@drawable/btnshape1"
        android:text="5" />
    <Button
        android:id="@+id/btn6"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="45dp"
        android:background="@drawable/btnshape1"
        android:text="6" />
    <Button
        android:id="@+id/btnjian"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/btnshape1"
        android:text="-" />
     </TableRow>
         <!-- 3 -->
     <TableRow 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:layout_marginTop="20dp">
    <Button
        android:id="@+id/btn7"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btnshape1"
        android:text="7" />

    <Button
        android:id="@+id/btn8"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="45dp"
        android:background="@drawable/btnshape1"
        android:text="8" />
    <Button
        android:id="@+id/btn9"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="45dp"
        android:background="@drawable/btnshape1"
        android:text="9" />
    <Button
        android:id="@+id/btnjia"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/btnshape1"
        android:text="+" />
     </TableRow>
         <!-- 4 -->
     <TableRow 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:layout_marginTop="20dp">
    <Button
        android:id="@+id/btn0"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btnshape1"
        android:text="0" />

    <Button
        android:id="@+id/btnpoint"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="45dp"
        android:background="@drawable/btnshape1"
        android:text="." />
    <Button
        android:id="@+id/btnclear"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="45dp"
        android:background="@drawable/btnshape1"
        android:text="C" />
    <Button
        android:id="@+id/btnsum"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/btnshape1"
        android:text="=" />

     </TableRow>
    </LinearLayout>

</LinearLayout>

4.主代码

package com.niuli.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener{

    private double input1 = 0, input2 = 0, result = 0;
    private int option = 0;
    boolean isClicked = false;
    private EditText input;
    private Button Backspace, C, Divide, Mul, Plus, Sub, Sum, Nine, Eight,
            Seven, Six, Five, Four, Three, Two, One, Zero, Point;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input = (EditText) findViewById(R.id.editinput);

        Backspace = (Button) findViewById(R.id.btnbackspace);
        Backspace.setOnClickListener(this);

        C = (Button) findViewById(R.id.btnclear);
        C.setOnClickListener(this);

        Divide = (Button) findViewById(R.id.btndiv);
        Divide.setOnClickListener(this);

        Mul = (Button) findViewById(R.id.btnX);
        Mul.setOnClickListener(this);

        Plus = (Button) findViewById(R.id.btnjia);
        Plus.setOnClickListener(this);

        Sub = (Button) findViewById(R.id.btnjian);
        Sub.setOnClickListener(this);

        Sum = (Button) findViewById(R.id.btnsum);
        Sum.setOnClickListener(this);

        Nine = (Button) findViewById(R.id.btn9);
        Nine.setOnClickListener(this);

        Eight = (Button) findViewById(R.id.btn8);
        Eight.setOnClickListener(this);

        Seven = (Button) findViewById(R.id.btn7);
        Seven.setOnClickListener(this);

        Six = (Button) findViewById(R.id.btn6);
        Six.setOnClickListener(this);

        Five = (Button) findViewById(R.id.btn5);
        Five.setOnClickListener(this);

        Four = (Button) findViewById(R.id.btn4);
        Four.setOnClickListener(this);

        Three = (Button) findViewById(R.id.btn3);
        Three.setOnClickListener(this);

        Two = (Button) findViewById(R.id.btn2);
        Two.setOnClickListener(this);

        One = (Button) findViewById(R.id.btn1);
        One.setOnClickListener(this);

        Zero = (Button) findViewById(R.id.btn0);
        Zero.setOnClickListener(this);

        Point = (Button) findViewById(R.id.btnpoint);
        Point.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btnbackspace:
            String str = input.getText().toString();
            try {
                input.setText(str.substring(0, str.length() - 1));
            } catch (Exception e) {
                input.setText("");
            }
            break;

        case R.id.btnclear:
            input.setText(null);
            break;

        case R.id.btn0:
            if (isClicked) {
                input.setText(null);
                isClicked = false;
            }

            String str_0 = input.getText().toString();
            str_0 += "0";
            input.setText(str_0);
            break;

        case R.id.btn1:
            if (isClicked) {
                input.setText(null);
                isClicked = false;
            }

            String str_1 = input.getText().toString();
            str_1 += "1";
            input.setText(str_1);
            break;

        case R.id.btn2:
            if (isClicked) {
                input.setText(null);
                isClicked = false;
            }

            String str_2 = input.getText().toString();
            str_2 += "2";
            input.setText(str_2);
            break;

        case R.id.btn3:
            if (isClicked) {
                input.setText(null);
                isClicked = false;
            }

            String str_3 = input.getText().toString();
            str_3 += "3";
            input.setText(str_3);
            break;

        case R.id.btn4:
            if (isClicked) {
                input.setText(null);
                isClicked = false;
            }

            String str_4 = input.getText().toString();
            str_4 += "4";
            input.setText(str_4);
            break;

        case R.id.btn5:
            if (isClicked) {
                input.setText(null);
                isClicked = false;
            }

            String str_5 = input.getText().toString();
            str_5 += "5";
            input.setText(str_5);
            break;

        case R.id.btn6:
            if (isClicked) {
                input.setText(null);
                isClicked = false;
            }

            String str_6 = input.getText().toString();
            str_6 += "6";
            input.setText(str_6);
            break;

        case R.id.btn7:
            if (isClicked) {
                input.setText(null);
                isClicked = false;
            }

            String str_7 = input.getText().toString();
            str_7 += "7";
            input.setText(str_7);
            break;

        case R.id.btn8:
            if (isClicked) {
                input.setText(null);
                isClicked = false;
            }

            String str_8 = input.getText().toString();
            str_8 += "8";
            input.setText(str_8);
            break;

        case R.id.btn9:
            if (isClicked) {
                input.setText(null);
                isClicked = false;
            }

            String str_9 = input.getText().toString();
            str_9 += "9";
            input.setText(str_9);
            break;

        case R.id.btnpoint:
            if (isClicked) {
                input.setText(null);
                isClicked = false;
            }

            String str_point = input.getText().toString();
            str_point += ".";
            input.setText(str_point);
            break;

        case R.id.btndiv:
            if (input.getText().toString().equals("")) {
                return;
            }

            input1 = Double.parseDouble(input.getText().toString());
            input.setText(null);
            option = 1;
            isClicked = false;
            break;

        case R.id.btnX:
            if (input.getText().toString().equals("")) {
                return;
            }

            input1 = Double.parseDouble(input.getText().toString());
            input.setText(null);
            option = 2;
            isClicked = false;
            break;

        case R.id.btnjia:
            if (input.getText().toString().equals("")) {
                return;
            }

            input1 = Double.parseDouble(input.getText().toString());
            input.setText(null);
            option = 3;
            isClicked = false;
            break;

        case R.id.btnjian:
            if (input.getText().toString().equals("")) {
                return;
            }

            input1 = Double.parseDouble(input.getText().toString());
            input.setText(null);
            option = 4;
            isClicked = false;
            break;

        case R.id.btnsum:
            String myStringEqu = input.getText().toString();
            if (myStringEqu.equals("")) {
                return;
            }
            input2 = Double.valueOf(myStringEqu);
            input.setText(null);

            switch (option) {
            case 0:
                result = 0;
                break;

            case 1:
                result = input1 / input2;
                break;

            case 2:
                result = input1 * input2;
                break;

            case 3:
                result = input1 + input2;
                break;

            case 4:
                result = input1 - input2;
                break;
            }

            input.setText(Double.toString(result));
            isClicked = true;
            break;

        default:
            break;
        }
    }



}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2015-08-17 16:11  牛李  阅读(548)  评论(0编辑  收藏  举报

导航