zoukankan      html  css  js  c++  java
  • 对于多个button要在同一个监听器中实现自己的单击事件的方法小诀窍。

      在网上的一些教程中往往是把一个button加入多个监听器,却非常少有人会把多个button在同一个监听器中去实现他们的单击事件,并且这杨的事实上是非常有用的,比方说在制作一个简单的计算器是就须要0-9。这十个数字button假设要单独的去写这十个button的单击事件,那能够想象这样写出来的代码绝对是不够健壮的,这种程序也是会影响其执行速度的。这种程序假设是在java中是非常easy实现的,可是在android中要涉及的知识还是非常多的,接下来先看看基本的代码:

      //////////////使用窗体作为监听器。

      public class index extends Activity implements OnClickListener{

      Button bs0,bs1,bs2,bs3,bs4,bs5,bs6,bs7,bs8,bs9,call,c;

      EditText edit;

      Button del;

      private static final int COUNT=0;

      public int bi[]={R.id.i0,R.id.i1,R.id.i2,R.id.i3,R.id.i4,R.id.i5,R.id.i6,R.id.i7,R.id.i8,R.id.i9};

      bs0=(Button) findViewById(bi[0]);

      bs1=(Button) findViewById(bi[1]);

      bs2=(Button) findViewById(bi[2]);

      bs3=(Button) findViewById(bi[3]);

      bs4=(Button) findViewById(bi[4]);

      bs5=(Button) findViewById(bi[5]);

      bs6=(Button) findViewById(bi[6]);

      bs7=(Button) findViewById(bi[7]);

      bs8=(Button) findViewById(bi[8]);

      bs9=(Button) findViewById(bi[9]);

      edit=(EditText) findViewById(R.id.edit);

      call=(Button) findViewById(R.id.call);

      c=(Button) findViewById(R.id.c);

      del=(Button) findViewById(R.id.del);

      bs0.setOnClickListener(this);

      bs1.setOnClickListener(this);

      bs2.setOnClickListener(this);

      bs3.setOnClickListener(this);

      bs4.setOnClickListener(this);

      bs5.setOnClickListener(this);

      bs6.setOnClickListener(this);

      bs7.setOnClickListener(this);

      bs8.setOnClickListener(this);

      bs9.setOnClickListener(this);

      call.setOnClickListener(this);

      c.setOnClickListener(this);

      del.setOnClickListener(this);

      }

      ///////////////重写onClick()方法

      public void onClick(View v) {

      //////////////使用HashMap把button当成键把数字当成值

      Integer i0=0,i1=1,i2=2,i3=3,i4=4,i5=5,i6=6,i7=7,i8=8,i9=9,d=10,ca=11,c1=12;

      HashMap

      bf.put(bs0, i0);bf.put(bs1, i1);bf.put(bs2, i2);bf.put(bs3, i3);bf.put(bs4, i4);bf.put(bs5, i5);bf.put(bs6, i6);

      bf.put(bs7, i7);bf.put(bs8, i8);bf.put(bs9, i9);bf.put(del, d);bf.put(call, ca);bf.put(c, c1);

      Button bbs=(Button)v;

      int ii=bf.get(v);

      if (ii<10){

      String s1=String.valueOf(ii);

      edit.append(s1);

      }

      //////////////////在依据获取的数字的值不同来为不同的button或其它的控件来加入它们的单击事件。

      else if(ii==10){

      String s3=edit.getText().toString();

      System.out.print(s3);

      s3=(s3.length()>1)?

    s3.substring(0,s3.length()-1):"";

      edit.setText(s3);

      }

      else if (ii==11){

      }

      else if(ii==12){

      index.this.finish();

      }

      }

  • 相关阅读:
    玩转 CSS3 3D 技术
    什么是网站劫持?
    html5 播放 rtsp
    display:none和visibility:hidden两者的区别
    css中div透明度有几种方法设置?
    前端有架构吗?
    HTML a标签打开新标签页避免出现安全漏洞,请使用“noopener”
    写给刚入门的前端工程师的前后端交互指南
    为什么会出现CSS前缀?
    cookies和session得一些理解
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5405332.html
Copyright © 2011-2022 走看看