使用内部类实现事件监听器

1. 使用内部类实现事件监听器的步骤

  • 第一步(定义成员变量):将内部类中要控制的组件定义为当前的Activity的成员变量
  • 第二步(获取组件):在当前Activity的onCreate()方法中获取需要的组件
  • 第三步(定义内部类):在当前的Activity中定义内部类成员,并编写事件处理器代码
  • 第四步(创建监听器):在当前Activity的onCreate()方法中创建监听器
  • 第五步(为事件源注册监听器):在当前Activity的onCreate()方法中为事件源注册监听器
    2.如
Layout下的activity_main_xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/test"
        android:text="测试文字"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp"
        android:gravity="center_horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/red"
            android:text="红色" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/green"
            android:text="绿色"
            android:layout_marginLeft="10dp"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/blue"
            android:text="蓝色"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>
</LinearLayout>

测试界面布局

MainActivity.java下的代码

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView test;             //定义成员变量
    private Button red, green, blue;   //定义成员变量
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        test=(TextView) findViewById(R.id.test);  //获取组件
        red=(Button) findViewById(R.id.red);
        green=(Button) findViewById(R.id.green);
        blue=(Button) findViewById(R.id.blue);
        InterLinsten interLinsten=new InterLinsten();//创建监听器
        red.setOnClickListener(interLinsten);        //注册监听器
        blue.setOnClickListener(interLinsten);
        green.setOnClickListener(interLinsten);
    }
    //定义内部类InterLinsten实现(implement)事件响应
    private class InterLinsten implements View.OnClickListener{ 

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.red:
                    test.setTextColor(Color.RED);
                    break;
                case R.id.blue:
                    test.setTextColor(Color.BLUE);
                    break;
                case R.id.green:
                    test.setTextColor(Color.GREEN);
                default:break;
            }

        }
    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值