• ANDROID笔记:基于GridLayout布局的简易计算器


      1 package com.example.classtest;
      2 
      3 import android.app.Activity;
      4 import android.os.Bundle;
      5 import android.view.View;
      6 import android.view.View.OnClickListener;
      7 import android.widget.TextView;
      8 
      9 public class MainActivity extends Activity implements OnClickListener {
     10     private int btnIds[] = { R.id.button0, R.id.button1, R.id.button2,
     11             R.id.button3, R.id.button4, R.id.button5, R.id.button6,
     12             R.id.button7, R.id.button8, R.id.button9, R.id.buttonadd,
     13             R.id.button1x, R.id.buttondiv, R.id.buttonequal, R.id.buttonmult,
     14             R.id.buttonpoint, R.id.buttonrem, R.id.buttonsub, R.id.buttonclear };
     15     TextView textView = null;
     16     String text = null;
     17 
     18     @Override
     19     protected void onCreate(Bundle savedInstanceState) {
     20         super.onCreate(savedInstanceState);
     21         setContentView(R.layout.activity_main);
     22         textView = (TextView) findViewById(R.id.textView1);
     23 
     24         for (int id : btnIds) {
     25             findViewById(id).setOnClickListener(this);
     26 
     27         }
     28     }
     29 
     30     @Override
     31     public void onClick(View v) {
     32         // TODO Auto-generated method stub
     33         text = textView.getText().toString();
     34         System.out.println(v.getId());
     35         switch (v.getId()) {
     36         case R.id.buttonclear:
     37             text = "";
     38             break;
     39         case R.id.button0:
     40             text += 0;
     41             break;
     42         case R.id.button1:
     43             text += 1;
     44             break;
     45         case R.id.button2:
     46             text += 2;
     47             break;
     48         case R.id.button3:
     49             text += 3;
     50             break;
     51         case R.id.button4:
     52             text += 4;
     53             break;
     54         case R.id.button5:
     55             text += 5;
     56             break;
     57         case R.id.button6:
     58             text += 6;
     59             break;
     60         case R.id.button7:
     61             text += 7;
     62             break;
     63         case R.id.button8:
     64             text += 8;
     65             break;
     66         case R.id.button9:
     67             text += 9;
     68             break;
     69         case R.id.buttonadd:
     70             handle();
     71             text += "+";
     72             break;
     73         case R.id.buttondiv:
     74             handle();
     75             text += "/";
     76             break;
     77         case R.id.buttonmult:
     78             handle();
     79             text += "*";
     80             break;
     81         case R.id.buttonpoint:
     82             handle();
     83             text += ".";
     84             break;
     85         case R.id.buttonrem:
     86             handle();
     87             text += "%";
     88             break;
     89         case R.id.buttonsub:
     90             handle();
     91             text += "-";
     92             break;
     93         case R.id.button1x:
     94             handle();
     95             text = (Float) (1 / Float.parseFloat(text)) + "";
     96             break;
     97         case R.id.buttonequal:
     98             handle();
     99             break;
    100         }
    101         textView.setText(text);
    102     }
    103 
    104     private void handle() {
    105         int point = -1;
    106         try {
    107             if ((point = text.indexOf("+")) != -1) {
    108                 text = (Float.parseFloat(text.substring(0, point)) + Float
    109                         .parseFloat(text.substring(point + 1))) + "";
    110             } else if ((point = text.indexOf("-")) != -1) {
    111                 text = (Float.parseFloat(text.substring(0, point)) - Float
    112                         .parseFloat(text.substring(point + 1))) + "";
    113             } else if ((point = text.indexOf("*")) != -1) {
    114                 text = (Float.parseFloat(text.substring(0, point)) * Float
    115                         .parseFloat(text.substring(point + 1))) + "";
    116             } else if ((point = text.indexOf("/")) != -1) {
    117                 text = (Float.parseFloat(text.substring(0, point)) / Float
    118                         .parseFloat(text.substring(point + 1))) + "";
    119             } else if ((point = text.indexOf("%")) != -1) {
    120                 text = (Float.parseFloat(text.substring(0, point)) % Float
    121                         .parseFloat(text.substring(point + 1))) + "";
    122             }
    123         } catch (Exception e) {
    124             System.err.println(e.getMessage());
    125         }
    126     }
    127 }
      1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      2     xmlns:tools="http://schemas.android.com/tools"
      3     android:layout_width="fill_parent"
      4     android:layout_height="fill_parent"
      5     tools:context=".MainActivity" >
      6 
      7     <GridLayout
      8         android:layout_width="match_parent"
      9         android:layout_height="wrap_content"
     10         android:layout_alignParentLeft="true"
     11         android:layout_alignParentTop="true"
     12         android:columnCount="5"
     13         android:rowCount="6" >
     14 
     15         <TextView
     16             android:id="@+id/textView1"
     17             android:layout_column="0"
     18             android:layout_row="0"
     19            android:textSize="50sp"
     20             android:layout_columnSpan="5"
     21             android:layout_gravity="fill_horizontal"
     22             android:textAppearance="?android:attr/textAppearanceLarge" />
     23 
     24         <Button
     25             android:id="@+id/button7"
     26             android:layout_column="0"
     27             android:layout_row="1"
     28             android:text="7" />
     29         
     30         <Button
     31             android:id="@+id/button8"
     32             android:layout_column="1"
     33             android:layout_row="1"
     34             android:text="8" />
     35         
     36         
     37         <Button
     38             android:id="@+id/button9"
     39             android:layout_column="2"
     40             android:layout_row="1"
     41             android:text="9" />
     42         
     43         <Button
     44             android:id="@+id/buttondiv"
     45             android:layout_column="3"
     46             android:layout_row="1"
     47             android:text="/" />
     48          <Button
     49             android:id="@+id/buttonrem"
     50             android:layout_column="4"
     51             android:layout_row="1"
     52             android:text="%" />
     53          
     54          <!--2  -->
     55          
     56         <Button
     57             android:id="@+id/button4"
     58             android:layout_column="0"
     59             android:layout_row="2"
     60             android:text="4" />
     61         
     62         <Button
     63             android:id="@+id/button5"
     64             android:layout_column="1"
     65             android:layout_row="2"
     66             android:text="5" />
     67         
     68         
     69         <Button
     70             android:id="@+id/button6"
     71             android:layout_column="2"
     72             android:layout_row="2"
     73             android:text="6" />
     74         
     75         <Button
     76             android:id="@+id/buttonmult"
     77             android:layout_column="3"
     78             android:layout_row="2"
     79             android:text="*" />
     80          <Button
     81             android:id="@+id/button1x"
     82             android:layout_column="4"
     83             android:layout_row="2"
     84             android:text="1/x" />
     85          <!--3  -->
     86           <Button
     87             android:id="@+id/button1"
     88             android:layout_column="0"
     89             android:layout_row="3"
     90             android:text="1" />
     91         
     92         <Button
     93             android:id="@+id/button2"
     94             android:layout_column="1"
     95             android:layout_row="3"
     96             android:text="2" />
     97         
     98         
     99         <Button
    100             android:id="@+id/button3"
    101             android:layout_column="2"
    102             android:layout_row="3"
    103             android:text="3" />
    104         
    105         <Button
    106             android:id="@+id/buttonsub"
    107             android:layout_column="3"
    108             android:layout_row="3"
    109             android:text="-" />
    110          <Button
    111             android:id="@+id/buttonequal"
    112             android:layout_column="4"
    113             android:layout_row="3"
    114             android:layout_rowSpan="2"
    115             android:layout_gravity="fill_vertical"
    116             android:text="=" />
    117          <!--4  -->
    118             <Button
    119             android:id="@+id/button0"
    120             android:layout_columnSpan="2"
    121             android:layout_gravity="fill_horizontal"
    122             android:layout_column="0"
    123             android:layout_row="4"
    124             android:text="0" />
    125         
    126         <Button
    127             android:id="@+id/buttonpoint"
    128             android:layout_column="2"
    129             android:layout_row="4"
    130             android:text="." />
    131         
    132         
    133         <Button
    134             android:id="@+id/buttonadd"
    135             android:layout_column="3"
    136             android:layout_row="4"
    137             android:text="+" />
    138           <Button
    139             android:id="@+id/buttonclear"
    140             android:layout_column="0"
    141             android:layout_row="5"
    142             android:text="清零" />
    143       
    144     </GridLayout>
    145 
    146 </RelativeLayout>

    GridLayout特殊属性:

     android:columnCount="5" 声明列数
     android:rowCount="6" 声明行数
    android:layout_columnSpan:占列(常和 android:layout_gravity="fill_horizontal"一起使用)
    android:layout_rowSpan:占行(常和 android:layout_gravity="fill_vertical"一起使用
  • 相关阅读:
    ajax提交Form
    MySQL新建用户,授权,删除用户,修改密码总结
    php 数组操作类(整合 给意见)
    PHP基于数组的分页函数(核心函数array_slice())
    php生成table表格
    百度地图定位
    python-redis-订阅和发布
    宿主机-免密登录Docker容器
    docker-文件系统出错处理
    python-redis集合模式
  • 原文地址:https://www.cnblogs.com/afluy/p/3369535.html
Copyright © 2020-2023  润新知