关闭

写了一个简易计算器

标签: android布局计算器
149人阅读 评论(0) 收藏 举报
分类:

这里写图片描述

  • 功能:能做简单的加减乘除运算。但是只能运算一次…………不能连续运算。

  • 布局源码
<RelativeLayout 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"
    tools:context="com.example.a.MainActivity" >

    <TableLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weightSum="7">


        <TextView
            android:id="@+id/tv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="" 
            android:textSize="24sp"/>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/one"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="1" />

            <Button
                android:id="@+id/two"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="2" />

            <Button
                android:id="@+id/three"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="3" />

            <Button
                android:id="@+id/add"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="+" />
        </TableRow>
          <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="4"
            android:layout_weight="1" >

            <Button
                android:id="@+id/four"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="4" />

            <Button
                android:id="@+id/five"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="5" />

            <Button
                android:id="@+id/six"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="6" />

            <Button
                android:id="@+id/remove"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="-" />
        </TableRow>

           <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/seven"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="7" />

            <Button
                android:id="@+id/eight"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="8" />

            <Button
                android:id="@+id/nine"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="9" />

            <Button
                android:id="@+id/x"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="x" />
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/zero"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="0" />
            <Button 
                android:id="@+id/qingchu"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="C"/>

            <Button
                android:id="@+id/deng"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="=" />

            <Button
                android:id="@+id/chu"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="/" />
        </TableRow>
    </TableLayout>

</RelativeLayout>
  • 逻辑源码
package com.example.a;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    public StringBuffer sb = new StringBuffer();//用于将每次按键按下的值连起来
    private int number = 0;//计数器:按下+为0;按下-为1;按下x为3;按下/为3;
    private TextView tv_xs;
    public long resultnum = 0;//结果值

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_xs = (TextView) findViewById(R.id.tv);
        Button one = (Button) findViewById(R.id.one);
        Button two = (Button) findViewById(R.id.two);
        Button three = (Button) findViewById(R.id.three);
        Button four = (Button) findViewById(R.id.four);
        Button five = (Button) findViewById(R.id.five);
        Button six = (Button) findViewById(R.id.six);
        Button seven = (Button) findViewById(R.id.seven);
        Button eight = (Button) findViewById(R.id.eight);
        Button nine = (Button) findViewById(R.id.nine);
        Button zero = (Button) findViewById(R.id.zero);
        Button add = (Button) findViewById(R.id.add);//加
        Button chu = (Button) findViewById(R.id.chu);//除
        Button remove = (Button) findViewById(R.id.remove);//减
        Button deng = (Button) findViewById(R.id.deng);//等号
        Button x = (Button) findViewById(R.id.x);//乘
        Button qingchu =(Button)findViewById(R.id.qingchu);//清零
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        zero.setOnClickListener(this);
        add.setOnClickListener(this);
        chu.setOnClickListener(this);
        remove.setOnClickListener(this);
        deng.setOnClickListener(this);
        x.setOnClickListener(this);
        qingchu.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        try {
            switch (v.getId()) {
            case R.id.one:
                int one = 1;
                sb.append(one);
                tv_xs.setText(sb.toString());
                break;
            case R.id.two:
                int two = 2;
                sb.append(two);
                tv_xs.setText(sb.toString());
                break;
            case R.id.three:
                int three = 3;
                sb.append(three);
                tv_xs.setText(sb.toString());
                break;
            case R.id.four:
                int four = 4;
                sb.append(four);
                tv_xs.setText(sb.toString());
                break;

            case R.id.five:
                int five = 5;
                sb.append(five);
                tv_xs.setText(sb.toString());
                break;

            case R.id.six:
                int six = 6;
                sb.append(six);
                tv_xs.setText(sb.toString());
                break;

            case R.id.seven:
                int seven = 7;
                sb.append(seven);
                tv_xs.setText(sb.toString());
                break;
            case R.id.eight:
                int eight = 8;
                sb.append(eight);
                tv_xs.setText(sb.toString());
                break;
            case R.id.nine:
                int nine = 9;
                sb.append(nine);
                tv_xs.setText(sb.toString());
                break;
            case R.id.zero:
                int zero = 0;
                sb.append(zero);
                tv_xs.setText(sb.toString());
                break;
            case R.id.add:
                tv_xs.setText("+");
                resultnum = Long.valueOf(sb.toString());
                sb.delete(0, sb.length());
                number = 0;
                break;
            case R.id.remove:
                tv_xs.setText("-");
                resultnum = Long.valueOf(sb.toString());
                sb.delete(0, sb.length());
                number = 1;
                break;
            case R.id.x:
                tv_xs.setText("x");
                resultnum = Long.valueOf(sb.toString());
                sb.delete(0, sb.length());
                number = 2;
                break;
            case R.id.chu:
                tv_xs.setText("%");
                resultnum = Long.valueOf(sb.toString());
                sb.delete(0, sb.length());
                number = 3;
                break;
            case R.id.deng:
                long deng_long = Long.valueOf(sb.toString());
                switch (number) {
                case 0:
                    long relust0 = resultnum + deng_long;
                    tv_xs.setText(relust0 + "");
                    break;
                case 1:
                    long relust1 = resultnum - deng_long;
                    tv_xs.setText(relust1 + "");
                    break;
                case 2:
                    long relust2 = resultnum * deng_long;
                    tv_xs.setText(relust2 + "");
                    break;
                case 3:
                    long relust3 = resultnum / deng_long;
                    tv_xs.setText(relust3 + "");
                    break;

                default:
                    break;
                }
                sb.delete(0, sb.length());
                break;
            case R.id.qingchu:
                tv_xs.setText("");
                sb.delete(0, sb.length());
                break;
            default:
                break;
            }
        } catch (Exception e) {
            // TODO: handle exception
            tv_xs.setText("");
        }

    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2009次
    • 积分:91
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章分类