事件监听器——计算器为例

MainActivity:
 1 package com.hanqi.testapp2;
 2 
 3 import android.os.Bundle;
 4 import android.support.v7.app.AppCompatActivity;
 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     Button bt9;
13     Button bt4;
14     Button bt5;
15     Button bt6;
16     Button bt_add;
17     Button bt_jian;
18     Button bt1;
19     Button bt2;
20     Button bt3;
21     Button bt_cheng;
22     Button bt0;
23     Button bt_dian;
24     Button bt_chu;
25     @Override
26     protected void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.activity_main);
29         tv_1 = (TextView)findViewById(R.id.tv_1);
30         //事件源
31         bt9 = (Button)findViewById(R.id.bt9);
32         bt4 = (Button)findViewById(R.id.bt4);
33         bt5 = (Button)findViewById(R.id.bt5);
34         bt6 = (Button)findViewById(R.id.bt6);
35         bt_add = (Button)findViewById(R.id.bt_add);
36         bt_jian = (Button)findViewById(R.id.bt_jian);
37         bt1 = (Button)findViewById(R.id.bt1);
38         bt2 = (Button)findViewById(R.id.bt2);
39         bt3 = (Button)findViewById(R.id.bt3);
40         bt_cheng = (Button)findViewById(R.id.bt_cheng);
41         bt0 = (Button)findViewById(R.id.bt0);
42         bt_dian = (Button)findViewById(R.id.bt_dian);
43         bt_chu = (Button)findViewById(R.id.bt_chu);
44         //给bt9按钮添加事件监听器 实现方式:1.匿名内部类
45         bt9.setOnClickListener(new View.OnClickListener() {
46             @Override
47             public void onClick(View v) {
48                 //处理事件的业务逻辑
49                 tv_1.setText("9");
50             }
51         });
52         //2.普通内部类
53         bt_OnClickListener bt_l = new bt_OnClickListener();
54         bt_add.setOnClickListener(bt_l);
55         bt4.setOnClickListener(bt_l);
56         bt5.setOnClickListener(bt_l);
57         bt6.setOnClickListener(bt_l);
58         bt_jian.setOnClickListener(bt_l);
59         bt1.setOnClickListener(bt_l);
60         bt2.setOnClickListener(bt_l);
61         bt3.setOnClickListener(bt_l);
62         bt_cheng.setOnClickListener(bt_l);
63         bt0.setOnClickListener(bt_l);
64         bt_dian.setOnClickListener(bt_l);
65         bt_chu.setOnClickListener(bt_l);
66     }
67 
68     //内部类实现OnClickListener接口
69     class bt_OnClickListener implements View.OnClickListener
70     {
71         @Override
72         //v就是事件源
73         public void onClick(View v) {
74             //转成按钮
75             Button bt = (Button)v;
76             //取得按钮上的文字
77             String str = bt.getText().toString();
78             //处理事件的业务逻辑  设置显示文字
79             tv_1.setText(str);
80         }
81     }
82     //3.关联方式
83     public void bt7_onClick(View v)
84     {
85         tv_1.setText("7");
86     }
87     public void bt8_onClick(View v)
88     {
89         tv_1.setText("8");
90     }
91     public void bt9_onClick(View v)
92     {
93         tv_1.setText("9");
94     }
95     public void bt_c(View v)
96     {
97         tv_1.setText(null);   //点击C会清零
98     }
99 }

