Android的四种处理事件方式

  1. 掌握事件的处理方法。
  2. 掌握组件间的数据传递方法。
  3. 掌握数据回传的方法。

 

  1. 分别用四种方式实现事件的处理。

方法一、定义事件监听器,再与组件绑定

button = (Button)findViewById(R.id.button11);获取代表监听器的对象
textView = (TextView)findViewById(R.id.text11);
ButtonClickListener listener = new ButtonClickListener();生成监听器
button.setOnClickListener(listener);绑定监听器

定义一个类,实现监听器接口

class ButtonClickListener implements View.OnClickListener{
     @Override
      public void onClick(View view) {
         textView.setTextColor(Color.CYAN );
         textView.setTextSize(24);点击后字体大小为24,颜色为CYAN 
     }
}

 

方法二、在与组件绑定时定义事件监听器

button2 = (Button)findViewById(R.id.button2);
textView2 = (TextView)findViewById(R.id.text2);

生成监听器并且绑定监听器
button2.setOnClickListener(new View.OnClickListener(){
     @Override
     public void onClick(View view) {
         textView2.setTextColor(Color.BLUE);点击后字体大小为40,颜色为BLUE
         textView2.setTextSize(40);
     }
});

方法在当前Activity实现监听接口

button3 = (Button)findViewById(R.id.button3);
     textView3 =(TextView)findViewById(R.id.text3);
     button3.setOnClickListener(this);当前Activity实现监听接口
public void onClick(View v){
     textView3.setTextSize(40);
     textView3.setTextColor(Color.GREEN);点击后字体大小为40,颜色为GREEN
}

 

方法在XML布局文件中设置回调方法名

<Button
     android:id="@+id/button4"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
    android:onClick="buttonClick"回调方法名
     android:text="点击4"/>

button4 = (Button)findViewById(R.id.button4);
     textView4 =(TextView)findViewById(R.id.text4);
public void buttonClick(View v){
     textView4.setTextColor(Color.BLUE);
     textView4.setTextSize(50);点击后字体大小为50,颜色为BLUE
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值