zoukankan      html  css  js  c++  java
  • 计算器 ——给按钮添加监听器

      1 <?xml version="1.0" encoding="utf-8"?>
      2 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
      3     xmlns:tools="http://schemas.android.com/tools"
      4     android:layout_width="match_parent"
      5     android:layout_height="match_parent"
      6     tools:context="com.hanqi.testapp2.MainActivity"
      7     android:rowCount="7"
      8     android:columnCount="4">
      9 
     10     <TextView
     11         android:layout_width="match_parent"
     12         android:layout_height="120dp"
     13         android:layout_columnSpan="4"
     14         android:layout_rowSpan="2"
     15         android:id="@+id/tv_1"
     16         android:text="0"
     17         android:textSize="40sp"
     18         android:paddingRight="10dp"
     19         android:gravity="right|center_vertical"
     20         android:textColor="#fff100"
     21         android:background="#000"/>
     22 
     23     <Button
     24         android:layout_width="wrap_content"
     25         android:layout_height="wrap_content"
     26         android:gravity="center"
     27         android:text="C"
     28         android:id="@+id/btc"
     29         android:layout_rowWeight="1"
     30         android:layout_columnWeight="1"
     31         android:textSize="30sp"/>
     32     <Button
     33         android:layout_width="wrap_content"
     34         android:layout_height="wrap_content"
     35         android:gravity="center"
     36         android:text="+/-"
     37         android:id="@+id/bt_hun"
     38         android:layout_rowWeight="1"
     39         android:layout_columnWeight="1"
     40         android:textSize="30sp"/>
     41     <Button
     42         android:layout_width="wrap_content"
     43         android:layout_height="wrap_content"
     44         android:gravity="center"
     45         android:text="%"
     46         android:id="@+id/bt_bai"
     47         android:layout_rowWeight="1"
     48         android:layout_columnWeight="1"
     49         android:textSize="30sp"/>
     50     <Button
     51         android:layout_width="wrap_content"
     52         android:layout_height="wrap_content"
     53         android:gravity="center"
     54         android:text="÷"
     55         android:id="@+id/bt_chu"
     56         android:layout_rowWeight="1"
     57         android:textSize="30sp"/>
     58 
     59     <Button
     60         android:layout_width="wrap_content"
     61         android:layout_height="wrap_content"
     62         android:gravity="center"
     63         android:text="7"
     64         android:onClick="bt7_OnClick"
     65         android:layout_rowWeight="1"
     66         android:layout_columnWeight="1"
     67         android:textSize="30sp"/>
     68 
     69     <Button
     70         android:layout_width="wrap_content"
     71         android:layout_height="wrap_content"
     72         android:gravity="center"
     73         android:text="8"
     74         android:onClick="bt8_OnClick"
     75         android:layout_rowWeight="1"
     76         android:layout_columnWeight="1"
     77         android:textSize="30sp"/>
     78 
     79     <Button
     80         android:layout_width="wrap_content"
     81         android:layout_height="wrap_content"
     82         android:gravity="center"
     83         android:text="9"
     84         android:id="@+id/bt9"
     85         android:layout_rowWeight="1"
     86         android:layout_columnWeight="1"
     87         android:textSize="30sp"/>
     88 
     89     <Button
     90         android:layout_width="wrap_content"
     91         android:layout_height="wrap_content"
     92         android:gravity="center"
     93         android:text="X"
     94         android:id="@+id/bt_cheng"
     95         android:layout_rowWeight="1"
     96         android:textSize="30sp"/>
     97 
     98     <Button
     99         android:layout_width="wrap_content"
    100         android:layout_height="wrap_content"
    101         android:gravity="center"
    102         android:text="4"
    103         android:id="@+id/bt4"
    104         android:layout_rowWeight="1"
    105         android:layout_columnWeight="1"
    106         android:textSize="30sp"/>
    107 
    108     <Button
    109         android:layout_width="wrap_content"
    110         android:layout_height="wrap_content"
    111         android:gravity="center"
    112         android:text="5"
    113         android:id="@+id/bt5"
    114         android:layout_rowWeight="1"
    115         android:layout_columnWeight="1"
    116         android:textSize="30sp"/>
    117 
    118     <Button
    119         android:layout_width="wrap_content"
    120         android:layout_height="wrap_content"
    121         android:gravity="center"
    122         android:text="6"
    123         android:id="@+id/bt6"
    124         android:layout_rowWeight="1"
    125         android:layout_columnWeight="1"
    126         android:textSize="30sp"/>
    127 
    128     <Button
    129         android:layout_width="wrap_content"
    130         android:layout_height="wrap_content"
    131         android:gravity="center"
    132         android:text="-"
    133         android:id="@+id/bt_jian"
    134         android:layout_rowWeight="1"
    135         android:textSize="30sp"/>
    136 
    137     <Button
    138         android:layout_width="wrap_content"
    139         android:layout_height="wrap_content"
    140         android:gravity="center"
    141         android:text="1"
    142         android:id="@+id/bt1"
    143         android:layout_rowWeight="1"
    144         android:layout_columnWeight="1"
    145         android:textSize="30sp"/>
    146     <Button
    147         android:layout_width="wrap_content"
    148         android:layout_height="wrap_content"
    149         android:gravity="center"
    150         android:text="2"
    151         android:id="@+id/bt2"
    152         android:layout_rowWeight="1"
    153         android:layout_columnWeight="1"
    154         android:textSize="30sp"/>
    155     <Button
    156         android:layout_width="wrap_content"
    157         android:layout_height="wrap_content"
    158         android:gravity="center"
    159         android:text="3"
    160         android:id="@+id/bt3"
    161         android:layout_rowWeight="1"
    162         android:layout_columnWeight="1"
    163         android:textSize="30sp"/>
    164     <Button
    165         android:layout_width="wrap_content"
    166         android:layout_height="wrap_content"
    167         android:gravity="center"
    168         android:text="+"
    169         android:id="@+id/bt_add"
    170         android:layout_rowWeight="1"
    171         android:textSize="30sp"/>
    172     <Button
    173         android:layout_width="wrap_content"
    174         android:layout_height="wrap_content"
    175         android:layout_columnSpan="2"
    176         android:gravity="center"
    177         android:layout_gravity="fill"
    178         android:text="0"
    179         android:id="@+id/bt0"
    180         android:layout_rowWeight="1"
    181         android:layout_columnWeight="1"
    182         android:textSize="30sp"/>
    183     <Button
    184         android:layout_width="wrap_content"
    185         android:layout_height="wrap_content"
    186         android:gravity="center"
    187         android:text="."
    188         android:id="@+id/bt_dian"
    189         android:layout_rowWeight="1"
    190         android:layout_columnWeight="1"
    191         android:textSize="30sp"/>
    192     <Button
    193         android:layout_width="wrap_content"
    194         android:layout_height="wrap_content"
    195         android:gravity="center"
    196         android:text="="
    197         android:id="@+id/bt_deng"
    198         android:layout_rowWeight="1"
    199         android:textSize="30sp"/>
    200 </GridLayout>
    activity_main
      1 package com.hanqi.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 bt9;
     14     Button bt4;
     15     Button bt5;
     16     Button bt6;
     17     Button bt_add;
     18     Button bt_jian;
     19     Button btc;
     20     Button bt_hun;
     21     Button bt_bai;
     22     Button bt_chu;
     23     Button bt_cheng;
     24     Button bt1;
     25     Button bt2;
     26     Button bt3;
     27     Button bt0;
     28     Button bt_dian;
     29     Button bt_deng;
     30 
     31 
     32 
     33 
     34     @Override
     35     protected void onCreate(Bundle savedInstanceState) {
     36         super.onCreate(savedInstanceState);
     37         setContentView(R.layout.activity_main);
     38 
     39         tv_1=(TextView)findViewById(R.id.tv_1);
     40 
     41         //事件源
     42         bt9=(Button)findViewById(R.id.bt9);
     43         bt4=(Button)findViewById(R.id.bt4);
     44         bt5=(Button)findViewById(R.id.bt5);
     45         bt6=(Button)findViewById(R.id.bt6);
     46         bt_add=(Button)findViewById(R.id.bt_add);
     47         bt_jian=(Button)findViewById(R.id.bt_jian);
     48         btc=(Button)findViewById(R.id.btc);
     49         bt_hun=(Button)findViewById(R.id.bt_hun);
     50         bt_bai=(Button)findViewById(R.id.bt_bai);
     51         bt_chu=(Button)findViewById(R.id.bt_chu);
     52         bt_cheng=(Button)findViewById(R.id.bt_cheng);
     53         bt1=(Button)findViewById(R.id.bt1);
     54         bt2=(Button)findViewById(R.id.bt2);
     55         bt3=(Button)findViewById(R.id.bt3);
     56         bt0=(Button)findViewById(R.id.bt0);
     57         bt_dian=(Button)findViewById(R.id.bt_dian);
     58         bt_deng=(Button)findViewById(R.id.bt_deng);
     59 
     60 
     61 
     62         //给bt9按钮添加事件监听器 实现方式:1.匿名内部类 不可以复用
     63         bt9.setOnClickListener(new View.OnClickListener() {
     64             @Override
     65             public void onClick(View v) {
     66 
     67                 //处理事件的业务逻辑
     68                 tv_1.setText("9");
     69 
     70             }
     71         });
     72 
     73         //2.普通内部类  可以复用
     74         bt_OnClickListener bt_1=new bt_OnClickListener();
     75 
     76         btc.setOnClickListener(bt_1);
     77         bt4.setOnClickListener(bt_1);
     78         bt5.setOnClickListener(bt_1);
     79         bt6.setOnClickListener(bt_1);
     80         bt_jian.setOnClickListener(bt_1);
     81         btc.setOnClickListener(bt_1);
     82         bt_hun.setOnClickListener(bt_1);
     83         bt_bai.setOnClickListener(bt_1);
     84         bt_chu.setOnClickListener(bt_1);
     85         bt_cheng.setOnClickListener(bt_1);
     86         bt1.setOnClickListener(bt_1);
     87         bt2.setOnClickListener(bt_1);
     88         bt3.setOnClickListener(bt_1);
     89         bt0.setOnClickListener(bt_1);
     90         bt_dian.setOnClickListener(bt_1);
     91         bt_deng.setOnClickListener(bt_1);
     92         bt_add.setOnClickListener(bt_1);
     93     }
     94 
     95     //内部类实现OnClickListener接口
     96     class  bt_OnClickListener implements  View.OnClickListener
     97     {
     98         @Override
     99         //v 事件源
    100         public void onClick(View v) {
    101 
    102             //转成按钮
    103             Button bt=(Button)v;
    104 
    105             //取得按钮上的文字
    106             String str=bt.getText().toString();
    107 
    108             //处理事件的业务逻辑 设置显示文字
    109             tv_1.setText(str);
    110         }
    111 
    112     }
    113 
    114     //3.关联方式
    115     public  void bt7_OnClick(View v)
    116     {
    117         tv_1.setText("7");
    118     }
    119 
    120     public  void bt8_OnClick(View v)
    121     {
    122         tv_1.setText("8");
    123     }
    124 }
    MainActivity

     

  • 相关阅读:
    HDU 5791 Two (DP)
    POJ 1088 滑雪 (DPor记忆化搜索)
    LightOJ 1011
    POJ 1787 Charlie's Change (多重背包 带结果组成)
    HDU 5550 Game Rooms (ccpc2015 K)(dp)
    HDU 5542 The Battle of Chibi (ccpc 南阳 C)(DP 树状数组 离散化)
    HDU 5543 Pick The Sticks (01背包)
    HDU 5546 Ancient Go (ccpc2015南阳G)
    NB-IoT的DRX、eDRX、PSM三个模式 (转载,描述的简单易懂)
    MQTT 嵌入式端通讯协议解析(转)
  • 原文地址:https://www.cnblogs.com/arxk/p/5455657.html
Copyright © 2011-2022 走看看