UI事件 计算器界面

1.MainActivity.java

  1 package com.example.administrator.testapp2;
  2 
  3 import android.support.v7.app.AppCompatActivity;
  4 import android.os.Bundle;
  5 import android.view.View;
  6 import android.widget.Button;
  7 import android.widget.TextView;
  8 
  9 public class MainActivity extends AppCompatActivity {
 10 
 11     TextView tv_1;
 12 
 13     Button bt1;
 14     Button bt2;
 15     Button bt3;
 16     Button bt4;
 17     Button bt5;
 18     Button bt6;
 19     Button bt7;
 20     Button bt8;
 21     Button bt9;
 22     Button bt0;
 23     Button bt_add;
 24     Button bt_jian;
 25     Button bt_cheng;
 26     Button bt_chu;
 27     Button bt_kuohao;
 28     Button bt_dian;
 29 
 30     @Override
 31     protected void onCreate(Bundle savedInstanceState) {
 32         super.onCreate(savedInstanceState);
 33         setContentView(R.layout.activity_main);
 34 
 35         tv_1 = (TextView)findViewById(R.id.tv_1);
 36 
 37         bt0 = (Button)findViewById(R.id.bt0);
 38         bt0.setOnClickListener(new View.OnClickListener() {
 39             @Override
 40             public void onClick(View v) {
 41                 tv_1.setText("0");
 42             }
 43         });
 44         bt1 = (Button)findViewById(R.id.bt1);
 45         bt1.setOnClickListener(new View.OnClickListener() {
 46             @Override
 47             public void onClick(View v) {
 48                 tv_1.setText("1");
 49             }
 50         });
 51         bt2 = (Button)findViewById(R.id.bt2);
 52         bt2.setOnClickListener(new View.OnClickListener() {
 53             @Override
 54             public void onClick(View v) {
 55                 tv_1.setText("2");
 56             }
 57         });
 58         bt3 = (Button)findViewById(R.id.bt3);
 59         bt3.setOnClickListener(new View.OnClickListener() {
 60             @Override
 61             public void onClick(View v) {
 62                 tv_1.setText("3");
 63             }
 64         });
 65         bt4 = (Button)findViewById(R.id.bt4);
 66         bt4.setOnClickListener(new View.OnClickListener() {
 67             @Override
 68             public void onClick(View v) {
 69                 tv_1.setText("4");
 70             }
 71         });
 72         bt5 = (Button)findViewById(R.id.bt5);
 73         bt5.setOnClickListener(new View.OnClickListener() {
 74             @Override
 75             public void onClick(View v) {
 76                 tv_1.setText("5");
 77             }
 78         });
 79         bt6 = (Button)findViewById(R.id.bt6);
 80         bt6.setOnClickListener(new View.OnClickListener() {
 81             @Override
 82             public void onClick(View v) {
 83                 tv_1.setText("6");
 84             }
 85         });
 86         bt7 = (Button)findViewById(R.id.bt7);
 87         bt7.setOnClickListener(new View.OnClickListener() {
 88             @Override
 89             public void onClick(View v) {
 90                 tv_1.setText("7");
 91             }
 92         });
 93         bt8 = (Button)findViewById(R.id.bt8);
 94         bt8.setOnClickListener(new View.OnClickListener() {
 95             @Override
 96             public void onClick(View v) {
 97                 tv_1.setText("8");
 98             }
 99         });
100         bt9 = (Button)findViewById(R.id.bt9);
101         bt9.setOnClickListener(new View.OnClickListener() {
102             @Override
103             public void onClick(View v) {
104                 tv_1.setText("9");
105             }
106         });
107         bt_add = (Button)findViewById(R.id.bt_add);
108         bt_add.setOnClickListener(new View.OnClickListener() {
109             @Override
110             public void onClick(View v) {
111                 tv_1.setText("+");
112             }
113         });
114         bt_jian = (Button)findViewById(R.id.bt_jian);
115         bt_jian.setOnClickListener(new View.OnClickListener() {
116             @Override
117             public void onClick(View v) {
118                 tv_1.setText("-");
119             }
120         });
121         bt_cheng = (Button)findViewById(R.id.bt_cheng);
122         bt_cheng.setOnClickListener(new View.OnClickListener() {
123             @Override
124             public void onClick(View v) {
125                 tv_1.setText("X");
126             }
127         });
128         bt_chu = (Button)findViewById(R.id.bt_chu);
129         bt_chu.setOnClickListener(new View.OnClickListener() {
130             @Override
131             public void onClick(View v) {
132                 tv_1.setText("÷");
133             }
134         });
135         bt_dian = (Button)findViewById(R.id.bt_dian);
136         bt_dian.setOnClickListener(new View.OnClickListener() {
137             @Override
138             public void onClick(View v) {
139                 tv_1.setText(".");
140             }
141         });
142         bt_kuohao = (Button)findViewById(R.id.bt_kuohao);
143         bt_kuohao.setOnClickListener(new View.OnClickListener() {
144             @Override
145             public void onClick(View v) {
146                 tv_1.setText("()");
147             }
148         });
149     }
150 }

