android 事件响应的四种实现方式

源代码:
public  class  MainActivity  extends  Activity  implements  OnClickListener{

      EditText  inputContent ;
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
        setContentView(R.layout.  activity_main );
         inputContent  = (EditText)findViewById(R.id. inputEditText );
        Button type1 = (Button)findViewById(R.id.  type1 );
        Button type2 = (Button)findViewById(R.id.  type2 );
        Button type3 = (Button)findViewById(R.id.  type3 );
        Button type4 = (Button)findViewById(R.id. type4 );
       
         //第一种无名内部类的方式
        type1.setOnClickListener(  new  OnClickListener() {
                  
                    @Override
                    public  void  onClick(View arg0) {
                          //  TODO  Auto-generated method stub
                        String content =  inputContent .getText().toString();
                        showContent(  "无名内部类的方式"  , content);
                  }
            });
       
         //第二种实现OnClickListener接口方式
        type2.setOnClickListener(  new  MyOnClickListener());
       
         //第三种activity自己实现OnClickListener接口
        type3.setOnClickListener(  this );
    }

     private  class  MyOnClickListener  implements  OnClickListener{

              @Override
              public  void  onClick(View arg0) {
                    //  TODO  Auto-generated method stub
                  String content =  inputContent .getText().toString();
                  showContent(  "实现OnClickListener接口方式"  , content);
            }
      
    }

        @Override
        public  void  onClick(View arg0) {
              //  TODO  Auto-generated method stub
              switch  (arg0.getId()) {
              case  R.id. type3  :
                  String content =  inputContent .getText().toString();
                  showContent(  "activity实现OnClickListener接口方式"  , content);
                    break ;

              default :
                    break ;
            }
      }
   
        //第四种布局文件绑定方法的方式
        public  void  type4(View arg0){
            String content =  inputContent .getText().toString();
            showContent(  "布局文件绑定方式的方式"  , content);
      }
        /**
       * 显示toast
       *  @param  type 那种方式
       *  @param  inputContent 输入内容
       */
        public  void  showContent(String type, String inputContent){
      Toast.makeText( this , type +  " 输入内容:"  + inputContent, Toast. LENGTH_LONG  ).show();
    }
}
布局文件:
< RelativeLayout  xmlns:android = "http://schemas.android.com/apk/res/android"
     xmlns:tools = "http://schemas.android.com/tools"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"
     android:paddingBottom = "@dimen/activity_vertical_margin"
     android:paddingLeft = "@dimen/activity_horizontal_margin"
     android:paddingRight = "@dimen/activity_horizontal_margin"
     android:paddingTop = "@dimen/activity_vertical_margin"
     tools:context = ".MainActivity"  >

     < EditText
         android:id = "@+id/inputEditText"
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content"
         android:layout_alignParentTop = "true"
         android:layout_centerHorizontal = "true"
         android:layout_marginTop = "32dp"
        />

     < Button
         android:id = "@+id/type1"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_alignRight = "@+id/inputEditText"
         android:layout_below = "@+id/inputEditText"
         android:text = "@string/type1"  />
     < Button
         android:id = "@+id/type2"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_alignRight = "@+id/inputEditText"
         android:layout_below = "@+id/type1"
         android:text = "@string/type2"  />
     < Button
         android:id = "@+id/type3"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_alignRight = "@+id/inputEditText"
         android:layout_below = "@+id/type2"
         android:text = "@string/type3"  />
     < Button
         android:id = "@+id/type4"
         android:onClick = "type4"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_alignRight = "@+id/inputEditText"
         android:layout_below = "@+id/type3"
         android:text = "@string/type4"  />

</ RelativeLayout >

第一种方式:无名内部类实现 OnClickListener接口
第二种方式:定义一个My OnClickListener类,该类实现 OnClickListener接口
第三种方式:activity实现 OnClickListener接口
第四种方式:布局文件绑定一个activity的方法
     在这四种方式中,前三种都是基于实现 OnClickListener接口,再在按钮上绑定这个类的方式实现,涉及观察者模式。第四种方式通过布局文件绑定方法的方式,通过java反射机制实现的。总的来说这四种方式第三种方式是最好的,当在一个界面的涉及很多按钮时,可以统一在一个方法里面判断,绑定也相对简单( setOnClickListener(  this )即可 ),对于第一方式,代码量比较多,比较分散,后期维护时必须找到响应代码修改,第二种方式在不同事件时,需要定义不同的类,也不利于维护,第四种方式同样的问题。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值