activity_main:
  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="6"
  6     android:columnCount="4">
  7     <TextView
  8         android:layout_width="match_parent"
  9         android:layout_height="200dp"
 10         android:layout_columnSpan="4"
 11         android:gravity="bottom|right"
 12         android:id="@+id/tv_1"
 13         android:textSize="50sp"
 14         android:paddingRight="10dp"
 15         android:textColor="#0F0"/>
 16     <Button
 17         android:layout_width="0dp"
 18         android:layout_height="0dp"
 19         android:text="c"
 20         android:layout_columnWeight="1"
 21         android:layout_rowWeight="1"
 22         android:textSize="25sp"
 23         android:onClick="bt_c"/>
 24     <Button
 25         android:layout_width="0dp"
 26         android:layout_height="0dp"
 27         android:text="( )"
 28         android:layout_columnWeight="1"
 29         android:layout_rowWeight="1"
 30         android:textSize="25sp"/>
 31     <Button
 32         android:layout_width="0dp"
 33         android:layout_height="0dp"
 34         android:text="÷"
 35         android:layout_columnWeight="1"
 36         android:layout_rowWeight="1"
 37         android:textSize="25sp"
 38         android:id="@+id/bt_chu"/>
 39     <Button
 40         android:layout_width="0dp"
 41         android:layout_height="0dp"
 42         android:text="×"
 43         android:layout_columnWeight="1"
 44         android:layout_rowWeight="1"
 45         android:textSize="25sp"
 46         android:id="@+id/bt_cheng"/>
 47     <Button
 48         android:layout_width="0dp"
 49         android:layout_height="0dp"
 50         android:text="7"
 51         android:layout_columnWeight="1"
 52         android:layout_rowWeight="1"
 53         android:textSize="25sp"
 54         android:onClick="bt7_onClick"/>
 55     <Button
 56         android:layout_width="0dp"
 57         android:layout_height="0dp"
 58         android:text="8"
 59         android:layout_columnWeight="1"
 60         android:layout_rowWeight="1"
 61         android:textSize="25sp"
 62         android:onClick="bt8_onClick"/>
 63     <Button
 64         android:layout_width="0dp"
 65         android:layout_height="0dp"
 66         android:text="9"
 67         android:layout_columnWeight="1"
 68         android:layout_rowWeight="1"
 69         android:textSize="25sp"
 70         android:onClick="bt9_onClick"
 71         android:id="@+id/bt9"/>
 72     <Button
 73         android:layout_width="0dp"
 74         android:layout_height="0dp"
 75         android:text="-"
 76         android:layout_columnWeight="1"
 77         android:layout_rowWeight="1"
 78         android:textSize="25sp"
 79         android:id="@+id/bt_jian"/>
 80     <Button
 81         android:layout_width="0dp"
 82         android:layout_height="0dp"
 83         android:text="4"
 84         android:layout_columnWeight="1"
 85         android:layout_rowWeight="1"
 86         android:textSize="25sp"
 87         android:id="@+id/bt4"/>
 88     <Button
 89         android:layout_width="0dp"
 90         android:layout_height="0dp"
 91         android:text="5"
 92         android:layout_columnWeight="1"
 93         android:layout_rowWeight="1"
 94         android:textSize="25sp"
 95         android:id="@+id/bt5"/>
 96     <Button
 97         android:layout_width="0dp"
 98         android:layout_height="0dp"
 99         android:text="6"
100         android:layout_columnWeight="1"
101         android:layout_rowWeight="1"
102         android:textSize="25sp"
103         android:id="@+id/bt6"/>
104     <Button
105         android:layout_width="0dp"
106         android:layout_height="0dp"
107         android:text="+"
108         android:layout_columnWeight="1"
109         android:layout_rowWeight="1"
110         android:textSize="25sp"
111         android:id="@+id/bt_add"/>
112     <Button
113         android:layout_width="0dp"
114         android:layout_height="0dp"
115         android:text="1"
116         android:layout_columnWeight="1"
117         android:layout_rowWeight="1"
118         android:textSize="25sp"
119         android:id="@+id/bt1"/>
120     <Button
121         android:layout_width="0dp"
122         android:layout_height="0dp"
123         android:text="2"
124         android:layout_columnWeight="1"
125         android:layout_rowWeight="1"
126         android:textSize="25sp"
127         android:id="@+id/bt2"/>
128     <Button
129         android:layout_width="0dp"
130         android:layout_height="0dp"
131         android:text="3"
132         android:layout_columnWeight="1"
133         android:layout_rowWeight="1"
134         android:textSize="25sp"
135         android:id="@+id/bt3"/>
136     <Button
137         android:layout_width="0dp"
138         android:layout_height="0dp"
139         android:text="="
140         android:layout_columnWeight="1"
141         android:layout_rowSpan="2"
142         android:layout_rowWeight="1"
143         android:textSize="25sp"
144         android:layout_gravity="fill"/>
145     <Button
146         android:layout_width="0dp"
147         android:layout_height="0dp"
148         android:text="0"
149         android:layout_columnWeight="1"
150         android:layout_columnSpan="2"
151         android:layout_rowWeight="1"
152         android:textSize="25sp"
153         android:id="@+id/bt0"/>
154     <Button
155         android:layout_width="0dp"
156         android:layout_height="0dp"
157         android:text="."
158         android:layout_columnWeight="1"
159         android:layout_rowWeight="1"
160         android:textSize="25sp"
161         android:id="@+id/bt_dian"/>
162 </GridLayout>

效果如图:

原文地址:https://www.cnblogs.com/hanazawalove/p/5455918.html