2.jsq.xml

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent"
  5     android:rowCount="5"
  6     android:columnCount="4">
  7 <TextView
  8     android:layout_width="match_parent"
  9     android:layout_height="220dp"
 10     android:layout_columnSpan="4"
 11     android:textSize="60dp"
 12     android:gravity="right|bottom"
 13     android:text="6"
 14     android:textColor="#0F0"
 15     android:paddingRight="10dp"
 16     android:id="@+id/tv_1"/>
 17 
 18     <Button
 19         android:layout_width="wrap_content"
 20         android:layout_height="wrap_content"
 21         android:text="CE"
 22         android:layout_rowWeight="1"
 23         android:id="@+id/bt_CE"
 24         android:textSize="25sp"
 25         android:layout_columnWeight="1"/>
 26     <Button
 27         android:layout_width="wrap_content"
 28         android:layout_height="wrap_content"
 29         android:text="←"
 30         android:id="@+id/bt_qc"
 31         android:textSize="25sp"
 32         android:layout_rowWeight="1"
 33         android:layout_columnWeight="1"/>
 34     <Button
 35         android:layout_width="wrap_content"
 36         android:layout_height="wrap_content"
 37         android:text="÷"
 38         android:id="@+id/bt_chu"
 39         android:textSize="25sp"
 40         android:layout_rowWeight="1"
 41         android:layout_columnWeight="1"/>
 42     <Button
 43         android:layout_width="wrap_content"
 44         android:layout_height="wrap_content"
 45         android:text="X"
 46         android:id="@+id/bt_cheng"
 47         android:textSize="25sp"
 48         android:layout_rowWeight="1"
 49         android:layout_columnWeight="1"/>
 50     <Button
 51         android:layout_width="wrap_content"
 52         android:layout_height="wrap_content"
 53         android:text="7"
 54         android:id="@+id/bt7"
 55         android:textSize="25sp"
 56         android:layout_rowWeight="1"
 57         android:layout_columnWeight="1"/>
 58     <Button
 59         android:layout_width="wrap_content"
 60         android:layout_height="wrap_content"
 61         android:text="8"
 62         android:id="@+id/bt8"
 63         android:textSize="25sp"
 64         android:layout_rowWeight="1"
 65         android:layout_columnWeight="1"/>
 66     <Button
 67         android:layout_width="wrap_content"
 68         android:layout_height="wrap_content"
 69         android:text="9"
 70         android:id="@+id/bt9"
 71         android:textSize="25sp"
 72         android:layout_rowWeight="1"
 73         android:layout_columnWeight="1"/>
 74     <Button
 75         android:layout_width="wrap_content"
 76         android:layout_height="wrap_content"
 77         android:text="-"
 78         android:id="@+id/bt_jian"
 79         android:textSize="25sp"
 80         android:layout_rowWeight="1"
 81         android:layout_columnWeight="1"/>
 82     <Button
 83         android:layout_width="wrap_content"
 84         android:layout_height="wrap_content"
 85         android:text="4"
 86         android:id="@+id/bt4"
 87         android:textSize="25sp"
 88         android:layout_rowWeight="1"
 89         android:layout_columnWeight="1"/>
 90     <Button
 91         android:layout_width="wrap_content"
 92         android:layout_height="wrap_content"
 93         android:text="5"
 94         android:id="@+id/bt5"
 95         android:textSize="25sp"
 96         android:layout_rowWeight="1"
 97         android:layout_columnWeight="1"/>
 98     <Button
 99         android:layout_width="wrap_content"
100         android:layout_height="wrap_content"
101         android:text="6"
102         android:id="@+id/bt6"
103         android:textSize="25sp"
104         android:layout_rowWeight="1"
105         android:layout_columnWeight="1"/>
106     <Button
107         android:layout_width="wrap_content"
108         android:layout_height="wrap_content"
109         android:text="+"
110         android:id="@+id/bt_add"
111         android:textSize="25sp"
112         android:layout_rowWeight="1"
113         android:layout_columnWeight="1"/>
114     <Button
115         android:layout_width="wrap_content"
116         android:layout_height="wrap_content"
117         android:text="1"
118         android:id="@+id/bt1"
119         android:textSize="25sp"
120         android:layout_rowWeight="1"
121         android:layout_columnWeight="1"/>
122     <Button
123         android:layout_width="wrap_content"
124         android:layout_height="wrap_content"
125         android:text="2"
126         android:id="@+id/bt2"
127         android:textSize="25sp"
128         android:layout_rowWeight="1"
129         android:layout_columnWeight="1"/>
130     <Button
131         android:layout_width="wrap_content"
132         android:layout_height="wrap_content"
133         android:text="3"
134         android:id="@+id/bt3"
135         android:textSize="25sp"
136         android:layout_rowWeight="1"
137         android:layout_columnWeight="1"/>
138     <Button
139         android:layout_width="wrap_content"
140         android:layout_height="wrap_content"
141         android:text="="
142         android:id="@+id/btdengyu"
143         android:textSize="25sp"
144         android:layout_columnWeight="1"
145         android:layout_rowSpan="2"
146         android:layout_rowWeight="1"
147         android:layout_gravity="fill"/>
148     <Button
149         android:layout_width="wrap_content"
150         android:layout_height="wrap_content"
151         android:text="0"
152         android:id="@+id/bt0"
153         android:textSize="25sp"
154         android:layout_columnWeight="1"
155         android:layout_rowWeight="1"/>
156 
157     <Button
158         android:layout_width="wrap_content"
159         android:layout_height="wrap_content"
160         android:text="."
161         android:id="@+id/bt_dian"
162         android:textSize="25sp"
163         android:layout_rowWeight="1"
164         android:layout_columnWeight="1"/>
165     <Button
166         android:layout_width="wrap_content"
167         android:layout_height="wrap_content"
168         android:text="()"
169         android:id="@+id/bt_kuohao"
170         android:textSize="25sp"
171         android:layout_rowWeight="1"
172         android:layout_columnWeight="1"/>
173 </GridLayout>
原文地址:https://www.cnblogs.com/TENOKAWA/p/5456593.html