一个监听器绑定多个按钮控件

该博客介绍了如何在Android应用中使用同一个监听器ButtonListener为多个按钮设置点击事件。每个按钮点击后会启动不同的Activity,例如btnS01E01点击后启动S01E01Activity,以此类推。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class IndexActivity extends AppCompatActivity {

    private Button btnS01E01;
    private Button btnS01E02;
    private Button btnS01E03;
    private Button btnS01E04;
    private Button btnS01E05;
    private Button btnS01E06;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        btnS01E01 = (Button)findViewById(R.id.btnS01E01);
        btnS01E02 = (Button)findViewById(R.id.btnS01E02);
        btnS01E03 = (Button)findViewById(R.id.btnS01E03);
        btnS01E04 = (Button)findViewById(R.id.btnS01E04);
        btnS01E05 = (Button)findViewById(R.id.btnS01E05);
        btnS01E06 = (Button)findViewById(R.id.btnS01E06);

        btnS01E01.setOnClickListener(new ButtonListener());
        btnS01E02.setOnClickListener(new ButtonListener());
        btnS01E03.setOnClickListener(new ButtonListener());
        btnS01E04.setOnClickListener(new ButtonListener());
        btnS01E05.setOnClickListener(new ButtonListener());
        btnS01E06.setOnClickListener(new ButtonListener());
    }

    class ButtonListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btnS01E01:
                    startActivity(new Intent(IndexActivity.this, S01E01Activity.class));
                    break;
                case R.id.btnS01E02:
                    startActivity(new Intent(IndexActivity.this, S01E02Activity.class));
                    break;
                case R.id.btnS01E03:
                    startActivity(new Intent(IndexActivity.this, S01E03Activity.class));
                    break;
                case R.id.btnS01E04:
                    startActivity(new Intent(IndexActivity.this, S01E04Activity.class));
                    break;
                case R.id.btnS01E05:
                    startActivity(new Intent(IndexActivity.this, S01E05Activity.class));
                    break;
                case R.id.btnS01E06:
                    startActivity(new Intent(IndexActivity.this, S01E06Activity.class));
                    break;
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值