• android简易计算器的实现


    最近接触了android开发就试着写了一个计算器的小程序:

    在xml文件中的布局代码如下:

      1 <?xml version="1.0" encoding="utf-8"?>
      2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      3     xmlns:tools="http://schemas.android.com/tools"
      4     android:id="@+id/activity_ji_suan02"
      5     android:layout_width="match_parent"
      6     android:layout_height="match_parent"
      7     android:orientation="vertical"
      8     tools:context="com.example.whs.sample01_1_activity.JiSuanActivity02">
      9 
     10     <TextView
     11         android:id="@+id/tv"
     12         android:layout_width="match_parent"
     13         android:layout_height="40dp"
     14         android:textSize="30dp"
     15         android:text="0"
     16         android:textColor="#ff0000"
     17         android:gravity="center_vertical|right"
     18         android:layout_marginRight="5dp"
     19         android:layout_marginLeft="5dp"
     20         android:background="#FFFF00"
     21         />
     22     <!--7 8 9 + -->
     23     <LinearLayout
     24         android:layout_width="match_parent"
     25         android:layout_height="wrap_content"
     26         android:paddingTop="5dp"
     27         android:orientation="horizontal"
     28         >
     29 
     30         <Button
     31             android:id="@+id/Button07"
     32             android:text="7"
     33             android:textSize="25dp"
     34             android:layout_width="80dp"
     35             android:layout_height="wrap_content" />
     36         <Button
     37             android:id="@+id/Button08"
     38             android:text="8"
     39             android:textSize="25dp"
     40             android:layout_width="80dp"
     41             android:layout_height="wrap_content" />
     42         <Button
     43             android:id="@+id/Button09"
     44             android:text="9"
     45             android:textSize="25dp"
     46             android:layout_width="80dp"
     47             android:layout_height="wrap_content" />
     48         <Button
     49             android:id="@+id/ButtonJia"
     50             android:text="+"
     51             android:textSize="25dp"
     52             android:layout_width="80dp"
     53             android:layout_height="wrap_content" />
     54 
     55     </LinearLayout>
     56 
     57     <!--4 5 6 - -->
     58     <LinearLayout
     59         android:layout_width="match_parent"
     60         android:layout_height="wrap_content"
     61         android:paddingTop="5dp"
     62         android:orientation="horizontal"
     63         >
     64 
     65         <Button
     66             android:id="@+id/Button04"
     67             android:text="4"
     68             android:textSize="25dp"
     69             android:layout_width="80dp"
     70             android:layout_height="wrap_content" />
     71         <Button
     72             android:id="@+id/Button05"
     73             android:text="5"
     74             android:textSize="25dp"
     75             android:layout_width="80dp"
     76             android:layout_height="wrap_content" />
     77         <Button
     78             android:id="@+id/Button06"
     79             android:text="6"
     80             android:textSize="25dp"
     81             android:layout_width="80dp"
     82             android:layout_height="wrap_content" />
     83         <Button
     84             android:id="@+id/ButtonJian"
     85             android:text="-"
     86             android:textSize="25dp"
     87             android:layout_width="80dp"
     88             android:layout_height="wrap_content" />
     89 
     90     </LinearLayout>
     91 
     92     <!--1 2 3 * -->
     93     <LinearLayout
     94         android:layout_width="match_parent"
     95         android:layout_height="wrap_content"
     96         android:paddingTop="5dp"
     97         android:orientation="horizontal"
     98         >
     99 
    100         <Button
    101             android:id="@+id/Button01"
    102             android:text="1"
    103             android:textSize="25dp"
    104             android:layout_width="80dp"
    105             android:layout_height="wrap_content" />
    106         <Button
    107             android:id="@+id/Button02"
    108             android:text="2"
    109             android:textSize="25dp"
    110             android:layout_width="80dp"
    111             android:layout_height="wrap_content" />
    112         <Button
    113             android:id="@+id/Button03"
    114             android:text="3"
    115             android:textSize="25dp"
    116             android:layout_width="80dp"
    117             android:layout_height="wrap_content" />
    118         <Button
    119             android:id="@+id/ButtonCheng"
    120             android:text="*"
    121             android:textSize="25dp"
    122             android:layout_width="80dp"
    123             android:layout_height="wrap_content" />
    124 
    125     </LinearLayout>
    126 
    127     <!--0 C = / -->
    128     <LinearLayout
    129         android:layout_width="match_parent"
    130         android:layout_height="wrap_content"
    131         android:paddingTop="5dp"
    132         android:orientation="horizontal"
    133         >
    134 
    135         <Button
    136             android:id="@+id/Button00"
    137             android:text="0"
    138             android:textSize="25dp"
    139             android:layout_width="80dp"
    140             android:layout_height="wrap_content" />
    141         <Button
    142             android:id="@+id/ButtonC"
    143             android:text="C"
    144             android:textSize="25dp"
    145             android:layout_width="80dp"
    146             android:layout_height="wrap_content" />
    147         <Button
    148             android:id="@+id/ButtonDeng"
    149             android:text="="
    150             android:textSize="25dp"
    151             android:layout_width="80dp"
    152             android:layout_height="wrap_content" />
    153         <Button
    154             android:id="@+id/ButtonChu"
    155             android:text="/"
    156             android:textSize="25dp"
    157             android:layout_width="80dp"
    158             android:layout_height="wrap_content" />
    159 
    160     </LinearLayout>
    161 
    162 
    163 
    164 
    165 
    166 </LinearLayout>

    在activity中实现具体的功能

      1 package com.example.whs.sample01_1_activity;
      2 
      3 import android.provider.Settings;
      4 import android.support.v7.app.AppCompatActivity;
      5 import android.os.Bundle;
      6 import android.view.View;
      7 import android.widget.Button;
      8 import android.widget.TextView;
      9 import android.widget.Toast;
     10 
     11 public class JiSuanActivity02 extends AppCompatActivity {
     12 
     13     TextView tv;
     14     int[] buttons;      //数字按钮数组
     15     int result;
     16     int result0;
     17     int  result1;
     18 
     19     //按钮对象声明
     20     Button buttonC;
     21     Button buttonJia;
     22     Button buttonJian;
     23     Button buttonCheng;
     24     Button buttonChu;
     25     Button buttonDengyu;
     26 
     27     String str1;    //旧输入的值
     28     String str2;    //新输入的值
     29 
     30     int flag=0;     //计算标志位,0第一次输入;1加; 2减; 3乘; 4除; 5等于
     31     Button temp;
     32 
     33     @Override
     34     protected void onCreate(Bundle savedInstanceState) {
     35         super.onCreate(savedInstanceState);
     36         setContentView(R.layout.activity_ji_suan02);
     37 
     38         initButton();
     39         //清空按钮点击事件
     40         buttonC.setOnClickListener(new View.OnClickListener() {
     41             @Override
     42             public void onClick(View view) {
     43                 str1 = "";
     44                 str2 = "";
     45                 tv.setText("0");
     46                 result = 0;
     47                 result1 = 0;
     48                 result0 = 0;
     49                 flag = 0;
     50             }
     51         });
     52 
     53         //监听
     54         for (int i = 0; i < buttons.length; i++){
     55             temp = getBtnForId(buttons[i]);
     56             temp.setOnClickListener(new View.OnClickListener() {
     57                 @Override
     58                 public void onClick(View view) {
     59                     if (flag != 0){
     60                         str1 = "";
     61                     }else {
     62                         str1 = tv.getText().toString().trim();
     63                         if (str1.equals("0")){
     64                             str1 = "";
     65                         }
     66                     }
     67 
     68                     str1 = str1 + String.valueOf(((Button)view).getText()); //获取新值
     69                     tv.setText(str1);
     70                 }
     71             });
     72         }
     73 
     74         buttonListener(buttonJia, 1);
     75         buttonListener(buttonJian, 2);
     76         buttonListener(buttonCheng, 3);
     77         buttonListener(buttonChu, 4);
     78 
     79         buttonDengyu.setOnClickListener(new View.OnClickListener() {
     80             @Override
     81             public void onClick(View view) {
     82 
     83                 result1 = Integer.parseInt(str1);
     84 
     85                 if (flag == 1){
     86                     result = result0 + result1;
     87                 }else if (flag == 2){
     88                     result = result0 - result1;
     89                 }else if (flag == 3){
     90                     result = result0 * result1;
     91                 }else if (flag == 4){
     92                     if (result1 == 0){
     93                         Toast.makeText(JiSuanActivity02.this, "除数不能为0", Toast.LENGTH_SHORT).show();
     94                     }else {
     95                         result = result0 / result1;
     96                     }
     97 
     98                 }else if (flag == 0){
     99                     result = result1;
    100                 }
    101                 String str = (result + "").trim();
    102 
    103                 if (result1 == 0 && flag == 4){
    104                     str = "错误";
    105                 }
    106                 tv.setText(str);
    107                 Toast.makeText(JiSuanActivity02.this, "结果是:" + result, Toast.LENGTH_SHORT).show();
    108             }
    109         });
    110     }
    111 
    112     //初始化控件资源
    113     public void initButton(){
    114         tv = (TextView)this.findViewById(R.id.tv);
    115         str1 = String.valueOf(tv.getText());
    116         str2 = "";
    117         buttonC = getBtnForId(R.id.ButtonC);
    118         buttonJia = getBtnForId(R.id.ButtonJia);
    119         buttonJian = getBtnForId(R.id.ButtonJian);
    120         buttonCheng = getBtnForId(R.id.ButtonCheng);
    121         buttonChu = getBtnForId(R.id.ButtonChu);
    122         buttonDengyu = getBtnForId(R.id.ButtonDeng);
    123 
    124         buttons = new int[]{
    125                 R.id.Button00,R.id.Button01,R.id.Button02,
    126                 R.id.Button03,
    127                 R.id.Button04,R.id.Button05,R.id.Button06,
    128                 R.id.Button07,R.id.Button08,R.id.Button09
    129         };
    130     }
    131     //根据id获取Button
    132     public Button getBtnForId(int rID){
    133         Button btn = (Button)this.findViewById(rID);
    134         return btn;
    135     }
    136     //按钮监听
    137     public void buttonListener(Button button, final int id){
    138         button.setOnClickListener(new View.OnClickListener() {
    139             @Override
    140             public void onClick(View view) {
    141                 String str = tv.getText().toString().trim();
    142                 result0 = Integer.parseInt(str);
    143                 //tv.setText("");
    144                 flag = id;
    145             }
    146         });
    147     }
    148 }
  • 相关阅读:
    [转]资本经营董事长班告诉你:不只企业有商业模式,个人商业价值更重要
    [转]把人当成一个公司来经营,心就不会那么累
    [转]东方财智商学院告诉你如何写成功的商业计划书,按这23点来就好了
    [转]《穷爸爸富爸爸》作者破产了,他捧红了“财务自由”
    [转]人人店短信插件开发
    [转]简单的动态修改RDLC报表页边距和列宽的方法
    [转]程序员应打破牢笼,展望更高层次的世界
    jQuery validate and groups
    [转]C#利用反射实现两个类的对象之间相同属性的值的复制
    [转]ui-grid User can't select the row by clicking the select checkbox available in the respective row when enableFullRowSelection : true"
  • 原文地址:https://www.cnblogs.com/whongs/p/7452001.html
Copyright © 2020-2023  润新知